Skip to content

Commit 8d2a9dd

Browse files
committed
Generic/DisallowLongArray: fix undefined index when running fixer
Came across this error when running fixer conflict checks for the various standards. Error encountered: `Undefined index: in phpcs/src/Fixer.php on line 524` Error occurred when due to, for instance, a git merge conflict artifact, the array closer has not been determined. Includes unit tests. To reproduce/test: take the updated unit test file and just run the original sniff over it.
1 parent be6850f commit 8d2a9dd

File tree

4 files changed

+44
-3
lines changed

4 files changed

+44
-3
lines changed

src/Standards/Generic/Sniffs/Arrays/DisallowLongArraySyntaxSniff.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,23 @@ public function register()
3939
*/
4040
public function process(File $phpcsFile, $stackPtr)
4141
{
42+
$tokens = $phpcsFile->getTokens();
43+
4244
$phpcsFile->recordMetric($stackPtr, 'Short array syntax used', 'no');
4345

4446
$error = 'Short array syntax must be used to define arrays';
45-
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'Found');
47+
48+
if (isset($tokens[$stackPtr]['parenthesis_opener']) === false
49+
|| isset($tokens[$stackPtr]['parenthesis_closer']) === false
50+
) {
51+
// Live coding/parse error, just show the error, don't try and fix it.
52+
$phpcsFile->addError($error, $stackPtr, 'Found');
53+
return;
54+
}
55+
56+
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'Found');
4657

4758
if ($fix === true) {
48-
$tokens = $phpcsFile->getTokens();
4959
$opener = $tokens[$stackPtr]['parenthesis_opener'];
5060
$closer = $tokens[$stackPtr]['parenthesis_closer'];
5161

src/Standards/Generic/Tests/Arrays/DisallowLongArraySyntaxUnitTest.inc

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,18 @@ abstract function foo(): array;
2121
abstract function foo(): Foo\Bar;
2222

2323
abstract function foo(): \Foo\Bar;
24+
25+
// The following function has a simulated git conflict for testing.
26+
// This is not a merge conflict - it is a valid test case to test handling of arrays without associated closer.
27+
// Please do not remove.
28+
function test()
29+
{
30+
$arr = array(
31+
'a' => 'a'
32+
<<<<<<< HEAD
33+
'b' => 'b'
34+
=======
35+
'c' => 'c'
36+
>>>>>>> master
37+
);
38+
}

src/Standards/Generic/Tests/Arrays/DisallowLongArraySyntaxUnitTest.inc.fixed

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ $foo = [
1010
3
1111
];
1212
$var = /*comment*/[1,2,3];
13-
$var = [];
13+
$var = array;
1414

1515
function foo(array $array) {}
1616

@@ -21,3 +21,18 @@ abstract function foo(): array;
2121
abstract function foo(): Foo\Bar;
2222

2323
abstract function foo(): \Foo\Bar;
24+
25+
// The following function has a simulated git conflict for testing.
26+
// This is not a merge conflict - it is a valid test case to test handling of arrays without associated closer.
27+
// Please do not remove.
28+
function test()
29+
{
30+
$arr = array(
31+
'a' => 'a'
32+
<<<<<<< HEAD
33+
'b' => 'b'
34+
=======
35+
'c' => 'c'
36+
>>>>>>> master
37+
);
38+
}

src/Standards/Generic/Tests/Arrays/DisallowLongArraySyntaxUnitTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public function getErrorList()
3232
7 => 1,
3333
12 => 1,
3434
13 => 1,
35+
30 => 1,
3536
];
3637

3738
}//end getErrorList()

0 commit comments

Comments
 (0)