diff --git a/src/Standards/Generic/Sniffs/Arrays/DisallowLongArraySyntaxSniff.php b/src/Standards/Generic/Sniffs/Arrays/DisallowLongArraySyntaxSniff.php index 58d26325c0..6854945ad2 100644 --- a/src/Standards/Generic/Sniffs/Arrays/DisallowLongArraySyntaxSniff.php +++ b/src/Standards/Generic/Sniffs/Arrays/DisallowLongArraySyntaxSniff.php @@ -45,9 +45,7 @@ public function process(File $phpcsFile, $stackPtr) $error = 'Short array syntax must be used to define arrays'; - if (isset($tokens[$stackPtr]['parenthesis_opener']) === false - || isset($tokens[$stackPtr]['parenthesis_closer']) === false - ) { + if (isset($tokens[$stackPtr]['parenthesis_opener'], $tokens[$stackPtr]['parenthesis_closer']) === false) { // Live coding/parse error, just show the error, don't try and fix it. $phpcsFile->addError($error, $stackPtr, 'Found'); return; @@ -61,13 +59,9 @@ public function process(File $phpcsFile, $stackPtr) $phpcsFile->fixer->beginChangeset(); - if ($opener === null) { - $phpcsFile->fixer->replaceToken($stackPtr, '[]'); - } else { - $phpcsFile->fixer->replaceToken($stackPtr, ''); - $phpcsFile->fixer->replaceToken($opener, '['); - $phpcsFile->fixer->replaceToken($closer, ']'); - } + $phpcsFile->fixer->replaceToken($stackPtr, ''); + $phpcsFile->fixer->replaceToken($opener, '['); + $phpcsFile->fixer->replaceToken($closer, ']'); $phpcsFile->fixer->endChangeset(); } diff --git a/src/Standards/Generic/Tests/Arrays/DisallowLongArraySyntaxUnitTest.1.inc b/src/Standards/Generic/Tests/Arrays/DisallowLongArraySyntaxUnitTest.1.inc index 60c2ef96f6..6855f02cb7 100644 --- a/src/Standards/Generic/Tests/Arrays/DisallowLongArraySyntaxUnitTest.1.inc +++ b/src/Standards/Generic/Tests/Arrays/DisallowLongArraySyntaxUnitTest.1.inc @@ -3,14 +3,13 @@ $var = array(); $var = [1,2,3]; $var = array(1,2,3); echo $var[1]; -$foo = array($var[1],$var[2]); +$foo = ARRAY($var[1],$var[2]); $foo = array( 1, 2, 3 ); $var = array/*comment*/(1,2,3); -$var = array; function foo(array $array) {} @@ -26,3 +25,9 @@ array_map( static fn (array $value): array => array_filter($value), [] ); + +class Foo { + function array() {} +} + +$obj->array( 1, 2, 3 ); diff --git a/src/Standards/Generic/Tests/Arrays/DisallowLongArraySyntaxUnitTest.1.inc.fixed b/src/Standards/Generic/Tests/Arrays/DisallowLongArraySyntaxUnitTest.1.inc.fixed index e37971f238..5e99306845 100644 --- a/src/Standards/Generic/Tests/Arrays/DisallowLongArraySyntaxUnitTest.1.inc.fixed +++ b/src/Standards/Generic/Tests/Arrays/DisallowLongArraySyntaxUnitTest.1.inc.fixed @@ -10,7 +10,6 @@ $foo = [ 3 ]; $var = /*comment*/[1,2,3]; -$var = array; function foo(array $array) {} @@ -26,3 +25,9 @@ array_map( static fn (array $value): array => array_filter($value), [] ); + +class Foo { + function array() {} +} + +$obj->array( 1, 2, 3 ); diff --git a/src/Standards/Generic/Tests/Arrays/DisallowLongArraySyntaxUnitTest.3.inc b/src/Standards/Generic/Tests/Arrays/DisallowLongArraySyntaxUnitTest.3.inc new file mode 100644 index 0000000000..68020bdcae --- /dev/null +++ b/src/Standards/Generic/Tests/Arrays/DisallowLongArraySyntaxUnitTest.3.inc @@ -0,0 +1,7 @@ + 1, 9 => 1, ]; + case 'DisallowLongArraySyntaxUnitTest.3.inc': + return [ + 7 => 1, + ]; default: return []; }//end switch