From b41ae595f03cff49306ca592c15783034b402ff3 Mon Sep 17 00:00:00 2001 From: Rodrigo Primo Date: Wed, 20 Dec 2023 10:44:40 -0300 Subject: [PATCH 1/3] Rename test case files for LanguageConstructSpacing sniff Doing this to be able to move tests with syntax errors to their own file. --- ...=> LanguageConstructSpacingUnitTest.1.inc} | 0 ...guageConstructSpacingUnitTest.1.inc.fixed} | 0 .../LanguageConstructSpacingUnitTest.php | 64 +++++++++++-------- 3 files changed, 36 insertions(+), 28 deletions(-) rename src/Standards/Generic/Tests/WhiteSpace/{LanguageConstructSpacingUnitTest.inc => LanguageConstructSpacingUnitTest.1.inc} (100%) rename src/Standards/Generic/Tests/WhiteSpace/{LanguageConstructSpacingUnitTest.inc.fixed => LanguageConstructSpacingUnitTest.1.inc.fixed} (100%) diff --git a/src/Standards/Generic/Tests/WhiteSpace/LanguageConstructSpacingUnitTest.inc b/src/Standards/Generic/Tests/WhiteSpace/LanguageConstructSpacingUnitTest.1.inc similarity index 100% rename from src/Standards/Generic/Tests/WhiteSpace/LanguageConstructSpacingUnitTest.inc rename to src/Standards/Generic/Tests/WhiteSpace/LanguageConstructSpacingUnitTest.1.inc diff --git a/src/Standards/Generic/Tests/WhiteSpace/LanguageConstructSpacingUnitTest.inc.fixed b/src/Standards/Generic/Tests/WhiteSpace/LanguageConstructSpacingUnitTest.1.inc.fixed similarity index 100% rename from src/Standards/Generic/Tests/WhiteSpace/LanguageConstructSpacingUnitTest.inc.fixed rename to src/Standards/Generic/Tests/WhiteSpace/LanguageConstructSpacingUnitTest.1.inc.fixed diff --git a/src/Standards/Generic/Tests/WhiteSpace/LanguageConstructSpacingUnitTest.php b/src/Standards/Generic/Tests/WhiteSpace/LanguageConstructSpacingUnitTest.php index b11fedcaba..185da98ef8 100644 --- a/src/Standards/Generic/Tests/WhiteSpace/LanguageConstructSpacingUnitTest.php +++ b/src/Standards/Generic/Tests/WhiteSpace/LanguageConstructSpacingUnitTest.php @@ -26,37 +26,45 @@ class LanguageConstructSpacingUnitTest 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 [ - 3 => 1, - 7 => 1, - 11 => 1, - 15 => 1, - 19 => 1, - 23 => 1, - 27 => 1, - 30 => 1, - 33 => 1, - 34 => 1, - 35 => 1, - 36 => 1, - 38 => 1, - 44 => 1, - 45 => 1, - 46 => 2, - 49 => 1, - 51 => 1, - 59 => 1, - 61 => 1, - 63 => 1, - 67 => 1, - 70 => 1, - 71 => 1, - 75 => 1, - ]; + switch ($testFile) { + case 'LanguageConstructSpacingUnitTest.1.inc': + return [ + 3 => 1, + 7 => 1, + 11 => 1, + 15 => 1, + 19 => 1, + 23 => 1, + 27 => 1, + 30 => 1, + 33 => 1, + 34 => 1, + 35 => 1, + 36 => 1, + 38 => 1, + 44 => 1, + 45 => 1, + 46 => 2, + 49 => 1, + 51 => 1, + 59 => 1, + 61 => 1, + 63 => 1, + 67 => 1, + 70 => 1, + 71 => 1, + 75 => 1, + ]; + + default: + return []; + }//end switch }//end getErrorList() From 6355e6e024e67045eecb3bd8d5fb9c87d21ec578 Mon Sep 17 00:00:00 2001 From: Rodrigo Primo Date: Wed, 20 Dec 2023 11:05:32 -0300 Subject: [PATCH 2/3] Move intentional parse error to its own file --- .../Tests/WhiteSpace/LanguageConstructSpacingUnitTest.1.inc | 3 --- .../WhiteSpace/LanguageConstructSpacingUnitTest.1.inc.fixed | 3 --- .../Tests/WhiteSpace/LanguageConstructSpacingUnitTest.2.inc | 4 ++++ 3 files changed, 4 insertions(+), 6 deletions(-) create mode 100644 src/Standards/Generic/Tests/WhiteSpace/LanguageConstructSpacingUnitTest.2.inc diff --git a/src/Standards/Generic/Tests/WhiteSpace/LanguageConstructSpacingUnitTest.1.inc b/src/Standards/Generic/Tests/WhiteSpace/LanguageConstructSpacingUnitTest.1.inc index 505cb6e40c..5353321ecc 100644 --- a/src/Standards/Generic/Tests/WhiteSpace/LanguageConstructSpacingUnitTest.1.inc +++ b/src/Standards/Generic/Tests/WhiteSpace/LanguageConstructSpacingUnitTest.1.inc @@ -74,6 +74,3 @@ $newLine; // The following line must have a single space at the end (after return) return $spaceAndNewLine; - -// The following line must be the last line in the file -return diff --git a/src/Standards/Generic/Tests/WhiteSpace/LanguageConstructSpacingUnitTest.1.inc.fixed b/src/Standards/Generic/Tests/WhiteSpace/LanguageConstructSpacingUnitTest.1.inc.fixed index 9934030bbe..9b5ad5816d 100644 --- a/src/Standards/Generic/Tests/WhiteSpace/LanguageConstructSpacingUnitTest.1.inc.fixed +++ b/src/Standards/Generic/Tests/WhiteSpace/LanguageConstructSpacingUnitTest.1.inc.fixed @@ -68,6 +68,3 @@ return $newLine; // The following line must have a single space at the end (after return) return $spaceAndNewLine; - -// The following line must be the last line in the file -return diff --git a/src/Standards/Generic/Tests/WhiteSpace/LanguageConstructSpacingUnitTest.2.inc b/src/Standards/Generic/Tests/WhiteSpace/LanguageConstructSpacingUnitTest.2.inc new file mode 100644 index 0000000000..5c9325fcaa --- /dev/null +++ b/src/Standards/Generic/Tests/WhiteSpace/LanguageConstructSpacingUnitTest.2.inc @@ -0,0 +1,4 @@ + Date: Wed, 20 Dec 2023 11:46:28 -0300 Subject: [PATCH 3/3] Improve test coverage of the LanguageConstructSpacing sniff This sniff has an `else if` condition (https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/c855348766a424d23425eba5cc4420e26e4a3e38/src/Standards/Generic/Sniffs/WhiteSpace/LanguageConstructSpacingSniff.php#L131) to handle language constructs that are followed by some content without a space or opening parenthesis first. This code was not exercised by the automated tests. This commit adds a few examples to make sure this part of the code is covered. --- .../LanguageConstructSpacingUnitTest.1.inc | 15 +++++++ ...nguageConstructSpacingUnitTest.1.inc.fixed | 15 +++++++ .../LanguageConstructSpacingUnitTest.php | 41 ++++++++++++------- 3 files changed, 56 insertions(+), 15 deletions(-) diff --git a/src/Standards/Generic/Tests/WhiteSpace/LanguageConstructSpacingUnitTest.1.inc b/src/Standards/Generic/Tests/WhiteSpace/LanguageConstructSpacingUnitTest.1.inc index 5353321ecc..1847778d09 100644 --- a/src/Standards/Generic/Tests/WhiteSpace/LanguageConstructSpacingUnitTest.1.inc +++ b/src/Standards/Generic/Tests/WhiteSpace/LanguageConstructSpacingUnitTest.1.inc @@ -2,32 +2,42 @@ echo $blah; echo $blah; echo($blah); +echo$blah; print $blah; print $blah; print($blah); +print$blah; include $blah; include $blah; include($blah); +include$blah; include_once $blah; include_once $blah; include_once($blah); +include_once$blah; require $blah; require $blah; require($blah); +require$blah; require_once $blah; require_once $blah; require_once($blah); +require_once$blah; $obj = new MyClass(); $obj = new MyClass(); +$obj = new $className(); +$obj = new$className(); + yield $blah; yield $blah; +yield$blah; yield from $test(); yield FROM $test(); @@ -39,12 +49,16 @@ yield From $test(); +yield FROM$test(); throw new Exception(); throw new Exception(); throw new Exception(); throw new Exception(); +throw $exception; +throw$exception; + namespace MyClass; namespace MyClass; namespace MyNamespace\MyClass; @@ -66,6 +80,7 @@ return; return $blah; return $blah; return($blah); +return$blah; return $tab; return diff --git a/src/Standards/Generic/Tests/WhiteSpace/LanguageConstructSpacingUnitTest.1.inc.fixed b/src/Standards/Generic/Tests/WhiteSpace/LanguageConstructSpacingUnitTest.1.inc.fixed index 9b5ad5816d..4f5d3cec2f 100644 --- a/src/Standards/Generic/Tests/WhiteSpace/LanguageConstructSpacingUnitTest.1.inc.fixed +++ b/src/Standards/Generic/Tests/WhiteSpace/LanguageConstructSpacingUnitTest.1.inc.fixed @@ -2,30 +2,40 @@ echo $blah; echo $blah; echo($blah); +echo $blah; print $blah; print $blah; print($blah); +print $blah; include $blah; include $blah; include($blah); +include $blah; include_once $blah; include_once $blah; include_once($blah); +include_once $blah; require $blah; require $blah; require($blah); +require $blah; require_once $blah; require_once $blah; require_once($blah); +require_once $blah; $obj = new MyClass(); $obj = new MyClass(); +$obj = new $className(); +$obj = new $className(); + +yield $blah; yield $blah; yield $blah; @@ -35,12 +45,16 @@ YIELD from $test(); yield from $test(); yield from $test(); yield From $test(); +yield FROM $test(); throw new Exception(); throw new Exception(); throw new Exception(); throw new Exception(); +throw $exception; +throw $exception; + namespace MyClass; namespace MyClass; namespace MyNamespace\MyClass; @@ -62,6 +76,7 @@ return; return $blah; return $blah; return($blah); +return $blah; return $tab; return $newLine; diff --git a/src/Standards/Generic/Tests/WhiteSpace/LanguageConstructSpacingUnitTest.php b/src/Standards/Generic/Tests/WhiteSpace/LanguageConstructSpacingUnitTest.php index 185da98ef8..0ac7e6d815 100644 --- a/src/Standards/Generic/Tests/WhiteSpace/LanguageConstructSpacingUnitTest.php +++ b/src/Standards/Generic/Tests/WhiteSpace/LanguageConstructSpacingUnitTest.php @@ -36,30 +36,41 @@ public function getErrorList($testFile='') case 'LanguageConstructSpacingUnitTest.1.inc': return [ 3 => 1, - 7 => 1, - 11 => 1, + 5 => 1, + 8 => 1, + 10 => 1, + 13 => 1, 15 => 1, - 19 => 1, + 18 => 1, + 20 => 1, 23 => 1, - 27 => 1, + 25 => 1, + 28 => 1, 30 => 1, 33 => 1, - 34 => 1, - 35 => 1, 36 => 1, - 38 => 1, + 39 => 1, + 40 => 1, + 43 => 1, 44 => 1, 45 => 1, - 46 => 2, - 49 => 1, - 51 => 1, - 59 => 1, - 61 => 1, + 46 => 1, + 48 => 1, + 52 => 1, + 55 => 1, + 56 => 1, + 57 => 2, + 60 => 1, 63 => 1, - 67 => 1, - 70 => 1, - 71 => 1, + 65 => 1, + 73 => 1, 75 => 1, + 77 => 1, + 81 => 1, + 83 => 1, + 85 => 1, + 86 => 1, + 90 => 1, ]; default: