From a0c505ec1ffcc71eff2a224150b0ed5ba47c0a52 Mon Sep 17 00:00:00 2001 From: Rodrigo Primo Date: Wed, 10 Apr 2024 09:57:37 -0300 Subject: [PATCH 1/4] Generic/DisallowLongArraySyntax: move an intentional parse error test This commit moves an intentional parse error test to its own file. The parse error test did not trigger DisallowLongArraySyntaxSniff::process() as this sniff listen for T_ARRAY and the `array` keyword without parentheses is tokenized by PHPCS as T_STRING. Removing the semicolon fixes that and makes the test effective. --- .../Tests/Arrays/DisallowLongArraySyntaxUnitTest.1.inc | 1 - .../Arrays/DisallowLongArraySyntaxUnitTest.1.inc.fixed | 1 - .../Tests/Arrays/DisallowLongArraySyntaxUnitTest.3.inc | 7 +++++++ .../Tests/Arrays/DisallowLongArraySyntaxUnitTest.php | 4 ++++ 4 files changed, 11 insertions(+), 2 deletions(-) create mode 100644 src/Standards/Generic/Tests/Arrays/DisallowLongArraySyntaxUnitTest.3.inc diff --git a/src/Standards/Generic/Tests/Arrays/DisallowLongArraySyntaxUnitTest.1.inc b/src/Standards/Generic/Tests/Arrays/DisallowLongArraySyntaxUnitTest.1.inc index 60c2ef96f6..6899947aca 100644 --- a/src/Standards/Generic/Tests/Arrays/DisallowLongArraySyntaxUnitTest.1.inc +++ b/src/Standards/Generic/Tests/Arrays/DisallowLongArraySyntaxUnitTest.1.inc @@ -10,7 +10,6 @@ $foo = array( 3 ); $var = array/*comment*/(1,2,3); -$var = array; function foo(array $array) {} diff --git a/src/Standards/Generic/Tests/Arrays/DisallowLongArraySyntaxUnitTest.1.inc.fixed b/src/Standards/Generic/Tests/Arrays/DisallowLongArraySyntaxUnitTest.1.inc.fixed index e37971f238..3c2d49aa47 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) {} 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 From 8c09939c1ce977bd8cdd0eae037cbc2237d49d87 Mon Sep 17 00:00:00 2001 From: Rodrigo Primo Date: Thu, 18 Apr 2024 14:45:44 -0300 Subject: [PATCH 2/4] Generic/DisallowLongArraySyntax: simplify if statement This commit uses a single isset() to check if two variables are set instead of using two isset() calls. --- .../Generic/Sniffs/Arrays/DisallowLongArraySyntaxSniff.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/Standards/Generic/Sniffs/Arrays/DisallowLongArraySyntaxSniff.php b/src/Standards/Generic/Sniffs/Arrays/DisallowLongArraySyntaxSniff.php index 58d26325c0..72cf8a3593 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; From 2000a32dd7346d3d579373025a251d5fc43c5566 Mon Sep 17 00:00:00 2001 From: Rodrigo Primo Date: Thu, 18 Apr 2024 15:07:46 -0300 Subject: [PATCH 3/4] Generic/DisallowLongArraySyntax: remove unreachable condition A check was added to the top of the file in 8d2a9dd to return an error early if the array is missing an opening and/or a closing parenthesis. This means that `$opener` can never be null as when the code reaches the point where this variable is set, `$tokens[$stackPtr]['parenthesis_opener']` cannot be null. --- .../Sniffs/Arrays/DisallowLongArraySyntaxSniff.php | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/src/Standards/Generic/Sniffs/Arrays/DisallowLongArraySyntaxSniff.php b/src/Standards/Generic/Sniffs/Arrays/DisallowLongArraySyntaxSniff.php index 72cf8a3593..6854945ad2 100644 --- a/src/Standards/Generic/Sniffs/Arrays/DisallowLongArraySyntaxSniff.php +++ b/src/Standards/Generic/Sniffs/Arrays/DisallowLongArraySyntaxSniff.php @@ -59,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(); } From e1f0e462b60f98d111969aa686b7dead2c36c543 Mon Sep 17 00:00:00 2001 From: Rodrigo Primo Date: Thu, 2 May 2024 10:43:39 -0300 Subject: [PATCH 4/4] Generic/DisallowLongArraySyntax: improve code coverage This commit adds two safeguard tests that do not trigger the sniff as they use the array keyword in a method definition and a method call. It also updates one of the existing tests to declare an array using array in uppercase. This is already handled correctly by PHPCS but it was not documented in a test. --- .../Tests/Arrays/DisallowLongArraySyntaxUnitTest.1.inc | 8 +++++++- .../Arrays/DisallowLongArraySyntaxUnitTest.1.inc.fixed | 6 ++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/Standards/Generic/Tests/Arrays/DisallowLongArraySyntaxUnitTest.1.inc b/src/Standards/Generic/Tests/Arrays/DisallowLongArraySyntaxUnitTest.1.inc index 6899947aca..6855f02cb7 100644 --- a/src/Standards/Generic/Tests/Arrays/DisallowLongArraySyntaxUnitTest.1.inc +++ b/src/Standards/Generic/Tests/Arrays/DisallowLongArraySyntaxUnitTest.1.inc @@ -3,7 +3,7 @@ $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, @@ -25,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 3c2d49aa47..5e99306845 100644 --- a/src/Standards/Generic/Tests/Arrays/DisallowLongArraySyntaxUnitTest.1.inc.fixed +++ b/src/Standards/Generic/Tests/Arrays/DisallowLongArraySyntaxUnitTest.1.inc.fixed @@ -25,3 +25,9 @@ array_map( static fn (array $value): array => array_filter($value), [] ); + +class Foo { + function array() {} +} + +$obj->array( 1, 2, 3 );