From 696465906ff3ed70f262d05b2c2f862b18a8692c Mon Sep 17 00:00:00 2001 From: Rodrigo Primo Date: Mon, 18 Dec 2023 11:14:56 -0300 Subject: [PATCH 1/3] Rename test case files for ArbitraryParenthesesSpacing sniff Doing this to be able to add more test case files to cover defensive code in the ArbitraryParenthesesSpacing sniff that protects it when checking code with parse errors. --- ...ArbitraryParenthesesSpacingUnitTest.1.inc} | 0 ...aryParenthesesSpacingUnitTest.1.inc.fixed} | 0 .../ArbitraryParenthesesSpacingUnitTest.php | 98 +++++++++++-------- 3 files changed, 57 insertions(+), 41 deletions(-) rename src/Standards/Generic/Tests/WhiteSpace/{ArbitraryParenthesesSpacingUnitTest.inc => ArbitraryParenthesesSpacingUnitTest.1.inc} (100%) rename src/Standards/Generic/Tests/WhiteSpace/{ArbitraryParenthesesSpacingUnitTest.inc.fixed => ArbitraryParenthesesSpacingUnitTest.1.inc.fixed} (100%) diff --git a/src/Standards/Generic/Tests/WhiteSpace/ArbitraryParenthesesSpacingUnitTest.inc b/src/Standards/Generic/Tests/WhiteSpace/ArbitraryParenthesesSpacingUnitTest.1.inc similarity index 100% rename from src/Standards/Generic/Tests/WhiteSpace/ArbitraryParenthesesSpacingUnitTest.inc rename to src/Standards/Generic/Tests/WhiteSpace/ArbitraryParenthesesSpacingUnitTest.1.inc diff --git a/src/Standards/Generic/Tests/WhiteSpace/ArbitraryParenthesesSpacingUnitTest.inc.fixed b/src/Standards/Generic/Tests/WhiteSpace/ArbitraryParenthesesSpacingUnitTest.1.inc.fixed similarity index 100% rename from src/Standards/Generic/Tests/WhiteSpace/ArbitraryParenthesesSpacingUnitTest.inc.fixed rename to src/Standards/Generic/Tests/WhiteSpace/ArbitraryParenthesesSpacingUnitTest.1.inc.fixed diff --git a/src/Standards/Generic/Tests/WhiteSpace/ArbitraryParenthesesSpacingUnitTest.php b/src/Standards/Generic/Tests/WhiteSpace/ArbitraryParenthesesSpacingUnitTest.php index 124efb7d11..488754337d 100644 --- a/src/Standards/Generic/Tests/WhiteSpace/ArbitraryParenthesesSpacingUnitTest.php +++ b/src/Standards/Generic/Tests/WhiteSpace/ArbitraryParenthesesSpacingUnitTest.php @@ -26,45 +26,53 @@ class ArbitraryParenthesesSpacingUnitTest 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 [ - 64 => 4, - 66 => 1, - 68 => 1, - 69 => 1, - 72 => 2, - 73 => 2, - 77 => 2, - 81 => 4, - 90 => 4, - 94 => 1, - 95 => 1, - 97 => 1, - 100 => 2, - 101 => 2, - 104 => 2, - 107 => 2, - 109 => 4, - 111 => 4, - 113 => 2, - 115 => 2, - 123 => 1, - 125 => 2, - 127 => 1, - 131 => 1, - 133 => 1, - 137 => 1, - 139 => 2, - 141 => 1, - 144 => 1, - 146 => 1, - 163 => 1, - 164 => 1, - 165 => 1, - ]; + switch ($testFile) { + case 'ArbitraryParenthesesSpacingUnitTest.1.inc': + return [ + 64 => 4, + 66 => 1, + 68 => 1, + 69 => 1, + 72 => 2, + 73 => 2, + 77 => 2, + 81 => 4, + 90 => 4, + 94 => 1, + 95 => 1, + 97 => 1, + 100 => 2, + 101 => 2, + 104 => 2, + 107 => 2, + 109 => 4, + 111 => 4, + 113 => 2, + 115 => 2, + 123 => 1, + 125 => 2, + 127 => 1, + 131 => 1, + 133 => 1, + 137 => 1, + 139 => 2, + 141 => 1, + 144 => 1, + 146 => 1, + 163 => 1, + 164 => 1, + 165 => 1, + ]; + + default: + return []; + }//end switch }//end getErrorList() @@ -75,14 +83,22 @@ public function getErrorList() * The key of the array should represent the line number and the value * should represent the number of warnings that should occur on that line. * + * @param string $testFile The name of the file being tested. + * * @return array */ - public function getWarningList() + public function getWarningList($testFile='') { - return [ - 55 => 1, - 56 => 1, - ]; + switch ($testFile) { + case 'ArbitraryParenthesesSpacingUnitTest.1.inc': + return [ + 55 => 1, + 56 => 1, + ]; + + default: + return []; + }//end switch }//end getWarningList() From 77390f43c39aae10f218d7fe3fd06291a3fa58ac Mon Sep 17 00:00:00 2001 From: Rodrigo Primo Date: Mon, 18 Dec 2023 11:24:55 -0300 Subject: [PATCH 2/3] Improve test coverage for the ArbitraryParenthesesSpacing sniff This commit improves the test coverage for the ArbitraryParenthesesSpacing sniff by adding one more test case files to exercise a part of the sniff code that checks for invalid syntax. --- .../WhiteSpace/ArbitraryParenthesesSpacingUnitTest.2.inc | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 src/Standards/Generic/Tests/WhiteSpace/ArbitraryParenthesesSpacingUnitTest.2.inc diff --git a/src/Standards/Generic/Tests/WhiteSpace/ArbitraryParenthesesSpacingUnitTest.2.inc b/src/Standards/Generic/Tests/WhiteSpace/ArbitraryParenthesesSpacingUnitTest.2.inc new file mode 100644 index 0000000000..3d5bcd0a5c --- /dev/null +++ b/src/Standards/Generic/Tests/WhiteSpace/ArbitraryParenthesesSpacingUnitTest.2.inc @@ -0,0 +1,4 @@ + Date: Tue, 19 Dec 2023 10:57:09 -0300 Subject: [PATCH 3/3] Add a test to ensure the sniff is not triggered for the match expression Adding this as `match` was added in PHP 8. --- .../WhiteSpace/ArbitraryParenthesesSpacingUnitTest.1.inc | 5 +++++ .../ArbitraryParenthesesSpacingUnitTest.1.inc.fixed | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/src/Standards/Generic/Tests/WhiteSpace/ArbitraryParenthesesSpacingUnitTest.1.inc b/src/Standards/Generic/Tests/WhiteSpace/ArbitraryParenthesesSpacingUnitTest.1.inc index 2399a387e1..8e17dbb644 100644 --- a/src/Standards/Generic/Tests/WhiteSpace/ArbitraryParenthesesSpacingUnitTest.1.inc +++ b/src/Standards/Generic/Tests/WhiteSpace/ArbitraryParenthesesSpacingUnitTest.1.inc @@ -175,3 +175,8 @@ class NonArbitraryParenthesesWithKeywords { $d = new parent( $foo,$bar ); } } + +// Test that the match expression does not trigger the sniff. +$b = match ( $a ) { + 1 => true, +}; diff --git a/src/Standards/Generic/Tests/WhiteSpace/ArbitraryParenthesesSpacingUnitTest.1.inc.fixed b/src/Standards/Generic/Tests/WhiteSpace/ArbitraryParenthesesSpacingUnitTest.1.inc.fixed index 9162728e17..c215753ccb 100644 --- a/src/Standards/Generic/Tests/WhiteSpace/ArbitraryParenthesesSpacingUnitTest.1.inc.fixed +++ b/src/Standards/Generic/Tests/WhiteSpace/ArbitraryParenthesesSpacingUnitTest.1.inc.fixed @@ -163,3 +163,8 @@ class NonArbitraryParenthesesWithKeywords { $d = new parent( $foo,$bar ); } } + +// Test that the match expression does not trigger the sniff. +$b = match ( $a ) { + 1 => true, +};