From c455e1ef344fc36eac3ea2624956bcb245fba2d8 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Sat, 15 Mar 2025 20:33:24 +0100 Subject: [PATCH 1/3] Tests: rename some test case files ... in anticipation of additional test case files for parse error being added. --- ...nitTest.inc => ClassCommentUnitTest.1.inc} | 0 .../Tests/Commenting/ClassCommentUnitTest.php | 74 +++-- ...Test.inc => FunctionCommentUnitTest.1.inc} | 0 ...ed => FunctionCommentUnitTest.1.inc.fixed} | 0 .../Commenting/FunctionCommentUnitTest.php | 122 ++++---- ...st.inc => ValidFunctionNameUnitTest.1.inc} | 0 .../ValidFunctionNameUnitTest.php | 214 ++++++------- ....inc => CamelCapsMethodNameUnitTest.1.inc} | 0 .../Methods/CamelCapsMethodNameUnitTest.php | 34 ++- ...st.inc => MethodDeclarationUnitTest.1.inc} | 0 ... => MethodDeclarationUnitTest.1.inc.fixed} | 0 .../Methods/MethodDeclarationUnitTest.php | 70 +++-- ...nitTest.inc => ClassCommentUnitTest.1.inc} | 0 .../Tests/Commenting/ClassCommentUnitTest.php | 50 ++-- ...Test.inc => FunctionCommentUnitTest.1.inc} | 0 ...ed => FunctionCommentUnitTest.1.inc.fixed} | 0 .../Commenting/FunctionCommentUnitTest.php | 281 +++++++++--------- ...tTest.inc => GlobalFunctionUnitTest.1.inc} | 0 .../Functions/GlobalFunctionUnitTest.php | 12 +- ...st.inc => ValidFunctionNameUnitTest.1.inc} | 0 .../ValidFunctionNameUnitTest.php | 34 ++- ...UnitTest.inc => MethodScopeUnitTest.1.inc} | 0 .../Squiz/Tests/Scope/MethodScopeUnitTest.php | 22 +- 23 files changed, 508 insertions(+), 405 deletions(-) rename src/Standards/PEAR/Tests/Commenting/{ClassCommentUnitTest.inc => ClassCommentUnitTest.1.inc} (100%) rename src/Standards/PEAR/Tests/Commenting/{FunctionCommentUnitTest.inc => FunctionCommentUnitTest.1.inc} (100%) rename src/Standards/PEAR/Tests/Commenting/{FunctionCommentUnitTest.inc.fixed => FunctionCommentUnitTest.1.inc.fixed} (100%) rename src/Standards/PEAR/Tests/NamingConventions/{ValidFunctionNameUnitTest.inc => ValidFunctionNameUnitTest.1.inc} (100%) rename src/Standards/PSR1/Tests/Methods/{CamelCapsMethodNameUnitTest.inc => CamelCapsMethodNameUnitTest.1.inc} (100%) rename src/Standards/PSR2/Tests/Methods/{MethodDeclarationUnitTest.inc => MethodDeclarationUnitTest.1.inc} (100%) rename src/Standards/PSR2/Tests/Methods/{MethodDeclarationUnitTest.inc.fixed => MethodDeclarationUnitTest.1.inc.fixed} (100%) rename src/Standards/Squiz/Tests/Commenting/{ClassCommentUnitTest.inc => ClassCommentUnitTest.1.inc} (100%) rename src/Standards/Squiz/Tests/Commenting/{FunctionCommentUnitTest.inc => FunctionCommentUnitTest.1.inc} (100%) rename src/Standards/Squiz/Tests/Commenting/{FunctionCommentUnitTest.inc.fixed => FunctionCommentUnitTest.1.inc.fixed} (100%) rename src/Standards/Squiz/Tests/Functions/{GlobalFunctionUnitTest.inc => GlobalFunctionUnitTest.1.inc} (100%) rename src/Standards/Squiz/Tests/NamingConventions/{ValidFunctionNameUnitTest.inc => ValidFunctionNameUnitTest.1.inc} (100%) rename src/Standards/Squiz/Tests/Scope/{MethodScopeUnitTest.inc => MethodScopeUnitTest.1.inc} (100%) diff --git a/src/Standards/PEAR/Tests/Commenting/ClassCommentUnitTest.inc b/src/Standards/PEAR/Tests/Commenting/ClassCommentUnitTest.1.inc similarity index 100% rename from src/Standards/PEAR/Tests/Commenting/ClassCommentUnitTest.inc rename to src/Standards/PEAR/Tests/Commenting/ClassCommentUnitTest.1.inc diff --git a/src/Standards/PEAR/Tests/Commenting/ClassCommentUnitTest.php b/src/Standards/PEAR/Tests/Commenting/ClassCommentUnitTest.php index e64a7e9890..f74354402f 100644 --- a/src/Standards/PEAR/Tests/Commenting/ClassCommentUnitTest.php +++ b/src/Standards/PEAR/Tests/Commenting/ClassCommentUnitTest.php @@ -26,33 +26,41 @@ final class ClassCommentUnitTest extends AbstractSniffTestCase * 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 test file being tested. + * * @return array */ - public function getErrorList() + public function getErrorList($testFile='') { - return [ - 4 => 1, - 15 => 1, - 51 => 1, - 63 => 1, - 65 => 2, - 66 => 1, - 68 => 1, - 70 => 1, - 71 => 1, - 72 => 1, - 74 => 2, - 75 => 1, - 76 => 1, - 77 => 1, - 85 => 1, - 96 => 5, - 106 => 5, - 116 => 5, - 126 => 5, - 161 => 1, - 163 => 1, - ]; + switch ($testFile) { + case 'ClassCommentUnitTest.1.inc': + return [ + 4 => 1, + 15 => 1, + 51 => 1, + 63 => 1, + 65 => 2, + 66 => 1, + 68 => 1, + 70 => 1, + 71 => 1, + 72 => 1, + 74 => 2, + 75 => 1, + 76 => 1, + 77 => 1, + 85 => 1, + 96 => 5, + 106 => 5, + 116 => 5, + 126 => 5, + 161 => 1, + 163 => 1, + ]; + + default: + return []; + }//end switch }//end getErrorList() @@ -63,14 +71,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 test file being tested. + * * @return array */ - public function getWarningList() + public function getWarningList($testFile='') { - return [ - 71 => 1, - 73 => 1, - ]; + switch ($testFile) { + case 'ClassCommentUnitTest.1.inc': + return [ + 71 => 1, + 73 => 1, + ]; + + default: + return []; + }//end switch }//end getWarningList() diff --git a/src/Standards/PEAR/Tests/Commenting/FunctionCommentUnitTest.inc b/src/Standards/PEAR/Tests/Commenting/FunctionCommentUnitTest.1.inc similarity index 100% rename from src/Standards/PEAR/Tests/Commenting/FunctionCommentUnitTest.inc rename to src/Standards/PEAR/Tests/Commenting/FunctionCommentUnitTest.1.inc diff --git a/src/Standards/PEAR/Tests/Commenting/FunctionCommentUnitTest.inc.fixed b/src/Standards/PEAR/Tests/Commenting/FunctionCommentUnitTest.1.inc.fixed similarity index 100% rename from src/Standards/PEAR/Tests/Commenting/FunctionCommentUnitTest.inc.fixed rename to src/Standards/PEAR/Tests/Commenting/FunctionCommentUnitTest.1.inc.fixed diff --git a/src/Standards/PEAR/Tests/Commenting/FunctionCommentUnitTest.php b/src/Standards/PEAR/Tests/Commenting/FunctionCommentUnitTest.php index 9202113943..871fab4357 100644 --- a/src/Standards/PEAR/Tests/Commenting/FunctionCommentUnitTest.php +++ b/src/Standards/PEAR/Tests/Commenting/FunctionCommentUnitTest.php @@ -26,66 +26,74 @@ final class FunctionCommentUnitTest extends AbstractSniffTestCase * 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 test file being tested. + * * @return array */ - public function getErrorList() + public function getErrorList($testFile='') { - return [ - 5 => 1, - 10 => 1, - 12 => 1, - 13 => 1, - 14 => 1, - 15 => 1, - 28 => 1, - 76 => 1, - 87 => 1, - 103 => 1, - 109 => 1, - 112 => 1, - 122 => 1, - 123 => 2, - 124 => 2, - 125 => 1, - 126 => 1, - 137 => 1, - 138 => 1, - 139 => 1, - 152 => 1, - 155 => 1, - 165 => 1, - 172 => 1, - 183 => 1, - 190 => 2, - 206 => 1, - 234 => 1, - 272 => 1, - 313 => 1, - 317 => 1, - 327 => 1, - 329 => 1, - 332 => 1, - 344 => 1, - 343 => 1, - 345 => 1, - 346 => 1, - 360 => 1, - 361 => 1, - 363 => 1, - 364 => 1, - 406 => 1, - 417 => 1, - 456 => 1, - 466 => 1, - 474 => 1, - 476 => 1, - 486 => 1, - 502 => 1, - 521 => 1, - 523 => 1, - 533 => 1, - 545 => 1, - ]; + switch ($testFile) { + case 'FunctionCommentUnitTest.1.inc': + return [ + 5 => 1, + 10 => 1, + 12 => 1, + 13 => 1, + 14 => 1, + 15 => 1, + 28 => 1, + 76 => 1, + 87 => 1, + 103 => 1, + 109 => 1, + 112 => 1, + 122 => 1, + 123 => 2, + 124 => 2, + 125 => 1, + 126 => 1, + 137 => 1, + 138 => 1, + 139 => 1, + 152 => 1, + 155 => 1, + 165 => 1, + 172 => 1, + 183 => 1, + 190 => 2, + 206 => 1, + 234 => 1, + 272 => 1, + 313 => 1, + 317 => 1, + 327 => 1, + 329 => 1, + 332 => 1, + 344 => 1, + 343 => 1, + 345 => 1, + 346 => 1, + 360 => 1, + 361 => 1, + 363 => 1, + 364 => 1, + 406 => 1, + 417 => 1, + 456 => 1, + 466 => 1, + 474 => 1, + 476 => 1, + 486 => 1, + 502 => 1, + 521 => 1, + 523 => 1, + 533 => 1, + 545 => 1, + ]; + + default: + return []; + }//end switch }//end getErrorList() diff --git a/src/Standards/PEAR/Tests/NamingConventions/ValidFunctionNameUnitTest.inc b/src/Standards/PEAR/Tests/NamingConventions/ValidFunctionNameUnitTest.1.inc similarity index 100% rename from src/Standards/PEAR/Tests/NamingConventions/ValidFunctionNameUnitTest.inc rename to src/Standards/PEAR/Tests/NamingConventions/ValidFunctionNameUnitTest.1.inc diff --git a/src/Standards/PEAR/Tests/NamingConventions/ValidFunctionNameUnitTest.php b/src/Standards/PEAR/Tests/NamingConventions/ValidFunctionNameUnitTest.php index a2157709d5..0b6938e413 100644 --- a/src/Standards/PEAR/Tests/NamingConventions/ValidFunctionNameUnitTest.php +++ b/src/Standards/PEAR/Tests/NamingConventions/ValidFunctionNameUnitTest.php @@ -26,112 +26,120 @@ final class ValidFunctionNameUnitTest extends AbstractSniffTestCase * 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 test file being tested. + * * @return array */ - public function getErrorList() + public function getErrorList($testFile='') { - return [ - 11 => 1, - 12 => 1, - 13 => 1, - 14 => 1, - 15 => 1, - 16 => 1, - 17 => 2, - 18 => 2, - 19 => 2, - 20 => 2, - 24 => 1, - 25 => 1, - 26 => 1, - 27 => 1, - 28 => 1, - 29 => 1, - 30 => 2, - 31 => 2, - 32 => 2, - 33 => 2, - 35 => 1, - 36 => 1, - 37 => 2, - 38 => 2, - 39 => 2, - 40 => 2, - 43 => 1, - 44 => 1, - 45 => 1, - 46 => 1, - 50 => 1, - 51 => 1, - 52 => 1, - 53 => 1, - 56 => 1, - 57 => 1, - 58 => 1, - 59 => 1, - 67 => 1, - 68 => 1, - 69 => 1, - 70 => 1, - 71 => 1, - 72 => 1, - 73 => 2, - 74 => 2, - 75 => 2, - 76 => 2, - 80 => 1, - 81 => 1, - 82 => 1, - 83 => 1, - 86 => 1, - 87 => 1, - 88 => 1, - 89 => 1, - 95 => 1, - 96 => 1, - 97 => 1, - 98 => 1, - 99 => 1, - 100 => 1, - 101 => 2, - 102 => 2, - 103 => 2, - 104 => 2, - 123 => 1, - 125 => 1, - 126 => 2, - 129 => 1, - 130 => 1, - 131 => 1, - 132 => 1, - 133 => 1, - 134 => 1, - 135 => 1, - 136 => 1, - 137 => 1, - 138 => 1, - 139 => 1, - 140 => 3, - 141 => 1, - 143 => 1, - 144 => 1, - 145 => 3, - 147 => 2, - 148 => 1, - 149 => 1, - 181 => 1, - 201 => 1, - 203 => 1, - 204 => 2, - 207 => 2, - 212 => 1, - 213 => 1, - 214 => 1, - 235 => 1, - 236 => 2, - 239 => 1, - 242 => 1, - ]; + switch ($testFile) { + case 'ValidFunctionNameUnitTest.1.inc': + return [ + 11 => 1, + 12 => 1, + 13 => 1, + 14 => 1, + 15 => 1, + 16 => 1, + 17 => 2, + 18 => 2, + 19 => 2, + 20 => 2, + 24 => 1, + 25 => 1, + 26 => 1, + 27 => 1, + 28 => 1, + 29 => 1, + 30 => 2, + 31 => 2, + 32 => 2, + 33 => 2, + 35 => 1, + 36 => 1, + 37 => 2, + 38 => 2, + 39 => 2, + 40 => 2, + 43 => 1, + 44 => 1, + 45 => 1, + 46 => 1, + 50 => 1, + 51 => 1, + 52 => 1, + 53 => 1, + 56 => 1, + 57 => 1, + 58 => 1, + 59 => 1, + 67 => 1, + 68 => 1, + 69 => 1, + 70 => 1, + 71 => 1, + 72 => 1, + 73 => 2, + 74 => 2, + 75 => 2, + 76 => 2, + 80 => 1, + 81 => 1, + 82 => 1, + 83 => 1, + 86 => 1, + 87 => 1, + 88 => 1, + 89 => 1, + 95 => 1, + 96 => 1, + 97 => 1, + 98 => 1, + 99 => 1, + 100 => 1, + 101 => 2, + 102 => 2, + 103 => 2, + 104 => 2, + 123 => 1, + 125 => 1, + 126 => 2, + 129 => 1, + 130 => 1, + 131 => 1, + 132 => 1, + 133 => 1, + 134 => 1, + 135 => 1, + 136 => 1, + 137 => 1, + 138 => 1, + 139 => 1, + 140 => 3, + 141 => 1, + 143 => 1, + 144 => 1, + 145 => 3, + 147 => 2, + 148 => 1, + 149 => 1, + 181 => 1, + 201 => 1, + 203 => 1, + 204 => 2, + 207 => 2, + 212 => 1, + 213 => 1, + 214 => 1, + 235 => 1, + 236 => 2, + 239 => 1, + 242 => 1, + ]; + + default: + return []; + }//end switch }//end getErrorList() diff --git a/src/Standards/PSR1/Tests/Methods/CamelCapsMethodNameUnitTest.inc b/src/Standards/PSR1/Tests/Methods/CamelCapsMethodNameUnitTest.1.inc similarity index 100% rename from src/Standards/PSR1/Tests/Methods/CamelCapsMethodNameUnitTest.inc rename to src/Standards/PSR1/Tests/Methods/CamelCapsMethodNameUnitTest.1.inc diff --git a/src/Standards/PSR1/Tests/Methods/CamelCapsMethodNameUnitTest.php b/src/Standards/PSR1/Tests/Methods/CamelCapsMethodNameUnitTest.php index c50ff10f5c..3337b95c27 100644 --- a/src/Standards/PSR1/Tests/Methods/CamelCapsMethodNameUnitTest.php +++ b/src/Standards/PSR1/Tests/Methods/CamelCapsMethodNameUnitTest.php @@ -26,22 +26,30 @@ final class CamelCapsMethodNameUnitTest extends AbstractSniffTestCase * 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 test file being tested. + * * @return array */ - public function getErrorList() + public function getErrorList($testFile='') { - return [ - 6 => 1, - 7 => 1, - 11 => 1, - 12 => 1, - 13 => 1, - 17 => 1, - 21 => 1, - 25 => 1, - 26 => 1, - 77 => 1, - ]; + switch ($testFile) { + case 'CamelCapsMethodNameUnitTest.1.inc': + return [ + 6 => 1, + 7 => 1, + 11 => 1, + 12 => 1, + 13 => 1, + 17 => 1, + 21 => 1, + 25 => 1, + 26 => 1, + 77 => 1, + ]; + + default: + return []; + }//end switch }//end getErrorList() diff --git a/src/Standards/PSR2/Tests/Methods/MethodDeclarationUnitTest.inc b/src/Standards/PSR2/Tests/Methods/MethodDeclarationUnitTest.1.inc similarity index 100% rename from src/Standards/PSR2/Tests/Methods/MethodDeclarationUnitTest.inc rename to src/Standards/PSR2/Tests/Methods/MethodDeclarationUnitTest.1.inc diff --git a/src/Standards/PSR2/Tests/Methods/MethodDeclarationUnitTest.inc.fixed b/src/Standards/PSR2/Tests/Methods/MethodDeclarationUnitTest.1.inc.fixed similarity index 100% rename from src/Standards/PSR2/Tests/Methods/MethodDeclarationUnitTest.inc.fixed rename to src/Standards/PSR2/Tests/Methods/MethodDeclarationUnitTest.1.inc.fixed diff --git a/src/Standards/PSR2/Tests/Methods/MethodDeclarationUnitTest.php b/src/Standards/PSR2/Tests/Methods/MethodDeclarationUnitTest.php index 6841499546..8e8eef3ef3 100644 --- a/src/Standards/PSR2/Tests/Methods/MethodDeclarationUnitTest.php +++ b/src/Standards/PSR2/Tests/Methods/MethodDeclarationUnitTest.php @@ -26,27 +26,35 @@ final class MethodDeclarationUnitTest extends AbstractSniffTestCase * 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 test file being tested. + * * @return array */ - public function getErrorList() + public function getErrorList($testFile='') { - return [ - 9 => 1, - 11 => 1, - 13 => 1, - 15 => 3, - 24 => 1, - 34 => 1, - 36 => 1, - 38 => 1, - 40 => 3, - 50 => 1, - 52 => 1, - 54 => 1, - 56 => 3, - 63 => 2, - 73 => 1, - ]; + switch ($testFile) { + case 'MethodDeclarationUnitTest.1.inc': + return [ + 9 => 1, + 11 => 1, + 13 => 1, + 15 => 3, + 24 => 1, + 34 => 1, + 36 => 1, + 38 => 1, + 40 => 3, + 50 => 1, + 52 => 1, + 54 => 1, + 56 => 3, + 63 => 2, + 73 => 1, + ]; + + default: + return []; + }//end switch }//end getErrorList() @@ -57,18 +65,26 @@ 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 test file being tested. + * * @return array */ - public function getWarningList() + public function getWarningList($testFile='') { - return [ - 5 => 1, - 21 => 1, - 30 => 1, - 46 => 1, - 63 => 1, - 70 => 1, - ]; + switch ($testFile) { + case 'MethodDeclarationUnitTest.1.inc': + return [ + 5 => 1, + 21 => 1, + 30 => 1, + 46 => 1, + 63 => 1, + 70 => 1, + ]; + + default: + return []; + }//end switch }//end getWarningList() diff --git a/src/Standards/Squiz/Tests/Commenting/ClassCommentUnitTest.inc b/src/Standards/Squiz/Tests/Commenting/ClassCommentUnitTest.1.inc similarity index 100% rename from src/Standards/Squiz/Tests/Commenting/ClassCommentUnitTest.inc rename to src/Standards/Squiz/Tests/Commenting/ClassCommentUnitTest.1.inc diff --git a/src/Standards/Squiz/Tests/Commenting/ClassCommentUnitTest.php b/src/Standards/Squiz/Tests/Commenting/ClassCommentUnitTest.php index 457b91d826..4a770a6ae6 100644 --- a/src/Standards/Squiz/Tests/Commenting/ClassCommentUnitTest.php +++ b/src/Standards/Squiz/Tests/Commenting/ClassCommentUnitTest.php @@ -26,18 +26,26 @@ final class ClassCommentUnitTest extends AbstractSniffTestCase * 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 test file being tested. + * * @return array */ - public function getErrorList() + public function getErrorList($testFile='') { - return [ - 2 => 1, - 15 => 1, - 31 => 1, - 54 => 1, - 143 => 1, - 145 => 1, - ]; + switch ($testFile) { + case 'ClassCommentUnitTest.1.inc': + return [ + 2 => 1, + 15 => 1, + 31 => 1, + 54 => 1, + 143 => 1, + 145 => 1, + ]; + + default: + return []; + }//end switch }//end getErrorList() @@ -48,17 +56,25 @@ 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 test file being tested. + * * @return array */ - public function getWarningList() + public function getWarningList($testFile='') { - return [ - 29 => 1, - 30 => 1, - 50 => 1, - 66 => 1, - 67 => 1, - ]; + switch ($testFile) { + case 'ClassCommentUnitTest.1.inc': + return [ + 29 => 1, + 30 => 1, + 50 => 1, + 66 => 1, + 67 => 1, + ]; + + default: + return []; + }//end switch }//end getWarningList() diff --git a/src/Standards/Squiz/Tests/Commenting/FunctionCommentUnitTest.inc b/src/Standards/Squiz/Tests/Commenting/FunctionCommentUnitTest.1.inc similarity index 100% rename from src/Standards/Squiz/Tests/Commenting/FunctionCommentUnitTest.inc rename to src/Standards/Squiz/Tests/Commenting/FunctionCommentUnitTest.1.inc diff --git a/src/Standards/Squiz/Tests/Commenting/FunctionCommentUnitTest.inc.fixed b/src/Standards/Squiz/Tests/Commenting/FunctionCommentUnitTest.1.inc.fixed similarity index 100% rename from src/Standards/Squiz/Tests/Commenting/FunctionCommentUnitTest.inc.fixed rename to src/Standards/Squiz/Tests/Commenting/FunctionCommentUnitTest.1.inc.fixed diff --git a/src/Standards/Squiz/Tests/Commenting/FunctionCommentUnitTest.php b/src/Standards/Squiz/Tests/Commenting/FunctionCommentUnitTest.php index 580981eae7..a28840c543 100644 --- a/src/Standards/Squiz/Tests/Commenting/FunctionCommentUnitTest.php +++ b/src/Standards/Squiz/Tests/Commenting/FunctionCommentUnitTest.php @@ -26,148 +26,155 @@ final class FunctionCommentUnitTest extends AbstractSniffTestCase * 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 test file being tested. + * * @return array */ - public function getErrorList() + public function getErrorList($testFile='') { - $errors = [ - 5 => 1, - 10 => 3, - 12 => 2, - 13 => 2, - 14 => 1, - 15 => 1, - 17 => 3, - 28 => 1, - 43 => 1, - 76 => 1, - 87 => 1, - 103 => 1, - 109 => 1, - 112 => 1, - 122 => 1, - 123 => 3, - 124 => 2, - 125 => 1, - 126 => 1, - 128 => 1, - 137 => 4, - 138 => 4, - 139 => 4, - 143 => 3, - 155 => 1, - 159 => 1, - 161 => 2, - 166 => 1, - 173 => 1, - 183 => 1, - 190 => 2, - 193 => 2, - 196 => 1, - 199 => 2, - 201 => 1, - 210 => 1, - 211 => 1, - 222 => 1, - 223 => 1, - 224 => 1, - 225 => 1, - 226 => 1, - 227 => 1, - 230 => 2, - 232 => 7, - 246 => 1, - 248 => 4, - 261 => 1, - 263 => 1, - 276 => 1, - 277 => 1, - 278 => 1, - 279 => 1, - 280 => 1, - 281 => 1, - 284 => 1, - 286 => 7, - 294 => 1, - 302 => 1, - 312 => 1, - 358 => 1, - 359 => 2, - 363 => 3, - 372 => 1, - 373 => 1, - 377 => 1, - 387 => 1, - 407 => 1, - 441 => 1, - 500 => 1, - 526 => 1, - 548 => 1, - 575 => 2, - 627 => 2, - 641 => 1, - 669 => 1, - 688 => 1, - 744 => 1, - 748 => 1, - 767 => 1, - 789 => 1, - 792 => 1, - 794 => 1, - 797 => 1, - 828 => 1, - 840 => 1, - 852 => 1, - 864 => 1, - 886 => 1, - 888 => 1, - 890 => 1, - 978 => 1, - 997 => 1, - 1002 => 1, - 1004 => 2, - 1006 => 1, - 1029 => 1, - 1053 => 1, - 1058 => 2, - 1069 => 1, - 1070 => 1, - 1071 => 1, - 1075 => 6, - 1080 => 2, - 1083 => 1, - 1084 => 1, - 1085 => 1, - 1089 => 3, - 1093 => 4, - 1100 => 1, - 1101 => 1, - 1102 => 1, - 1103 => 1, - 1107 => 8, - 1123 => 1, - 1124 => 1, - 1125 => 1, - 1129 => 3, - 1138 => 1, - 1139 => 1, - 1144 => 1, - 1145 => 1, - 1151 => 1, - 1154 => 1, - 1160 => 1, - ]; + switch ($testFile) { + case 'FunctionCommentUnitTest.1.inc': + $errors = [ + 5 => 1, + 10 => 3, + 12 => 2, + 13 => 2, + 14 => 1, + 15 => 1, + 17 => 3, + 28 => 1, + 43 => 1, + 76 => 1, + 87 => 1, + 103 => 1, + 109 => 1, + 112 => 1, + 122 => 1, + 123 => 3, + 124 => 2, + 125 => 1, + 126 => 1, + 128 => 1, + 137 => 4, + 138 => 4, + 139 => 4, + 143 => 3, + 155 => 1, + 159 => 1, + 161 => 2, + 166 => 1, + 173 => 1, + 183 => 1, + 190 => 2, + 193 => 2, + 196 => 1, + 199 => 2, + 201 => 1, + 210 => 1, + 211 => 1, + 222 => 1, + 223 => 1, + 224 => 1, + 225 => 1, + 226 => 1, + 227 => 1, + 230 => 2, + 232 => 7, + 246 => 1, + 248 => 4, + 261 => 1, + 263 => 1, + 276 => 1, + 277 => 1, + 278 => 1, + 279 => 1, + 280 => 1, + 281 => 1, + 284 => 1, + 286 => 7, + 294 => 1, + 302 => 1, + 312 => 1, + 358 => 1, + 359 => 2, + 363 => 3, + 372 => 1, + 373 => 1, + 377 => 1, + 387 => 1, + 407 => 1, + 441 => 1, + 500 => 1, + 526 => 1, + 548 => 1, + 575 => 2, + 627 => 2, + 641 => 1, + 669 => 1, + 688 => 1, + 744 => 1, + 748 => 1, + 767 => 1, + 789 => 1, + 792 => 1, + 794 => 1, + 797 => 1, + 828 => 1, + 840 => 1, + 852 => 1, + 864 => 1, + 886 => 1, + 888 => 1, + 890 => 1, + 978 => 1, + 997 => 1, + 1002 => 1, + 1004 => 2, + 1006 => 1, + 1029 => 1, + 1053 => 1, + 1058 => 2, + 1069 => 1, + 1070 => 1, + 1071 => 1, + 1075 => 6, + 1080 => 2, + 1083 => 1, + 1084 => 1, + 1085 => 1, + 1089 => 3, + 1093 => 4, + 1100 => 1, + 1101 => 1, + 1102 => 1, + 1103 => 1, + 1107 => 8, + 1123 => 1, + 1124 => 1, + 1125 => 1, + 1129 => 3, + 1138 => 1, + 1139 => 1, + 1144 => 1, + 1145 => 1, + 1151 => 1, + 1154 => 1, + 1160 => 1, + ]; - // Mixed type hints only work from PHP 8.0 onwards. - if (PHP_VERSION_ID >= 80000) { - $errors[265] = 1; - $errors[459] = 1; - $errors[893] = 3; - } else { - $errors[1023] = 1; - } + // Mixed type hints only work from PHP 8.0 onwards. + if (PHP_VERSION_ID >= 80000) { + $errors[265] = 1; + $errors[459] = 1; + $errors[893] = 3; + } else { + $errors[1023] = 1; + } + return $errors; - return $errors; + default: + return []; + }//end switch }//end getErrorList() diff --git a/src/Standards/Squiz/Tests/Functions/GlobalFunctionUnitTest.inc b/src/Standards/Squiz/Tests/Functions/GlobalFunctionUnitTest.1.inc similarity index 100% rename from src/Standards/Squiz/Tests/Functions/GlobalFunctionUnitTest.inc rename to src/Standards/Squiz/Tests/Functions/GlobalFunctionUnitTest.1.inc diff --git a/src/Standards/Squiz/Tests/Functions/GlobalFunctionUnitTest.php b/src/Standards/Squiz/Tests/Functions/GlobalFunctionUnitTest.php index b801b13695..b4997e0548 100644 --- a/src/Standards/Squiz/Tests/Functions/GlobalFunctionUnitTest.php +++ b/src/Standards/Squiz/Tests/Functions/GlobalFunctionUnitTest.php @@ -41,11 +41,19 @@ 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 test file being tested. + * * @return array */ - public function getWarningList() + public function getWarningList($testFile='') { - return [2 => 1]; + switch ($testFile) { + case 'GlobalFunctionUnitTest.1.inc': + return [2 => 1]; + + default: + return []; + }//end switch }//end getWarningList() diff --git a/src/Standards/Squiz/Tests/NamingConventions/ValidFunctionNameUnitTest.inc b/src/Standards/Squiz/Tests/NamingConventions/ValidFunctionNameUnitTest.1.inc similarity index 100% rename from src/Standards/Squiz/Tests/NamingConventions/ValidFunctionNameUnitTest.inc rename to src/Standards/Squiz/Tests/NamingConventions/ValidFunctionNameUnitTest.1.inc diff --git a/src/Standards/Squiz/Tests/NamingConventions/ValidFunctionNameUnitTest.php b/src/Standards/Squiz/Tests/NamingConventions/ValidFunctionNameUnitTest.php index 78206ab465..09d669e88f 100644 --- a/src/Standards/Squiz/Tests/NamingConventions/ValidFunctionNameUnitTest.php +++ b/src/Standards/Squiz/Tests/NamingConventions/ValidFunctionNameUnitTest.php @@ -26,22 +26,30 @@ final class ValidFunctionNameUnitTest extends AbstractSniffTestCase * 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 test file being tested. + * * @return array */ - public function getErrorList() + public function getErrorList($testFile='') { - return [ - 4 => 1, - 5 => 1, - 6 => 1, - 7 => 1, - 8 => 1, - 9 => 1, - 11 => 1, - 12 => 1, - 13 => 1, - 14 => 2, - ]; + switch ($testFile) { + case 'ValidFunctionNameUnitTest.1.inc': + return [ + 4 => 1, + 5 => 1, + 6 => 1, + 7 => 1, + 8 => 1, + 9 => 1, + 11 => 1, + 12 => 1, + 13 => 1, + 14 => 2, + ]; + + default: + return []; + }//end switch }//end getErrorList() diff --git a/src/Standards/Squiz/Tests/Scope/MethodScopeUnitTest.inc b/src/Standards/Squiz/Tests/Scope/MethodScopeUnitTest.1.inc similarity index 100% rename from src/Standards/Squiz/Tests/Scope/MethodScopeUnitTest.inc rename to src/Standards/Squiz/Tests/Scope/MethodScopeUnitTest.1.inc diff --git a/src/Standards/Squiz/Tests/Scope/MethodScopeUnitTest.php b/src/Standards/Squiz/Tests/Scope/MethodScopeUnitTest.php index a05ddb2ec0..b7ec7b7be1 100644 --- a/src/Standards/Squiz/Tests/Scope/MethodScopeUnitTest.php +++ b/src/Standards/Squiz/Tests/Scope/MethodScopeUnitTest.php @@ -26,16 +26,24 @@ final class MethodScopeUnitTest extends AbstractSniffTestCase * 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 test file being tested. + * * @return array */ - public function getErrorList() + public function getErrorList($testFile='') { - return [ - 6 => 1, - 30 => 1, - 39 => 1, - 46 => 1, - ]; + switch ($testFile) { + case 'MethodScopeUnitTest.1.inc': + return [ + 6 => 1, + 30 => 1, + 39 => 1, + 46 => 1, + ]; + + default: + return []; + }//end switch }//end getErrorList() From 40dd4b571528d629a4075f9faf2e1fc3c8b40ae1 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Sun, 16 Mar 2025 10:38:16 +0100 Subject: [PATCH 2/3] Various sniffs: add extra tests with live coding/parse errors Includes fixing up some inline comments for sniffs which don't search for closures, but could encounter live coding. Includes one minor bug fix/defensive coding fix in the `Squiz.Commenting.ClosingDeclarationComment` sniff. --- .../UselessOverridingMethodUnitTest.7.inc | 9 +++++++++ .../ConstructorNameUnitTest.3.inc | 9 +++++++++ .../ConstructorNameUnitTest.4.inc | 11 +++++++++++ .../ConstructorNameUnitTest.5.inc | 11 +++++++++++ .../NamingConventions/ValidFunctionNameSniff.php | 4 ++-- .../Tests/Commenting/ClassCommentUnitTest.2.inc | 7 +++++++ .../PEAR/Tests/Commenting/ClassCommentUnitTest.php | 5 +++++ .../Tests/Commenting/FunctionCommentUnitTest.2.inc | 7 +++++++ .../Tests/Commenting/FunctionCommentUnitTest.3.inc | 14 ++++++++++++++ .../Tests/Commenting/FunctionCommentUnitTest.php | 10 ++++++++++ .../ValidFunctionNameUnitTest.2.inc | 5 +++++ .../ValidFunctionNameUnitTest.3.inc | 9 +++++++++ .../Sniffs/Methods/CamelCapsMethodNameSniff.php | 2 +- .../Methods/CamelCapsMethodNameUnitTest.2.inc | 9 +++++++++ .../PSR2/Sniffs/Methods/MethodDeclarationSniff.php | 2 +- .../Tests/Methods/MethodDeclarationUnitTest.2.inc | 9 +++++++++ .../Commenting/ClosingDeclarationCommentSniff.php | 5 +++++ .../Squiz/Sniffs/Scope/MethodScopeSniff.php | 2 +- .../Tests/Commenting/ClassCommentUnitTest.2.inc | 7 +++++++ .../Tests/Commenting/ClassCommentUnitTest.php | 5 +++++ .../ClosingDeclarationCommentUnitTest.6.inc | 7 +++++++ .../Tests/Commenting/FunctionCommentUnitTest.2.inc | 12 ++++++++++++ .../Tests/Commenting/FunctionCommentUnitTest.php | 5 +++++ .../Tests/Functions/GlobalFunctionUnitTest.2.inc | 5 +++++ .../ValidFunctionNameUnitTest.2.inc | 5 +++++ .../Squiz/Tests/Scope/MethodScopeUnitTest.2.inc | 9 +++++++++ 26 files changed, 180 insertions(+), 5 deletions(-) create mode 100644 src/Standards/Generic/Tests/CodeAnalysis/UselessOverridingMethodUnitTest.7.inc create mode 100644 src/Standards/Generic/Tests/NamingConventions/ConstructorNameUnitTest.3.inc create mode 100644 src/Standards/Generic/Tests/NamingConventions/ConstructorNameUnitTest.4.inc create mode 100644 src/Standards/Generic/Tests/NamingConventions/ConstructorNameUnitTest.5.inc create mode 100644 src/Standards/PEAR/Tests/Commenting/ClassCommentUnitTest.2.inc create mode 100644 src/Standards/PEAR/Tests/Commenting/FunctionCommentUnitTest.2.inc create mode 100644 src/Standards/PEAR/Tests/Commenting/FunctionCommentUnitTest.3.inc create mode 100644 src/Standards/PEAR/Tests/NamingConventions/ValidFunctionNameUnitTest.2.inc create mode 100644 src/Standards/PEAR/Tests/NamingConventions/ValidFunctionNameUnitTest.3.inc create mode 100644 src/Standards/PSR1/Tests/Methods/CamelCapsMethodNameUnitTest.2.inc create mode 100644 src/Standards/PSR2/Tests/Methods/MethodDeclarationUnitTest.2.inc create mode 100644 src/Standards/Squiz/Tests/Commenting/ClassCommentUnitTest.2.inc create mode 100644 src/Standards/Squiz/Tests/Commenting/ClosingDeclarationCommentUnitTest.6.inc create mode 100644 src/Standards/Squiz/Tests/Commenting/FunctionCommentUnitTest.2.inc create mode 100644 src/Standards/Squiz/Tests/Functions/GlobalFunctionUnitTest.2.inc create mode 100644 src/Standards/Squiz/Tests/NamingConventions/ValidFunctionNameUnitTest.2.inc create mode 100644 src/Standards/Squiz/Tests/Scope/MethodScopeUnitTest.2.inc diff --git a/src/Standards/Generic/Tests/CodeAnalysis/UselessOverridingMethodUnitTest.7.inc b/src/Standards/Generic/Tests/CodeAnalysis/UselessOverridingMethodUnitTest.7.inc new file mode 100644 index 0000000000..8a0dd4827f --- /dev/null +++ b/src/Standards/Generic/Tests/CodeAnalysis/UselessOverridingMethodUnitTest.7.inc @@ -0,0 +1,9 @@ +getDeclarationName($stackPtr); if ($methodName === null) { - // Ignore closures. + // Ignore live coding. return; } @@ -182,7 +182,7 @@ protected function processTokenOutsideScope(File $phpcsFile, $stackPtr) { $functionName = $phpcsFile->getDeclarationName($stackPtr); if ($functionName === null) { - // Ignore closures. + // Ignore live coding. return; } diff --git a/src/Standards/PEAR/Tests/Commenting/ClassCommentUnitTest.2.inc b/src/Standards/PEAR/Tests/Commenting/ClassCommentUnitTest.2.inc new file mode 100644 index 0000000000..dcb5ed507a --- /dev/null +++ b/src/Standards/PEAR/Tests/Commenting/ClassCommentUnitTest.2.inc @@ -0,0 +1,7 @@ + 1, ]; + case 'ClassCommentUnitTest.2.inc': + return [ + 7 => 1, + ]; + default: return []; }//end switch diff --git a/src/Standards/PEAR/Tests/Commenting/FunctionCommentUnitTest.2.inc b/src/Standards/PEAR/Tests/Commenting/FunctionCommentUnitTest.2.inc new file mode 100644 index 0000000000..6fd197e569 --- /dev/null +++ b/src/Standards/PEAR/Tests/Commenting/FunctionCommentUnitTest.2.inc @@ -0,0 +1,7 @@ + 1, ]; + case 'FunctionCommentUnitTest.2.inc': + return [ + 7 => 1, + ]; + + case 'FunctionCommentUnitTest.3.inc': + return [ + 10 => 1, + ]; + default: return []; }//end switch diff --git a/src/Standards/PEAR/Tests/NamingConventions/ValidFunctionNameUnitTest.2.inc b/src/Standards/PEAR/Tests/NamingConventions/ValidFunctionNameUnitTest.2.inc new file mode 100644 index 0000000000..27b95be4fa --- /dev/null +++ b/src/Standards/PEAR/Tests/NamingConventions/ValidFunctionNameUnitTest.2.inc @@ -0,0 +1,5 @@ +getDeclarationName($stackPtr); if ($methodName === null) { - // Ignore closures. + // Ignore live coding. return; } diff --git a/src/Standards/PSR1/Tests/Methods/CamelCapsMethodNameUnitTest.2.inc b/src/Standards/PSR1/Tests/Methods/CamelCapsMethodNameUnitTest.2.inc new file mode 100644 index 0000000000..b39ba26804 --- /dev/null +++ b/src/Standards/PSR1/Tests/Methods/CamelCapsMethodNameUnitTest.2.inc @@ -0,0 +1,9 @@ +getDeclarationName($stackPtr); if ($methodName === null) { - // Ignore closures. + // Ignore live coding. return; } diff --git a/src/Standards/PSR2/Tests/Methods/MethodDeclarationUnitTest.2.inc b/src/Standards/PSR2/Tests/Methods/MethodDeclarationUnitTest.2.inc new file mode 100644 index 0000000000..f662fb4028 --- /dev/null +++ b/src/Standards/PSR2/Tests/Methods/MethodDeclarationUnitTest.2.inc @@ -0,0 +1,9 @@ +getDeclarationName($stackPtr); + if ($decName === null) { + // Parse error or live coding. + return; + } + $comment = '//end '.$decName.'()'; } else if ($tokens[$stackPtr]['code'] === T_CLASS) { $comment = '//end class'; diff --git a/src/Standards/Squiz/Sniffs/Scope/MethodScopeSniff.php b/src/Standards/Squiz/Sniffs/Scope/MethodScopeSniff.php index 39ed5b0dd4..4a1d6249d5 100644 --- a/src/Standards/Squiz/Sniffs/Scope/MethodScopeSniff.php +++ b/src/Standards/Squiz/Sniffs/Scope/MethodScopeSniff.php @@ -50,7 +50,7 @@ protected function processTokenWithinScope(File $phpcsFile, $stackPtr, $currScop $methodName = $phpcsFile->getDeclarationName($stackPtr); if ($methodName === null) { - // Ignore closures. + // Ignore live coding. return; } diff --git a/src/Standards/Squiz/Tests/Commenting/ClassCommentUnitTest.2.inc b/src/Standards/Squiz/Tests/Commenting/ClassCommentUnitTest.2.inc new file mode 100644 index 0000000000..18ab1e7940 --- /dev/null +++ b/src/Standards/Squiz/Tests/Commenting/ClassCommentUnitTest.2.inc @@ -0,0 +1,7 @@ + 1, ]; + case 'ClassCommentUnitTest.2.inc': + return [ + 7 => 1, + ]; + default: return []; }//end switch diff --git a/src/Standards/Squiz/Tests/Commenting/ClosingDeclarationCommentUnitTest.6.inc b/src/Standards/Squiz/Tests/Commenting/ClosingDeclarationCommentUnitTest.6.inc new file mode 100644 index 0000000000..a996c81aa8 --- /dev/null +++ b/src/Standards/Squiz/Tests/Commenting/ClosingDeclarationCommentUnitTest.6.inc @@ -0,0 +1,7 @@ + 1, + ]; + default: return []; }//end switch diff --git a/src/Standards/Squiz/Tests/Functions/GlobalFunctionUnitTest.2.inc b/src/Standards/Squiz/Tests/Functions/GlobalFunctionUnitTest.2.inc new file mode 100644 index 0000000000..27b95be4fa --- /dev/null +++ b/src/Standards/Squiz/Tests/Functions/GlobalFunctionUnitTest.2.inc @@ -0,0 +1,5 @@ + Date: Thu, 18 Apr 2024 07:51:24 +0200 Subject: [PATCH 3/3] File::getDeclarationName(): stop accepting tokens for non-named structures The `File::getDeclarationName()` method - for historic reasons - accepted the `T_CLOSURE` and `T_ANON_CLASS` tokens, even though these structures will never have a name, and returned `null` for those tokens. This commit changes the `File::getDeclarationName()` method to no longer accept those tokens and throw an exception if they are passed to the method instead. As a secondary change, when the name of a valid structure cannot be determined, the method will now no longer return `null`, but will return an empty string. This normalizes the return type of the method to always return a string (or throw an exception). Includes updated unit tests to match. Related to squizlabs/PHP_CodeSniffer 3766 --- All PHPCS native sniffs using the `File::getDeclarationName()` method have been reviewed and where necessary fixed to allow for this change. Also note that in some cases, the sniff already contained sufficient protection and in other case, the fact that the method will no longer return `null`, meant that the sniff code could be simplified. List of sniffs reviewed: - `AbstractScopeSniff` - `Generic.Classes.DuplicateClassName` - `Generic.CodeAnalysis.UnusedFunctionParameter` - `Generic.CodeAnalysis.UselessOverridingMethod` - `Generic.NamingConventions.AbstractClassNamePrefix` - `Generic.NamingConventions.CamelCapsFunctionName` - `Generic.NamingConventions.ConstructorName` - `Generic.NamingConventions.InterfaceNameSuffix` - `Generic.NamingConventions.TraitNameSuffix` - `PEAR.Commenting.ClassComment` - `PEAR.Commenting.FunctionComment` - `PEAR.Functions.FunctionDeclaration` - `PEAR.NamingConventions.ValidFunctionName` - `PSR1.Methods.CamelCapsMethodName` - `PSR2.Methods.MethodDeclaration` - `Squiz.Classes.ClassFileName` - `Squiz.Classes.SelfMemberReference` - `Squiz.Commenting.ClassComment` - `Squiz.Commenting.ClosingDeclarationComment` - `Squiz.Commenting.FunctionComment` - `Squiz.Functions.GlobalFunction` - `Squiz.NamingConventions.ValidFunctionName` - `Squiz.Scope.MethodScope` :point_right: The changes to the PEAR/FunctionDeclaration sniff will be easier to review while ignoring whitespace. --- src/Files/File.php | 13 ++-- .../UnusedFunctionParameterSniff.php | 10 ++- .../AbstractClassNamePrefixSniff.php | 2 +- .../CamelCapsFunctionNameSniff.php | 10 +-- .../ConstructorNameSniff.php | 11 ++- .../InterfaceNameSuffixSniff.php | 2 +- .../TraitNameSuffixSniff.php | 2 +- .../Functions/FunctionDeclarationSniff.php | 72 ++++++++++--------- .../ValidFunctionNameSniff.php | 13 ++-- .../Methods/CamelCapsMethodNameSniff.php | 8 +-- .../Sniffs/Methods/MethodDeclarationSniff.php | 2 +- .../Sniffs/Classes/ClassFileNameSniff.php | 2 +- .../ClosingDeclarationCommentSniff.php | 2 +- .../Sniffs/Functions/GlobalFunctionSniff.php | 2 +- .../ValidFunctionNameSniff.php | 2 +- .../Squiz/Sniffs/Scope/MethodScopeSniff.php | 2 +- .../GetDeclarationNameParseError1Test.php | 8 +-- .../GetDeclarationNameParseError2Test.php | 8 +-- tests/Core/File/GetDeclarationNameTest.php | 38 ++++------ 19 files changed, 95 insertions(+), 114 deletions(-) diff --git a/src/Files/File.php b/src/Files/File.php index dbaaf3b204..96bda88ef2 100644 --- a/src/Files/File.php +++ b/src/Files/File.php @@ -1204,20 +1204,15 @@ public function getFilename() * @param int $stackPtr The position of the declaration token which * declared the class, interface, trait, or function. * - * @return string|null The name of the class, interface, trait, or function; - * or NULL if the function or class is anonymous. + * @return string The name of the class, interface, trait, or function or an empty string + * if the name could not be determined (live coding). * @throws \PHP_CodeSniffer\Exceptions\RuntimeException If the specified token is not of type - * T_FUNCTION, T_CLASS, T_ANON_CLASS, - * T_CLOSURE, T_TRAIT, T_ENUM, or T_INTERFACE. + * T_FUNCTION, T_CLASS, T_TRAIT, T_ENUM, or T_INTERFACE. */ public function getDeclarationName($stackPtr) { $tokenCode = $this->tokens[$stackPtr]['code']; - if ($tokenCode === T_ANON_CLASS || $tokenCode === T_CLOSURE) { - return null; - } - if ($tokenCode !== T_FUNCTION && $tokenCode !== T_CLASS && $tokenCode !== T_INTERFACE @@ -1236,7 +1231,7 @@ public function getDeclarationName($stackPtr) $stopPoint = $this->tokens[$stackPtr]['scope_opener']; } - $content = null; + $content = ''; for ($i = $stackPtr; $i < $stopPoint; $i++) { if ($this->tokens[$i]['code'] === T_STRING) { $content = $this->tokens[$i]['content']; diff --git a/src/Standards/Generic/Sniffs/CodeAnalysis/UnusedFunctionParameterSniff.php b/src/Standards/Generic/Sniffs/CodeAnalysis/UnusedFunctionParameterSniff.php index e076b83dce..e83cb09bf1 100644 --- a/src/Standards/Generic/Sniffs/CodeAnalysis/UnusedFunctionParameterSniff.php +++ b/src/Standards/Generic/Sniffs/CodeAnalysis/UnusedFunctionParameterSniff.php @@ -99,12 +99,10 @@ public function process(File $phpcsFile, $stackPtr) $classPtr = $phpcsFile->getCondition($stackPtr, T_CLASS); if ($classPtr !== false) { // Check for magic methods and ignore these as the method signature cannot be changed. - $methodName = $phpcsFile->getDeclarationName($stackPtr); - if (empty($methodName) === false) { - $methodNameLc = strtolower($methodName); - if (isset($this->magicMethods[$methodNameLc]) === true) { - return; - } + $methodName = $phpcsFile->getDeclarationName($stackPtr); + $methodNameLc = strtolower($methodName); + if (isset($this->magicMethods[$methodNameLc]) === true) { + return; } // Check for extends/implements and adjust the error code when found. diff --git a/src/Standards/Generic/Sniffs/NamingConventions/AbstractClassNamePrefixSniff.php b/src/Standards/Generic/Sniffs/NamingConventions/AbstractClassNamePrefixSniff.php index 44c16390e6..f009d56ef2 100644 --- a/src/Standards/Generic/Sniffs/NamingConventions/AbstractClassNamePrefixSniff.php +++ b/src/Standards/Generic/Sniffs/NamingConventions/AbstractClassNamePrefixSniff.php @@ -44,7 +44,7 @@ public function process(File $phpcsFile, $stackPtr) } $className = $phpcsFile->getDeclarationName($stackPtr); - if ($className === null) { + if ($className === '') { // Live coding or parse error. return; } diff --git a/src/Standards/Generic/Sniffs/NamingConventions/CamelCapsFunctionNameSniff.php b/src/Standards/Generic/Sniffs/NamingConventions/CamelCapsFunctionNameSniff.php index d596f17442..48d93c6d50 100644 --- a/src/Standards/Generic/Sniffs/NamingConventions/CamelCapsFunctionNameSniff.php +++ b/src/Standards/Generic/Sniffs/NamingConventions/CamelCapsFunctionNameSniff.php @@ -112,14 +112,14 @@ protected function processTokenWithinScope(File $phpcsFile, $stackPtr, $currScop } $methodName = $phpcsFile->getDeclarationName($stackPtr); - if ($methodName === null) { + if ($methodName === '') { // Live coding or parse error. Bow out. return; } - $className = $phpcsFile->getDeclarationName($currScope); - if (isset($className) === false) { - $className = '[Anonymous Class]'; + $className = '[Anonymous Class]'; + if ($tokens[$currScope]['code'] !== T_ANON_CLASS) { + $className = $phpcsFile->getDeclarationName($currScope); } $errorData = [$className.'::'.$methodName]; @@ -187,7 +187,7 @@ protected function processTokenWithinScope(File $phpcsFile, $stackPtr, $currScop protected function processTokenOutsideScope(File $phpcsFile, $stackPtr) { $functionName = $phpcsFile->getDeclarationName($stackPtr); - if ($functionName === null) { + if ($functionName === '') { // Live coding or parse error. Bow out. return; } diff --git a/src/Standards/Generic/Sniffs/NamingConventions/ConstructorNameSniff.php b/src/Standards/Generic/Sniffs/NamingConventions/ConstructorNameSniff.php index e87f066b3f..1818ce1af3 100644 --- a/src/Standards/Generic/Sniffs/NamingConventions/ConstructorNameSniff.php +++ b/src/Standards/Generic/Sniffs/NamingConventions/ConstructorNameSniff.php @@ -67,10 +67,9 @@ protected function processTokenWithinScope(File $phpcsFile, $stackPtr, $currScop return; } - $className = $phpcsFile->getDeclarationName($currScope); - if (empty($className) === false) { - // Not an anonymous class. - $className = strtolower($className); + $className = '[Anonymous Class]'; + if ($tokens[$currScope]['code'] !== T_ANON_CLASS) { + $className = strtolower($phpcsFile->getDeclarationName($currScope)); } if ($className !== $this->currentClass) { @@ -79,7 +78,7 @@ protected function processTokenWithinScope(File $phpcsFile, $stackPtr, $currScop } $methodName = $phpcsFile->getDeclarationName($stackPtr); - if ($methodName === null) { + if ($methodName === '') { // Live coding or parse error. Bow out. return; } @@ -170,7 +169,7 @@ protected function loadFunctionNamesInScope(File $phpcsFile, $currScope) } $methodName = $phpcsFile->getDeclarationName($i); - if ($methodName === null) { + if ($methodName === '') { // Live coding or parse error. Ignore. continue; } diff --git a/src/Standards/Generic/Sniffs/NamingConventions/InterfaceNameSuffixSniff.php b/src/Standards/Generic/Sniffs/NamingConventions/InterfaceNameSuffixSniff.php index 6dfad6b406..1bd01b335c 100644 --- a/src/Standards/Generic/Sniffs/NamingConventions/InterfaceNameSuffixSniff.php +++ b/src/Standards/Generic/Sniffs/NamingConventions/InterfaceNameSuffixSniff.php @@ -39,7 +39,7 @@ public function register() public function process(File $phpcsFile, $stackPtr) { $interfaceName = $phpcsFile->getDeclarationName($stackPtr); - if ($interfaceName === null) { + if ($interfaceName === '') { // Live coding or parse error. Bow out. return; } diff --git a/src/Standards/Generic/Sniffs/NamingConventions/TraitNameSuffixSniff.php b/src/Standards/Generic/Sniffs/NamingConventions/TraitNameSuffixSniff.php index 79a77557d1..45bc549718 100644 --- a/src/Standards/Generic/Sniffs/NamingConventions/TraitNameSuffixSniff.php +++ b/src/Standards/Generic/Sniffs/NamingConventions/TraitNameSuffixSniff.php @@ -39,7 +39,7 @@ public function register() public function process(File $phpcsFile, $stackPtr) { $traitName = $phpcsFile->getDeclarationName($stackPtr); - if ($traitName === null) { + if ($traitName === '') { // Live coding or parse error. Bow out. return; } diff --git a/src/Standards/PEAR/Sniffs/Functions/FunctionDeclarationSniff.php b/src/Standards/PEAR/Sniffs/Functions/FunctionDeclarationSniff.php index 4ccc0b8137..c394aa592c 100644 --- a/src/Standards/PEAR/Sniffs/Functions/FunctionDeclarationSniff.php +++ b/src/Standards/PEAR/Sniffs/Functions/FunctionDeclarationSniff.php @@ -94,47 +94,49 @@ public function process(File $phpcsFile, $stackPtr) // and the opening parenthesis. // Unfinished closures are tokenized as T_FUNCTION however, and can be excluded // by checking if the function has a name. - $methodProps = $phpcsFile->getMethodProperties($stackPtr); - $methodName = $phpcsFile->getDeclarationName($stackPtr); - if ($tokens[$stackPtr]['code'] === T_FUNCTION && $methodName !== null) { - if ($tokens[($openBracket - 1)]['content'] === $phpcsFile->eolChar) { - $spaces = 'newline'; - } else if ($tokens[($openBracket - 1)]['code'] === T_WHITESPACE) { - $spaces = $tokens[($openBracket - 1)]['length']; - } else { - $spaces = 0; - } - - if ($spaces !== 0) { - $error = 'Expected 0 spaces before opening parenthesis; %s found'; - $data = [$spaces]; - $fix = $phpcsFile->addFixableError($error, $openBracket, 'SpaceBeforeOpenParen', $data); - if ($fix === true) { - $phpcsFile->fixer->replaceToken(($openBracket - 1), ''); + if ($tokens[$stackPtr]['code'] === T_FUNCTION) { + $methodProps = $phpcsFile->getMethodProperties($stackPtr); + $methodName = $phpcsFile->getDeclarationName($stackPtr); + if ($methodName !== '') { + if ($tokens[($openBracket - 1)]['content'] === $phpcsFile->eolChar) { + $spaces = 'newline'; + } else if ($tokens[($openBracket - 1)]['code'] === T_WHITESPACE) { + $spaces = $tokens[($openBracket - 1)]['length']; + } else { + $spaces = 0; } - } - // Must be no space before semicolon in abstract/interface methods. - if ($methodProps['has_body'] === false) { - $end = $phpcsFile->findNext(T_SEMICOLON, $closeBracket); - if ($end !== false) { - if ($tokens[($end - 1)]['content'] === $phpcsFile->eolChar) { - $spaces = 'newline'; - } else if ($tokens[($end - 1)]['code'] === T_WHITESPACE) { - $spaces = $tokens[($end - 1)]['length']; - } else { - $spaces = 0; + if ($spaces !== 0) { + $error = 'Expected 0 spaces before opening parenthesis; %s found'; + $data = [$spaces]; + $fix = $phpcsFile->addFixableError($error, $openBracket, 'SpaceBeforeOpenParen', $data); + if ($fix === true) { + $phpcsFile->fixer->replaceToken(($openBracket - 1), ''); } + } - if ($spaces !== 0) { - $error = 'Expected 0 spaces before semicolon; %s found'; - $data = [$spaces]; - $fix = $phpcsFile->addFixableError($error, $end, 'SpaceBeforeSemicolon', $data); - if ($fix === true) { - $phpcsFile->fixer->replaceToken(($end - 1), ''); + // Must be no space before semicolon in abstract/interface methods. + if ($methodProps['has_body'] === false) { + $end = $phpcsFile->findNext(T_SEMICOLON, $closeBracket); + if ($end !== false) { + if ($tokens[($end - 1)]['content'] === $phpcsFile->eolChar) { + $spaces = 'newline'; + } else if ($tokens[($end - 1)]['code'] === T_WHITESPACE) { + $spaces = $tokens[($end - 1)]['length']; + } else { + $spaces = 0; + } + + if ($spaces !== 0) { + $error = 'Expected 0 spaces before semicolon; %s found'; + $data = [$spaces]; + $fix = $phpcsFile->addFixableError($error, $end, 'SpaceBeforeSemicolon', $data); + if ($fix === true) { + $phpcsFile->fixer->replaceToken(($end - 1), ''); + } } } - } + }//end if }//end if }//end if diff --git a/src/Standards/PEAR/Sniffs/NamingConventions/ValidFunctionNameSniff.php b/src/Standards/PEAR/Sniffs/NamingConventions/ValidFunctionNameSniff.php index a20e03172c..f46fb2791d 100644 --- a/src/Standards/PEAR/Sniffs/NamingConventions/ValidFunctionNameSniff.php +++ b/src/Standards/PEAR/Sniffs/NamingConventions/ValidFunctionNameSniff.php @@ -83,14 +83,14 @@ protected function processTokenWithinScope(File $phpcsFile, $stackPtr, $currScop } $methodName = $phpcsFile->getDeclarationName($stackPtr); - if ($methodName === null) { + if ($methodName === '') { // Ignore live coding. return; } - $className = $phpcsFile->getDeclarationName($currScope); - if (isset($className) === false) { - $className = '[Anonymous Class]'; + $className = '[Anonymous Class]'; + if ($tokens[$currScope]['code'] !== T_ANON_CLASS) { + $className = $phpcsFile->getDeclarationName($currScope); } $errorData = [$className.'::'.$methodName]; @@ -181,11 +181,6 @@ protected function processTokenWithinScope(File $phpcsFile, $stackPtr, $currScop protected function processTokenOutsideScope(File $phpcsFile, $stackPtr) { $functionName = $phpcsFile->getDeclarationName($stackPtr); - if ($functionName === null) { - // Ignore live coding. - return; - } - if (ltrim($functionName, '_') === '') { // Ignore special functions. return; diff --git a/src/Standards/PSR1/Sniffs/Methods/CamelCapsMethodNameSniff.php b/src/Standards/PSR1/Sniffs/Methods/CamelCapsMethodNameSniff.php index 248cf51136..068d2a24a9 100644 --- a/src/Standards/PSR1/Sniffs/Methods/CamelCapsMethodNameSniff.php +++ b/src/Standards/PSR1/Sniffs/Methods/CamelCapsMethodNameSniff.php @@ -40,7 +40,7 @@ protected function processTokenWithinScope(File $phpcsFile, $stackPtr, $currScop } $methodName = $phpcsFile->getDeclarationName($stackPtr); - if ($methodName === null) { + if ($methodName === '') { // Ignore live coding. return; } @@ -58,9 +58,9 @@ protected function processTokenWithinScope(File $phpcsFile, $stackPtr, $currScop $testName = ltrim($methodName, '_'); if ($testName !== '' && Common::isCamelCaps($testName, false, true, false) === false) { $error = 'Method name "%s" is not in camel caps format'; - $className = $phpcsFile->getDeclarationName($currScope); - if (isset($className) === false) { - $className = '[Anonymous Class]'; + $className = '[Anonymous Class]'; + if ($tokens[$currScope]['code'] !== T_ANON_CLASS) { + $className = $phpcsFile->getDeclarationName($currScope); } $errorData = [$className.'::'.$methodName]; diff --git a/src/Standards/PSR2/Sniffs/Methods/MethodDeclarationSniff.php b/src/Standards/PSR2/Sniffs/Methods/MethodDeclarationSniff.php index e28fbd46ee..d70660886e 100644 --- a/src/Standards/PSR2/Sniffs/Methods/MethodDeclarationSniff.php +++ b/src/Standards/PSR2/Sniffs/Methods/MethodDeclarationSniff.php @@ -49,7 +49,7 @@ protected function processTokenWithinScope(File $phpcsFile, $stackPtr, $currScop } $methodName = $phpcsFile->getDeclarationName($stackPtr); - if ($methodName === null) { + if ($methodName === '') { // Ignore live coding. return; } diff --git a/src/Standards/Squiz/Sniffs/Classes/ClassFileNameSniff.php b/src/Standards/Squiz/Sniffs/Classes/ClassFileNameSniff.php index 346534ba4b..31af5f3a47 100644 --- a/src/Standards/Squiz/Sniffs/Classes/ClassFileNameSniff.php +++ b/src/Standards/Squiz/Sniffs/Classes/ClassFileNameSniff.php @@ -55,7 +55,7 @@ public function process(File $phpcsFile, $stackPtr) $tokens = $phpcsFile->getTokens(); $ooName = $phpcsFile->getDeclarationName($stackPtr); - if ($ooName === null) { + if ($ooName === '') { // Probably parse error/live coding. return; } diff --git a/src/Standards/Squiz/Sniffs/Commenting/ClosingDeclarationCommentSniff.php b/src/Standards/Squiz/Sniffs/Commenting/ClosingDeclarationCommentSniff.php index 7c3add51af..a9fd5d39b8 100644 --- a/src/Standards/Squiz/Sniffs/Commenting/ClosingDeclarationCommentSniff.php +++ b/src/Standards/Squiz/Sniffs/Commenting/ClosingDeclarationCommentSniff.php @@ -67,7 +67,7 @@ public function process(File $phpcsFile, $stackPtr) } $decName = $phpcsFile->getDeclarationName($stackPtr); - if ($decName === null) { + if ($decName === '') { // Parse error or live coding. return; } diff --git a/src/Standards/Squiz/Sniffs/Functions/GlobalFunctionSniff.php b/src/Standards/Squiz/Sniffs/Functions/GlobalFunctionSniff.php index af3d902ecb..1c3db67c0d 100644 --- a/src/Standards/Squiz/Sniffs/Functions/GlobalFunctionSniff.php +++ b/src/Standards/Squiz/Sniffs/Functions/GlobalFunctionSniff.php @@ -43,7 +43,7 @@ public function process(File $phpcsFile, $stackPtr) if (empty($tokens[$stackPtr]['conditions']) === true) { $functionName = $phpcsFile->getDeclarationName($stackPtr); - if ($functionName === null) { + if ($functionName === '') { return; } diff --git a/src/Standards/Squiz/Sniffs/NamingConventions/ValidFunctionNameSniff.php b/src/Standards/Squiz/Sniffs/NamingConventions/ValidFunctionNameSniff.php index 2450ba7fdb..90ceaf2212 100644 --- a/src/Standards/Squiz/Sniffs/NamingConventions/ValidFunctionNameSniff.php +++ b/src/Standards/Squiz/Sniffs/NamingConventions/ValidFunctionNameSniff.php @@ -29,7 +29,7 @@ class ValidFunctionNameSniff extends PEARValidFunctionNameSniff protected function processTokenOutsideScope(File $phpcsFile, $stackPtr) { $functionName = $phpcsFile->getDeclarationName($stackPtr); - if ($functionName === null) { + if ($functionName === '') { return; } diff --git a/src/Standards/Squiz/Sniffs/Scope/MethodScopeSniff.php b/src/Standards/Squiz/Sniffs/Scope/MethodScopeSniff.php index 4a1d6249d5..faad52e2b4 100644 --- a/src/Standards/Squiz/Sniffs/Scope/MethodScopeSniff.php +++ b/src/Standards/Squiz/Sniffs/Scope/MethodScopeSniff.php @@ -49,7 +49,7 @@ protected function processTokenWithinScope(File $phpcsFile, $stackPtr, $currScop } $methodName = $phpcsFile->getDeclarationName($stackPtr); - if ($methodName === null) { + if ($methodName === '') { // Ignore live coding. return; } diff --git a/tests/Core/File/GetDeclarationNameParseError1Test.php b/tests/Core/File/GetDeclarationNameParseError1Test.php index ee70c4b0f3..3c90169e5b 100644 --- a/tests/Core/File/GetDeclarationNameParseError1Test.php +++ b/tests/Core/File/GetDeclarationNameParseError1Test.php @@ -21,17 +21,17 @@ final class GetDeclarationNameParseError1Test extends AbstractMethodTestCase /** - * Test receiving "null" in case of a parse error. + * Test receiving an empty string in case of a parse error. * * @return void */ - public function testGetDeclarationNameNull() + public function testGetDeclarationName() { $target = $this->getTargetToken('/* testLiveCoding */', T_FUNCTION); $result = self::$phpcsFile->getDeclarationName($target); - $this->assertNull($result); + $this->assertSame('', $result); - }//end testGetDeclarationNameNull() + }//end testGetDeclarationName() }//end class diff --git a/tests/Core/File/GetDeclarationNameParseError2Test.php b/tests/Core/File/GetDeclarationNameParseError2Test.php index ad036a8d0d..4d8029b7d5 100644 --- a/tests/Core/File/GetDeclarationNameParseError2Test.php +++ b/tests/Core/File/GetDeclarationNameParseError2Test.php @@ -21,17 +21,17 @@ final class GetDeclarationNameParseError2Test extends AbstractMethodTestCase /** - * Test receiving "null" in case of a parse error. + * Test receiving an empty string in case of a parse error. * * @return void */ - public function testGetDeclarationNameNull() + public function testGetDeclarationName() { $target = $this->getTargetToken('/* testLiveCoding */', T_FUNCTION); $result = self::$phpcsFile->getDeclarationName($target); - $this->assertNull($result); + $this->assertSame('', $result); - }//end testGetDeclarationNameNull() + }//end testGetDeclarationName() }//end class diff --git a/tests/Core/File/GetDeclarationNameTest.php b/tests/Core/File/GetDeclarationNameTest.php index 6371aacf61..ed07605773 100644 --- a/tests/Core/File/GetDeclarationNameTest.php +++ b/tests/Core/File/GetDeclarationNameTest.php @@ -10,6 +10,7 @@ namespace PHP_CodeSniffer\Tests\Core\File; use PHP_CodeSniffer\Tests\Core\AbstractMethodTestCase; +use PHP_CodeSniffer\Util\Tokens; /** * Tests for the \PHP_CodeSniffer\Files\File:getDeclarationName method. @@ -23,47 +24,38 @@ final class GetDeclarationNameTest extends AbstractMethodTestCase /** * Test receiving an expected exception when a non-supported token is passed. * - * @return void - */ - public function testInvalidTokenPassed() - { - $this->expectRunTimeException('Token type "T_STRING" is not T_FUNCTION, T_CLASS, T_INTERFACE, T_TRAIT or T_ENUM'); - - $target = $this->getTargetToken('/* testInvalidTokenPassed */', T_STRING); - self::$phpcsFile->getDeclarationName($target); - - }//end testInvalidTokenPassed() - - - /** - * Test receiving "null" when passed an anonymous construct or in case of a parse error. - * * @param string $testMarker The comment which prefaces the target token in the test file. * @param int|string $targetType Token type of the token to get as stackPtr. * - * @dataProvider dataGetDeclarationNameNull + * @dataProvider dataInvalidTokenPassed * * @return void */ - public function testGetDeclarationNameNull($testMarker, $targetType) + public function testInvalidTokenPassed($testMarker, $targetType) { + $tokenName = Tokens::tokenName($targetType); + $this->expectRunTimeException('Token type "'.$tokenName.'" is not T_FUNCTION, T_CLASS, T_INTERFACE, T_TRAIT or T_ENUM'); + $target = $this->getTargetToken($testMarker, $targetType); - $result = self::$phpcsFile->getDeclarationName($target); - $this->assertNull($result); + self::$phpcsFile->getDeclarationName($target); - }//end testGetDeclarationNameNull() + }//end testInvalidTokenPassed() /** * Data provider. * - * @see testGetDeclarationNameNull() For the array format. + * @see testGetInvalidTokenPassed() For the array format. * * @return array> */ - public static function dataGetDeclarationNameNull() + public static function dataInvalidTokenPassed() { return [ + 'unsupported token T_STRING' => [ + 'testMarker' => '/* testInvalidTokenPassed */', + 'targetType' => T_STRING, + ], 'closure' => [ 'testMarker' => '/* testClosure */', 'targetType' => T_CLOSURE, @@ -86,7 +78,7 @@ public static function dataGetDeclarationNameNull() ], ]; - }//end dataGetDeclarationNameNull() + }//end dataInvalidTokenPassed() /**