From d9fccc36b5a100a4372db3fac1e03c81d5a99e10 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Sun, 10 Dec 2023 01:37:06 +0100 Subject: [PATCH 1/3] Squiz/ScopeKeywordSpacing: rename the test case file ... to allow for adding additional test case files for this sniff. --- ....inc => ScopeKeywordSpacingUnitTest.1.inc} | 0 ...> ScopeKeywordSpacingUnitTest.1.inc.fixed} | 0 .../ScopeKeywordSpacingUnitTest.php | 56 +++++++++++-------- 3 files changed, 32 insertions(+), 24 deletions(-) rename src/Standards/Squiz/Tests/WhiteSpace/{ScopeKeywordSpacingUnitTest.inc => ScopeKeywordSpacingUnitTest.1.inc} (100%) rename src/Standards/Squiz/Tests/WhiteSpace/{ScopeKeywordSpacingUnitTest.inc.fixed => ScopeKeywordSpacingUnitTest.1.inc.fixed} (100%) diff --git a/src/Standards/Squiz/Tests/WhiteSpace/ScopeKeywordSpacingUnitTest.inc b/src/Standards/Squiz/Tests/WhiteSpace/ScopeKeywordSpacingUnitTest.1.inc similarity index 100% rename from src/Standards/Squiz/Tests/WhiteSpace/ScopeKeywordSpacingUnitTest.inc rename to src/Standards/Squiz/Tests/WhiteSpace/ScopeKeywordSpacingUnitTest.1.inc diff --git a/src/Standards/Squiz/Tests/WhiteSpace/ScopeKeywordSpacingUnitTest.inc.fixed b/src/Standards/Squiz/Tests/WhiteSpace/ScopeKeywordSpacingUnitTest.1.inc.fixed similarity index 100% rename from src/Standards/Squiz/Tests/WhiteSpace/ScopeKeywordSpacingUnitTest.inc.fixed rename to src/Standards/Squiz/Tests/WhiteSpace/ScopeKeywordSpacingUnitTest.1.inc.fixed diff --git a/src/Standards/Squiz/Tests/WhiteSpace/ScopeKeywordSpacingUnitTest.php b/src/Standards/Squiz/Tests/WhiteSpace/ScopeKeywordSpacingUnitTest.php index aaa49d65d1..8b6562c0f6 100644 --- a/src/Standards/Squiz/Tests/WhiteSpace/ScopeKeywordSpacingUnitTest.php +++ b/src/Standards/Squiz/Tests/WhiteSpace/ScopeKeywordSpacingUnitTest.php @@ -26,33 +26,41 @@ class ScopeKeywordSpacingUnitTest extends AbstractSniffUnitTest * The key of the array should represent the line number and the value * should represent the number of errors that should occur on that line. * + * @param string $testFile The name of the file being tested. + * * @return array */ - public function getErrorList() + public function getErrorList($testFile='') { - return [ - 7 => 2, - 8 => 1, - 13 => 1, - 14 => 1, - 15 => 1, - 17 => 2, - 26 => 1, - 28 => 1, - 29 => 1, - 64 => 1, - 67 => 1, - 71 => 1, - 103 => 1, - 106 => 1, - 111 => 1, - 119 => 1, - 121 => 1, - 127 => 2, - 134 => 2, - 138 => 2, - 140 => 3, - ]; + switch ($testFile) { + case 'ScopeKeywordSpacingUnitTest.1.inc': + return [ + 7 => 2, + 8 => 1, + 13 => 1, + 14 => 1, + 15 => 1, + 17 => 2, + 26 => 1, + 28 => 1, + 29 => 1, + 64 => 1, + 67 => 1, + 71 => 1, + 103 => 1, + 106 => 1, + 111 => 1, + 119 => 1, + 121 => 1, + 127 => 2, + 134 => 2, + 138 => 2, + 140 => 3, + ]; + + default: + return []; + }//end switch }//end getErrorList() From 393f2317de6aa936160cccd7a41e2fc152996324 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Sun, 10 Dec 2023 02:03:15 +0100 Subject: [PATCH 2/3] Squiz/ScopeKeywordSpacing: add more defensive coding When the sniff would encounter a parse error/be run during live coding, the sniff could trigger an `Undefined array key` notice for the use of `$stackPtr + 2`. Fixed now by bowing out early if the keyword is found at the end of a file. Includes additional tests. --- .../Squiz/Sniffs/WhiteSpace/ScopeKeywordSpacingSniff.php | 3 +++ .../Tests/WhiteSpace/ScopeKeywordSpacingUnitTest.2.inc | 6 ++++++ .../Tests/WhiteSpace/ScopeKeywordSpacingUnitTest.3.inc | 6 ++++++ .../WhiteSpace/ScopeKeywordSpacingUnitTest.3.inc.fixed | 6 ++++++ .../Squiz/Tests/WhiteSpace/ScopeKeywordSpacingUnitTest.php | 3 +++ 5 files changed, 24 insertions(+) create mode 100644 src/Standards/Squiz/Tests/WhiteSpace/ScopeKeywordSpacingUnitTest.2.inc create mode 100644 src/Standards/Squiz/Tests/WhiteSpace/ScopeKeywordSpacingUnitTest.3.inc create mode 100644 src/Standards/Squiz/Tests/WhiteSpace/ScopeKeywordSpacingUnitTest.3.inc.fixed diff --git a/src/Standards/Squiz/Sniffs/WhiteSpace/ScopeKeywordSpacingSniff.php b/src/Standards/Squiz/Sniffs/WhiteSpace/ScopeKeywordSpacingSniff.php index 1424819f51..6a72c07e19 100644 --- a/src/Standards/Squiz/Sniffs/WhiteSpace/ScopeKeywordSpacingSniff.php +++ b/src/Standards/Squiz/Sniffs/WhiteSpace/ScopeKeywordSpacingSniff.php @@ -127,6 +127,9 @@ public function process(File $phpcsFile, $stackPtr) if ($tokens[($stackPtr + 1)]['code'] !== T_WHITESPACE) { $spacing = 0; + } else if (isset($tokens[($stackPtr + 2)]) === false) { + // Parse error/live coding. Bow out. + return; } else { if ($tokens[($stackPtr + 2)]['line'] !== $tokens[$stackPtr]['line']) { $spacing = 'newline'; diff --git a/src/Standards/Squiz/Tests/WhiteSpace/ScopeKeywordSpacingUnitTest.2.inc b/src/Standards/Squiz/Tests/WhiteSpace/ScopeKeywordSpacingUnitTest.2.inc new file mode 100644 index 0000000000..45cfb53431 --- /dev/null +++ b/src/Standards/Squiz/Tests/WhiteSpace/ScopeKeywordSpacingUnitTest.2.inc @@ -0,0 +1,6 @@ + 3, ]; + case 'ScopeKeywordSpacingUnitTest.3.inc': + return [6 => 1]; + default: return []; }//end switch From 1a046acbdbdc1e434c3742e6a458a6b766213ed4 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Sun, 10 Dec 2023 02:07:20 +0100 Subject: [PATCH 3/3] Squiz/ScopeKeywordSpacing: add extra tests with static in a union type This is already handled correctly, the tests just safeguards this. --- .../Tests/WhiteSpace/ScopeKeywordSpacingUnitTest.1.inc | 6 +++--- .../WhiteSpace/ScopeKeywordSpacingUnitTest.1.inc.fixed | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Standards/Squiz/Tests/WhiteSpace/ScopeKeywordSpacingUnitTest.1.inc b/src/Standards/Squiz/Tests/WhiteSpace/ScopeKeywordSpacingUnitTest.1.inc index 12685dc97d..a76f42fef4 100644 --- a/src/Standards/Squiz/Tests/WhiteSpace/ScopeKeywordSpacingUnitTest.1.inc +++ b/src/Standards/Squiz/Tests/WhiteSpace/ScopeKeywordSpacingUnitTest.1.inc @@ -95,9 +95,9 @@ public static function fCreate($attributes = []): ?static } // Also account for static used within union types. -public function fCreate($attributes = []): object|static -{ -} +public function staticLast($attributes = []): object|static {} +public function staticMiddle(): string|static|object {} +public function staticFirst(): static|object {} // Ensure that static as a scope keyword when preceeded by a colon which is not for a type declaration is still handled. $callback = $cond ? get_fn_name() : static function ($a) { return $a * 10; }; diff --git a/src/Standards/Squiz/Tests/WhiteSpace/ScopeKeywordSpacingUnitTest.1.inc.fixed b/src/Standards/Squiz/Tests/WhiteSpace/ScopeKeywordSpacingUnitTest.1.inc.fixed index d3b682ed75..7fd80626d5 100644 --- a/src/Standards/Squiz/Tests/WhiteSpace/ScopeKeywordSpacingUnitTest.1.inc.fixed +++ b/src/Standards/Squiz/Tests/WhiteSpace/ScopeKeywordSpacingUnitTest.1.inc.fixed @@ -90,9 +90,9 @@ public static function fCreate($attributes = []): ?static } // Also account for static used within union types. -public function fCreate($attributes = []): object|static -{ -} +public function staticLast($attributes = []): object|static {} +public function staticMiddle(): string|static|object {} +public function staticFirst(): static|object {} // Ensure that static as a scope keyword when preceeded by a colon which is not for a type declaration is still handled. $callback = $cond ? get_fn_name() : static function ($a) { return $a * 10; };