From 8a464d96ccd4d3377f754f213df638dcdb810a47 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Tue, 11 Mar 2025 06:56:14 +0100 Subject: [PATCH 1/2] Tests: rename various test case files ... to allow for adding additional test case files containing parse errors. --- ...itTest.inc => LowerCaseTypeUnitTest.1.inc} | 0 ...ixed => LowerCaseTypeUnitTest.1.inc.fixed} | 0 .../Tests/PHP/LowerCaseTypeUnitTest.php | 152 ++++++++++-------- ...st.inc => ValidVariableNameUnitTest.1.inc} | 0 .../ValidVariableNameUnitTest.php | 28 ++-- ....inc => PropertyDeclarationUnitTest.1.inc} | 0 ...> PropertyDeclarationUnitTest.1.inc.fixed} | 0 .../Classes/PropertyDeclarationUnitTest.php | 96 ++++++----- ...st.inc => ValidVariableNameUnitTest.1.inc} | 0 .../ValidVariableNameUnitTest.php | 88 +++++----- ...tTest.inc => MemberVarScopeUnitTest.1.inc} | 0 .../Tests/Scope/MemberVarScopeUnitTest.php | 46 ++++-- ...st.inc => ValidVariableNameUnitTest.1.inc} | 0 .../ValidVariableNameUnitTest.php | 122 ++++++++------ 14 files changed, 305 insertions(+), 227 deletions(-) rename src/Standards/Generic/Tests/PHP/{LowerCaseTypeUnitTest.inc => LowerCaseTypeUnitTest.1.inc} (100%) rename src/Standards/Generic/Tests/PHP/{LowerCaseTypeUnitTest.inc.fixed => LowerCaseTypeUnitTest.1.inc.fixed} (100%) rename src/Standards/PEAR/Tests/NamingConventions/{ValidVariableNameUnitTest.inc => ValidVariableNameUnitTest.1.inc} (100%) rename src/Standards/PSR2/Tests/Classes/{PropertyDeclarationUnitTest.inc => PropertyDeclarationUnitTest.1.inc} (100%) rename src/Standards/PSR2/Tests/Classes/{PropertyDeclarationUnitTest.inc.fixed => PropertyDeclarationUnitTest.1.inc.fixed} (100%) rename src/Standards/Squiz/Tests/NamingConventions/{ValidVariableNameUnitTest.inc => ValidVariableNameUnitTest.1.inc} (100%) rename src/Standards/Squiz/Tests/Scope/{MemberVarScopeUnitTest.inc => MemberVarScopeUnitTest.1.inc} (100%) rename src/Standards/Zend/Tests/NamingConventions/{ValidVariableNameUnitTest.inc => ValidVariableNameUnitTest.1.inc} (100%) diff --git a/src/Standards/Generic/Tests/PHP/LowerCaseTypeUnitTest.inc b/src/Standards/Generic/Tests/PHP/LowerCaseTypeUnitTest.1.inc similarity index 100% rename from src/Standards/Generic/Tests/PHP/LowerCaseTypeUnitTest.inc rename to src/Standards/Generic/Tests/PHP/LowerCaseTypeUnitTest.1.inc diff --git a/src/Standards/Generic/Tests/PHP/LowerCaseTypeUnitTest.inc.fixed b/src/Standards/Generic/Tests/PHP/LowerCaseTypeUnitTest.1.inc.fixed similarity index 100% rename from src/Standards/Generic/Tests/PHP/LowerCaseTypeUnitTest.inc.fixed rename to src/Standards/Generic/Tests/PHP/LowerCaseTypeUnitTest.1.inc.fixed diff --git a/src/Standards/Generic/Tests/PHP/LowerCaseTypeUnitTest.php b/src/Standards/Generic/Tests/PHP/LowerCaseTypeUnitTest.php index 2621932857..948453672c 100644 --- a/src/Standards/Generic/Tests/PHP/LowerCaseTypeUnitTest.php +++ b/src/Standards/Generic/Tests/PHP/LowerCaseTypeUnitTest.php @@ -26,74 +26,82 @@ final class LowerCaseTypeUnitTest 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 [ - 14 => 1, - 15 => 1, - 16 => 1, - 17 => 1, - 18 => 1, - 21 => 4, - 22 => 3, - 23 => 3, - 25 => 1, - 26 => 2, - 27 => 2, - 32 => 4, - 36 => 1, - 37 => 1, - 38 => 1, - 39 => 1, - 43 => 2, - 44 => 1, - 46 => 1, - 49 => 1, - 51 => 2, - 53 => 1, - 55 => 2, - 60 => 1, - 61 => 1, - 62 => 1, - 63 => 1, - 64 => 1, - 65 => 1, - 66 => 1, - 67 => 1, - 68 => 1, - 69 => 1, - 71 => 3, - 72 => 2, - 73 => 3, - 74 => 3, - 78 => 3, - 82 => 2, - 85 => 1, - 94 => 5, - 96 => 4, - 105 => 1, - 106 => 1, - 107 => 1, - 111 => 1, - 112 => 1, - 113 => 1, - 114 => 1, - 117 => 1, - 118 => 1, - 119 => 1, - 122 => 3, - 123 => 2, - 124 => 3, - 125 => 3, - 129 => 2, - 131 => 1, - 134 => 1, - 135 => 1, - 136 => 1, - 139 => 2, - ]; + switch ($testFile) { + case 'LowerCaseTypeUnitTest.1.inc': + return [ + 14 => 1, + 15 => 1, + 16 => 1, + 17 => 1, + 18 => 1, + 21 => 4, + 22 => 3, + 23 => 3, + 25 => 1, + 26 => 2, + 27 => 2, + 32 => 4, + 36 => 1, + 37 => 1, + 38 => 1, + 39 => 1, + 43 => 2, + 44 => 1, + 46 => 1, + 49 => 1, + 51 => 2, + 53 => 1, + 55 => 2, + 60 => 1, + 61 => 1, + 62 => 1, + 63 => 1, + 64 => 1, + 65 => 1, + 66 => 1, + 67 => 1, + 68 => 1, + 69 => 1, + 71 => 3, + 72 => 2, + 73 => 3, + 74 => 3, + 78 => 3, + 82 => 2, + 85 => 1, + 94 => 5, + 96 => 4, + 105 => 1, + 106 => 1, + 107 => 1, + 111 => 1, + 112 => 1, + 113 => 1, + 114 => 1, + 117 => 1, + 118 => 1, + 119 => 1, + 122 => 3, + 123 => 2, + 124 => 3, + 125 => 3, + 129 => 2, + 131 => 1, + 134 => 1, + 135 => 1, + 136 => 1, + 139 => 2, + ]; + + default: + return []; + }//end switch }//end getErrorList() @@ -104,12 +112,20 @@ 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='') { - // Warning from getMemberProperties() about parse error. - return [144 => 1]; + switch ($testFile) { + case 'LowerCaseTypeUnitTest.1.inc': + // Warning from getMemberProperties() about parse error. + return [144 => 1]; + + default: + return []; + }//end switch }//end getWarningList() diff --git a/src/Standards/PEAR/Tests/NamingConventions/ValidVariableNameUnitTest.inc b/src/Standards/PEAR/Tests/NamingConventions/ValidVariableNameUnitTest.1.inc similarity index 100% rename from src/Standards/PEAR/Tests/NamingConventions/ValidVariableNameUnitTest.inc rename to src/Standards/PEAR/Tests/NamingConventions/ValidVariableNameUnitTest.1.inc diff --git a/src/Standards/PEAR/Tests/NamingConventions/ValidVariableNameUnitTest.php b/src/Standards/PEAR/Tests/NamingConventions/ValidVariableNameUnitTest.php index c98af3bc51..f737b140d3 100644 --- a/src/Standards/PEAR/Tests/NamingConventions/ValidVariableNameUnitTest.php +++ b/src/Standards/PEAR/Tests/NamingConventions/ValidVariableNameUnitTest.php @@ -26,19 +26,27 @@ final class ValidVariableNameUnitTest 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 [ - 12 => 1, - 17 => 1, - 22 => 1, - 92 => 1, - 93 => 1, - 94 => 1, - 99 => 1, - ]; + switch ($testFile) { + case 'ValidVariableNameUnitTest.1.inc': + return [ + 12 => 1, + 17 => 1, + 22 => 1, + 92 => 1, + 93 => 1, + 94 => 1, + 99 => 1, + ]; + + default: + return []; + }//end switch }//end getErrorList() diff --git a/src/Standards/PSR2/Tests/Classes/PropertyDeclarationUnitTest.inc b/src/Standards/PSR2/Tests/Classes/PropertyDeclarationUnitTest.1.inc similarity index 100% rename from src/Standards/PSR2/Tests/Classes/PropertyDeclarationUnitTest.inc rename to src/Standards/PSR2/Tests/Classes/PropertyDeclarationUnitTest.1.inc diff --git a/src/Standards/PSR2/Tests/Classes/PropertyDeclarationUnitTest.inc.fixed b/src/Standards/PSR2/Tests/Classes/PropertyDeclarationUnitTest.1.inc.fixed similarity index 100% rename from src/Standards/PSR2/Tests/Classes/PropertyDeclarationUnitTest.inc.fixed rename to src/Standards/PSR2/Tests/Classes/PropertyDeclarationUnitTest.1.inc.fixed diff --git a/src/Standards/PSR2/Tests/Classes/PropertyDeclarationUnitTest.php b/src/Standards/PSR2/Tests/Classes/PropertyDeclarationUnitTest.php index 6310098525..e600a199a9 100644 --- a/src/Standards/PSR2/Tests/Classes/PropertyDeclarationUnitTest.php +++ b/src/Standards/PSR2/Tests/Classes/PropertyDeclarationUnitTest.php @@ -26,42 +26,50 @@ final class PropertyDeclarationUnitTest 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 => 1, - 9 => 2, - 10 => 1, - 11 => 1, - 17 => 1, - 18 => 1, - 23 => 1, - 38 => 1, - 41 => 1, - 42 => 1, - 50 => 2, - 51 => 1, - 55 => 1, - 56 => 1, - 61 => 1, - 62 => 1, - 68 => 1, - 69 => 1, - 71 => 1, - 72 => 1, - 76 => 1, - 80 => 1, - 82 => 1, - 84 => 1, - 86 => 1, - 90 => 1, - 94 => 1, - 95 => 1, - 96 => 1, - 97 => 2, - ]; + switch ($testFile) { + case 'PropertyDeclarationUnitTest.1.inc': + return [ + 7 => 1, + 9 => 2, + 10 => 1, + 11 => 1, + 17 => 1, + 18 => 1, + 23 => 1, + 38 => 1, + 41 => 1, + 42 => 1, + 50 => 2, + 51 => 1, + 55 => 1, + 56 => 1, + 61 => 1, + 62 => 1, + 68 => 1, + 69 => 1, + 71 => 1, + 72 => 1, + 76 => 1, + 80 => 1, + 82 => 1, + 84 => 1, + 86 => 1, + 90 => 1, + 94 => 1, + 95 => 1, + 96 => 1, + 97 => 2, + ]; + + default: + return []; + }//end switch }//end getErrorList() @@ -72,16 +80,24 @@ 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 [ - 13 => 1, - 14 => 1, - 15 => 1, - 53 => 1, - ]; + switch ($testFile) { + case 'PropertyDeclarationUnitTest.1.inc': + return [ + 13 => 1, + 14 => 1, + 15 => 1, + 53 => 1, + ]; + + default: + return []; + }//end switch }//end getWarningList() diff --git a/src/Standards/Squiz/Tests/NamingConventions/ValidVariableNameUnitTest.inc b/src/Standards/Squiz/Tests/NamingConventions/ValidVariableNameUnitTest.1.inc similarity index 100% rename from src/Standards/Squiz/Tests/NamingConventions/ValidVariableNameUnitTest.inc rename to src/Standards/Squiz/Tests/NamingConventions/ValidVariableNameUnitTest.1.inc diff --git a/src/Standards/Squiz/Tests/NamingConventions/ValidVariableNameUnitTest.php b/src/Standards/Squiz/Tests/NamingConventions/ValidVariableNameUnitTest.php index d8d8695c6e..3057bc3d63 100644 --- a/src/Standards/Squiz/Tests/NamingConventions/ValidVariableNameUnitTest.php +++ b/src/Standards/Squiz/Tests/NamingConventions/ValidVariableNameUnitTest.php @@ -26,51 +26,57 @@ final class ValidVariableNameUnitTest 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='') { - $errors = [ - 3 => 1, - 5 => 1, - 10 => 1, - 12 => 1, - 15 => 1, - 17 => 1, - 20 => 1, - 22 => 1, - 25 => 1, - 27 => 1, - 31 => 1, - 33 => 1, - 36 => 1, - 37 => 1, - 39 => 1, - 42 => 1, - 44 => 1, - 53 => 1, - 58 => 1, - 62 => 1, - 63 => 1, - 64 => 1, - 67 => 1, - 81 => 1, - 106 => 1, - 107 => 2, - 108 => 1, - 111 => 1, - 112 => 1, - 113 => 1, - 114 => 1, - 123 => 1, - 138 => 1, - 141 => 1, - 146 => 1, - 152 => 1, - 155 => 1, - ]; + switch ($testFile) { + case 'ValidVariableNameUnitTest.1.inc': + return [ + 3 => 1, + 5 => 1, + 10 => 1, + 12 => 1, + 15 => 1, + 17 => 1, + 20 => 1, + 22 => 1, + 25 => 1, + 27 => 1, + 31 => 1, + 33 => 1, + 36 => 1, + 37 => 1, + 39 => 1, + 42 => 1, + 44 => 1, + 53 => 1, + 58 => 1, + 62 => 1, + 63 => 1, + 64 => 1, + 67 => 1, + 81 => 1, + 106 => 1, + 107 => 2, + 108 => 1, + 111 => 1, + 112 => 1, + 113 => 1, + 114 => 1, + 123 => 1, + 138 => 1, + 141 => 1, + 146 => 1, + 152 => 1, + 155 => 1, + ]; - return $errors; + default: + return []; + }//end switch }//end getErrorList() diff --git a/src/Standards/Squiz/Tests/Scope/MemberVarScopeUnitTest.inc b/src/Standards/Squiz/Tests/Scope/MemberVarScopeUnitTest.1.inc similarity index 100% rename from src/Standards/Squiz/Tests/Scope/MemberVarScopeUnitTest.inc rename to src/Standards/Squiz/Tests/Scope/MemberVarScopeUnitTest.1.inc diff --git a/src/Standards/Squiz/Tests/Scope/MemberVarScopeUnitTest.php b/src/Standards/Squiz/Tests/Scope/MemberVarScopeUnitTest.php index 309d029830..8fdb7d0d29 100644 --- a/src/Standards/Squiz/Tests/Scope/MemberVarScopeUnitTest.php +++ b/src/Standards/Squiz/Tests/Scope/MemberVarScopeUnitTest.php @@ -26,21 +26,29 @@ final class MemberVarScopeUnitTest 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 => 1, - 25 => 1, - 29 => 1, - 33 => 1, - 39 => 1, - 41 => 1, - 66 => 2, - 67 => 1, - 75 => 1, - ]; + switch ($testFile) { + case 'MemberVarScopeUnitTest.1.inc': + return [ + 7 => 1, + 25 => 1, + 29 => 1, + 33 => 1, + 39 => 1, + 41 => 1, + 66 => 2, + 67 => 1, + 75 => 1, + ]; + + default: + return []; + }//end switch }//end getErrorList() @@ -51,12 +59,20 @@ 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='') { - // Warning from getMemberProperties() about parse error. - return [71 => 1]; + switch ($testFile) { + case 'MemberVarScopeUnitTest.1.inc': + // Warning from getMemberProperties() about parse error. + return [71 => 1]; + + default: + return []; + }//end switch }//end getWarningList() diff --git a/src/Standards/Zend/Tests/NamingConventions/ValidVariableNameUnitTest.inc b/src/Standards/Zend/Tests/NamingConventions/ValidVariableNameUnitTest.1.inc similarity index 100% rename from src/Standards/Zend/Tests/NamingConventions/ValidVariableNameUnitTest.inc rename to src/Standards/Zend/Tests/NamingConventions/ValidVariableNameUnitTest.1.inc diff --git a/src/Standards/Zend/Tests/NamingConventions/ValidVariableNameUnitTest.php b/src/Standards/Zend/Tests/NamingConventions/ValidVariableNameUnitTest.php index c66a2a42c8..98c4093867 100644 --- a/src/Standards/Zend/Tests/NamingConventions/ValidVariableNameUnitTest.php +++ b/src/Standards/Zend/Tests/NamingConventions/ValidVariableNameUnitTest.php @@ -26,42 +26,50 @@ final class ValidVariableNameUnitTest 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, - 5 => 1, - 11 => 1, - 13 => 1, - 17 => 1, - 19 => 1, - 23 => 1, - 25 => 1, - 29 => 1, - 31 => 1, - 36 => 1, - 38 => 1, - 42 => 1, - 44 => 1, - 48 => 1, - 50 => 1, - 61 => 1, - 67 => 1, - 72 => 1, - 74 => 1, - 75 => 1, - 76 => 1, - 79 => 1, - 96 => 1, - 99 => 1, - 113 => 1, - 116 => 1, - 121 => 1, - 126 => 1, - 129 => 1, - ]; + switch ($testFile) { + case 'ValidVariableNameUnitTest.1.inc': + return [ + 3 => 1, + 5 => 1, + 11 => 1, + 13 => 1, + 17 => 1, + 19 => 1, + 23 => 1, + 25 => 1, + 29 => 1, + 31 => 1, + 36 => 1, + 38 => 1, + 42 => 1, + 44 => 1, + 48 => 1, + 50 => 1, + 61 => 1, + 67 => 1, + 72 => 1, + 74 => 1, + 75 => 1, + 76 => 1, + 79 => 1, + 96 => 1, + 99 => 1, + 113 => 1, + 116 => 1, + 121 => 1, + 126 => 1, + 129 => 1, + ]; + + default: + return []; + }//end switch }//end getErrorList() @@ -72,29 +80,37 @@ 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 [ - 6 => 1, - 14 => 1, - 20 => 1, - 26 => 1, - 32 => 1, - 39 => 1, - 45 => 1, - 51 => 1, - 64 => 1, - 70 => 1, - 73 => 1, - 76 => 1, - 79 => 1, - 82 => 1, - 94 => 1, - // Warning from getMemberProperties() about parse error. - 107 => 1, - ]; + switch ($testFile) { + case 'ValidVariableNameUnitTest.1.inc': + return [ + 6 => 1, + 14 => 1, + 20 => 1, + 26 => 1, + 32 => 1, + 39 => 1, + 45 => 1, + 51 => 1, + 64 => 1, + 70 => 1, + 73 => 1, + 76 => 1, + 79 => 1, + 82 => 1, + 94 => 1, + // Warning from getMemberProperties() about parse error. + 107 => 1, + ]; + + default: + return []; + }//end switch }//end getWarningList() From ba47d74cbe5ab55e0f3306b622310bc20d8173fa Mon Sep 17 00:00:00 2001 From: jrfnl Date: Tue, 11 Mar 2025 07:35:03 +0100 Subject: [PATCH 2/2] File::getMemberProperties(): removed parse error warning The `File::getMemberProperties()` method used to be inconsistent in how it handled variable tokens which were not property declarations. * In most cases ("normal" variable, function parameter), it would throw a `RuntimeException` _"'$stackPtr is not a class member var'"_. * However, for "properties" declared in an `enum` or `interface` construct, it would register a warning about a possible parse error and return an empty array. This parse error warning has now been removed. As, as of PHP 8.4, declaring (hooked) properties in an interface is no longer a parse error (see the [Property Hooks RFC](https://wiki.php.net/rfc/property-hooks)), properties declared in interfaces will now be analyzed by the function, like any other property, and will return an array of information about the property. For "properties" declared in enums, which is still not allowed in PHP, the method will throw the `RuntimeException` _"'$stackPtr is not a class member var'"_. Includes updated unit tests for the `File::getMemberProperties()` method. Includes a review of all uses of the `File::getMemberProperties()` method in PHPCS native sniffs and updating the code where necessary. Includes adding/updating tests with properties in interfaces and enums for each of those sniffs. Closes squizlabs/PHP_CodeSniffer 2455 Also related to 734 Co-authored-by: Greg Sherwood --- src/Files/File.php | 31 ++++------------- .../Generic/Sniffs/PHP/LowerCaseTypeSniff.php | 7 +--- .../Tests/PHP/LowerCaseTypeUnitTest.1.inc | 5 ++- .../PHP/LowerCaseTypeUnitTest.1.inc.fixed | 5 ++- .../Tests/PHP/LowerCaseTypeUnitTest.2.inc | 8 +++++ .../Tests/PHP/LowerCaseTypeUnitTest.php | 14 ++------ .../ValidVariableNameSniff.php | 10 +++--- .../ValidVariableNameUnitTest.1.inc | 5 +++ .../ValidVariableNameUnitTest.2.inc | 8 +++++ .../ValidVariableNameUnitTest.php | 15 ++++---- .../Classes/PropertyDeclarationSniff.php | 17 ++++------ .../Classes/PropertyDeclarationUnitTest.1.inc | 5 +++ .../PropertyDeclarationUnitTest.1.inc.fixed | 5 +++ .../Classes/PropertyDeclarationUnitTest.2.inc | 8 +++++ .../Classes/PropertyDeclarationUnitTest.php | 9 ++--- .../ValidVariableNameSniff.php | 16 ++++----- .../Sniffs/Scope/MemberVarScopeSniff.php | 16 ++++++--- .../ValidVariableNameUnitTest.1.inc | 5 +++ .../ValidVariableNameUnitTest.2.inc | 8 +++++ .../ValidVariableNameUnitTest.php | 1 + .../Tests/Scope/MemberVarScopeUnitTest.1.inc | 2 +- .../Tests/Scope/MemberVarScopeUnitTest.2.inc | 8 +++++ .../Tests/Scope/MemberVarScopeUnitTest.php | 14 ++------ .../ValidVariableNameSniff.php | 14 ++++---- .../ValidVariableNameUnitTest.1.inc | 5 +++ .../ValidVariableNameUnitTest.2.inc | 8 +++++ .../ValidVariableNameUnitTest.php | 34 +++++++++---------- tests/Core/File/GetMemberPropertiesTest.php | 19 +++++++---- 28 files changed, 174 insertions(+), 128 deletions(-) create mode 100644 src/Standards/Generic/Tests/PHP/LowerCaseTypeUnitTest.2.inc create mode 100644 src/Standards/PEAR/Tests/NamingConventions/ValidVariableNameUnitTest.2.inc create mode 100644 src/Standards/PSR2/Tests/Classes/PropertyDeclarationUnitTest.2.inc create mode 100644 src/Standards/Squiz/Tests/NamingConventions/ValidVariableNameUnitTest.2.inc create mode 100644 src/Standards/Squiz/Tests/Scope/MemberVarScopeUnitTest.2.inc create mode 100644 src/Standards/Zend/Tests/NamingConventions/ValidVariableNameUnitTest.2.inc diff --git a/src/Files/File.php b/src/Files/File.php index 34d371f713..dbaaf3b204 100644 --- a/src/Files/File.php +++ b/src/Files/File.php @@ -1791,34 +1791,15 @@ public function getMemberProperties($stackPtr) throw new RuntimeException('$stackPtr must be of type T_VARIABLE'); } - $conditions = array_keys($this->tokens[$stackPtr]['conditions']); + $conditions = $this->tokens[$stackPtr]['conditions']; + $conditions = array_keys($conditions); $ptr = array_pop($conditions); if (isset($this->tokens[$ptr]) === false - || ($this->tokens[$ptr]['code'] !== T_CLASS - && $this->tokens[$ptr]['code'] !== T_ANON_CLASS - && $this->tokens[$ptr]['code'] !== T_TRAIT) + || isset(Tokens::$ooScopeTokens[$this->tokens[$ptr]['code']]) === false + || $this->tokens[$ptr]['code'] === T_ENUM ) { - if (isset($this->tokens[$ptr]) === true - && ($this->tokens[$ptr]['code'] === T_INTERFACE - || $this->tokens[$ptr]['code'] === T_ENUM) - ) { - // T_VARIABLEs in interfaces/enums can actually be method arguments - // but they won't be seen as being inside the method because there - // are no scope openers and closers for abstract methods. If it is in - // parentheses, we can be pretty sure it is a method argument. - if (isset($this->tokens[$stackPtr]['nested_parenthesis']) === false - || empty($this->tokens[$stackPtr]['nested_parenthesis']) === true - ) { - $error = 'Possible parse error: %ss may not include member vars'; - $code = sprintf('Internal.ParseError.%sHasMemberVar', ucfirst($this->tokens[$ptr]['content'])); - $data = [strtolower($this->tokens[$ptr]['content'])]; - $this->addWarning($error, $stackPtr, $code, $data); - return []; - } - } else { - throw new RuntimeException('$stackPtr is not a class member var'); - } - }//end if + throw new RuntimeException('$stackPtr is not a class member var'); + } // Make sure it's not a method parameter. if (empty($this->tokens[$stackPtr]['nested_parenthesis']) === false) { diff --git a/src/Standards/Generic/Sniffs/PHP/LowerCaseTypeSniff.php b/src/Standards/Generic/Sniffs/PHP/LowerCaseTypeSniff.php index 1b085853e1..2da9709ec5 100644 --- a/src/Standards/Generic/Sniffs/PHP/LowerCaseTypeSniff.php +++ b/src/Standards/Generic/Sniffs/PHP/LowerCaseTypeSniff.php @@ -162,15 +162,10 @@ public function process(File $phpcsFile, $stackPtr) try { $props = $phpcsFile->getMemberProperties($i); } catch (RuntimeException $e) { - // Not an OO property. + // Parse error: property in enum. Ignore. continue; } - if (empty($props) === true) { - // Parse error - property in interface or enum. Ignore. - return; - } - // Strip off potential nullable indication. $type = ltrim($props['type'], '?'); diff --git a/src/Standards/Generic/Tests/PHP/LowerCaseTypeUnitTest.1.inc b/src/Standards/Generic/Tests/PHP/LowerCaseTypeUnitTest.1.inc index fb5b1fd5a8..021b1b08c5 100644 --- a/src/Standards/Generic/Tests/PHP/LowerCaseTypeUnitTest.1.inc +++ b/src/Standards/Generic/Tests/PHP/LowerCaseTypeUnitTest.1.inc @@ -139,7 +139,6 @@ class DNFTypes { function DNFReturnTypes ($var): object|(Self&\Package\Other_Class)|sTRINg|false {} } -// Intentional error, should be ignored by the sniff. -interface PropertiesNotAllowed { - public $notAllowed; +interface PHP84HookedProperty { + public String $readable { get; } } diff --git a/src/Standards/Generic/Tests/PHP/LowerCaseTypeUnitTest.1.inc.fixed b/src/Standards/Generic/Tests/PHP/LowerCaseTypeUnitTest.1.inc.fixed index 10be06b0bb..f9e7ecc89f 100644 --- a/src/Standards/Generic/Tests/PHP/LowerCaseTypeUnitTest.1.inc.fixed +++ b/src/Standards/Generic/Tests/PHP/LowerCaseTypeUnitTest.1.inc.fixed @@ -139,7 +139,6 @@ class DNFTypes { function DNFReturnTypes ($var): object|(self&\Package\Other_Class)|string|false {} } -// Intentional error, should be ignored by the sniff. -interface PropertiesNotAllowed { - public $notAllowed; +interface PHP84HookedProperty { + public string $readable { get; } } diff --git a/src/Standards/Generic/Tests/PHP/LowerCaseTypeUnitTest.2.inc b/src/Standards/Generic/Tests/PHP/LowerCaseTypeUnitTest.2.inc new file mode 100644 index 0000000000..002dfe5ebf --- /dev/null +++ b/src/Standards/Generic/Tests/PHP/LowerCaseTypeUnitTest.2.inc @@ -0,0 +1,8 @@ + 1, 136 => 1, 139 => 2, + 143 => 1, ]; default: @@ -112,20 +113,11 @@ public function getErrorList($testFile='') * 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($testFile='') + public function getWarningList() { - switch ($testFile) { - case 'LowerCaseTypeUnitTest.1.inc': - // Warning from getMemberProperties() about parse error. - return [144 => 1]; - - default: - return []; - }//end switch + return []; }//end getWarningList() diff --git a/src/Standards/PEAR/Sniffs/NamingConventions/ValidVariableNameSniff.php b/src/Standards/PEAR/Sniffs/NamingConventions/ValidVariableNameSniff.php index 48674b1e80..33ba8737cd 100644 --- a/src/Standards/PEAR/Sniffs/NamingConventions/ValidVariableNameSniff.php +++ b/src/Standards/PEAR/Sniffs/NamingConventions/ValidVariableNameSniff.php @@ -9,6 +9,7 @@ namespace PHP_CodeSniffer\Standards\PEAR\Sniffs\NamingConventions; +use PHP_CodeSniffer\Exceptions\RuntimeException; use PHP_CodeSniffer\Files\File; use PHP_CodeSniffer\Sniffs\AbstractVariableSniff; @@ -27,13 +28,14 @@ class ValidVariableNameSniff extends AbstractVariableSniff */ protected function processMemberVar(File $phpcsFile, $stackPtr) { - $tokens = $phpcsFile->getTokens(); - - $memberProps = $phpcsFile->getMemberProperties($stackPtr); - if (empty($memberProps) === true) { + try { + $memberProps = $phpcsFile->getMemberProperties($stackPtr); + } catch (RuntimeException $e) { + // Parse error: property in enum. Ignore. return; } + $tokens = $phpcsFile->getTokens(); $memberName = ltrim($tokens[$stackPtr]['content'], '$'); $scope = $memberProps['scope']; $scopeSpecified = $memberProps['scope_specified']; diff --git a/src/Standards/PEAR/Tests/NamingConventions/ValidVariableNameUnitTest.1.inc b/src/Standards/PEAR/Tests/NamingConventions/ValidVariableNameUnitTest.1.inc index 3c03da3fd2..44bb869888 100644 --- a/src/Standards/PEAR/Tests/NamingConventions/ValidVariableNameUnitTest.1.inc +++ b/src/Standards/PEAR/Tests/NamingConventions/ValidVariableNameUnitTest.1.inc @@ -99,3 +99,8 @@ $util->setLogger( private $varName = 'hello'; private $_varName = 'hello'; }); + +interface PHP84HookedProperty { + public $thisisfine { get; } + public $_underscore { get; } +} diff --git a/src/Standards/PEAR/Tests/NamingConventions/ValidVariableNameUnitTest.2.inc b/src/Standards/PEAR/Tests/NamingConventions/ValidVariableNameUnitTest.2.inc new file mode 100644 index 0000000000..b87e48ae71 --- /dev/null +++ b/src/Standards/PEAR/Tests/NamingConventions/ValidVariableNameUnitTest.2.inc @@ -0,0 +1,8 @@ + 1, - 17 => 1, - 22 => 1, - 92 => 1, - 93 => 1, - 94 => 1, - 99 => 1, + 12 => 1, + 17 => 1, + 22 => 1, + 92 => 1, + 93 => 1, + 94 => 1, + 99 => 1, + 105 => 1, ]; default: diff --git a/src/Standards/PSR2/Sniffs/Classes/PropertyDeclarationSniff.php b/src/Standards/PSR2/Sniffs/Classes/PropertyDeclarationSniff.php index 70828bb991..73eae4158f 100644 --- a/src/Standards/PSR2/Sniffs/Classes/PropertyDeclarationSniff.php +++ b/src/Standards/PSR2/Sniffs/Classes/PropertyDeclarationSniff.php @@ -28,6 +28,13 @@ class PropertyDeclarationSniff extends AbstractVariableSniff */ protected function processMemberVar(File $phpcsFile, $stackPtr) { + try { + $propertyInfo = $phpcsFile->getMemberProperties($stackPtr); + } catch (Exception $e) { + // Parse error: property in enum. Ignore. + return; + } + $tokens = $phpcsFile->getTokens(); if ($tokens[$stackPtr]['content'][1] === '_') { @@ -63,16 +70,6 @@ protected function processMemberVar(File $phpcsFile, $stackPtr) $phpcsFile->addError($error, $stackPtr, 'Multiple'); } - try { - $propertyInfo = $phpcsFile->getMemberProperties($stackPtr); - if (empty($propertyInfo) === true) { - return; - } - } catch (Exception $e) { - // Turns out not to be a property after all. - return; - } - if ($propertyInfo['type'] !== '') { $typeToken = $propertyInfo['type_end_token']; $error = 'There must be 1 space after the property type declaration; %s found'; diff --git a/src/Standards/PSR2/Tests/Classes/PropertyDeclarationUnitTest.1.inc b/src/Standards/PSR2/Tests/Classes/PropertyDeclarationUnitTest.1.inc index 4db25459cc..9fe4f72631 100644 --- a/src/Standards/PSR2/Tests/Classes/PropertyDeclarationUnitTest.1.inc +++ b/src/Standards/PSR2/Tests/Classes/PropertyDeclarationUnitTest.1.inc @@ -96,3 +96,8 @@ class FinalProperties { public FINAL ?int $wrongOrder1; static protected final ?string $wrongOrder2; } + +interface PHP84HookedProperty { + public $thisisfine { get; } + public $_underscore { get; } +} diff --git a/src/Standards/PSR2/Tests/Classes/PropertyDeclarationUnitTest.1.inc.fixed b/src/Standards/PSR2/Tests/Classes/PropertyDeclarationUnitTest.1.inc.fixed index fd5d9fa59e..a89de7dfb1 100644 --- a/src/Standards/PSR2/Tests/Classes/PropertyDeclarationUnitTest.1.inc.fixed +++ b/src/Standards/PSR2/Tests/Classes/PropertyDeclarationUnitTest.1.inc.fixed @@ -93,3 +93,8 @@ class FinalProperties { FINAL public ?int $wrongOrder1; final protected static ?string $wrongOrder2; } + +interface PHP84HookedProperty { + public $thisisfine { get; } + public $_underscore { get; } +} diff --git a/src/Standards/PSR2/Tests/Classes/PropertyDeclarationUnitTest.2.inc b/src/Standards/PSR2/Tests/Classes/PropertyDeclarationUnitTest.2.inc new file mode 100644 index 0000000000..b87e48ae71 --- /dev/null +++ b/src/Standards/PSR2/Tests/Classes/PropertyDeclarationUnitTest.2.inc @@ -0,0 +1,8 @@ + 1, - 14 => 1, - 15 => 1, - 53 => 1, + 13 => 1, + 14 => 1, + 15 => 1, + 53 => 1, + 102 => 1, ]; default: diff --git a/src/Standards/Squiz/Sniffs/NamingConventions/ValidVariableNameSniff.php b/src/Standards/Squiz/Sniffs/NamingConventions/ValidVariableNameSniff.php index 79f9c4b231..e96c578962 100644 --- a/src/Standards/Squiz/Sniffs/NamingConventions/ValidVariableNameSniff.php +++ b/src/Standards/Squiz/Sniffs/NamingConventions/ValidVariableNameSniff.php @@ -9,6 +9,7 @@ namespace PHP_CodeSniffer\Standards\Squiz\Sniffs\NamingConventions; +use PHP_CodeSniffer\Exceptions\RuntimeException; use PHP_CodeSniffer\Files\File; use PHP_CodeSniffer\Sniffs\AbstractVariableSniff; use PHP_CodeSniffer\Util\Common; @@ -114,18 +115,15 @@ protected function processVariable(File $phpcsFile, $stackPtr) */ protected function processMemberVar(File $phpcsFile, $stackPtr) { - $tokens = $phpcsFile->getTokens(); - - $varName = ltrim($tokens[$stackPtr]['content'], '$'); - $memberProps = $phpcsFile->getMemberProperties($stackPtr); - if (empty($memberProps) === true) { - // Couldn't get any info about this variable, which - // generally means it is invalid or possibly has a parse - // error. Any errors will be reported by the core, so - // we can ignore it. + try { + $memberProps = $phpcsFile->getMemberProperties($stackPtr); + } catch (RuntimeException $e) { + // Parse error: property in enum. Ignore. return; } + $tokens = $phpcsFile->getTokens(); + $varName = ltrim($tokens[$stackPtr]['content'], '$'); $public = ($memberProps['scope'] !== 'private'); $errorData = [$varName]; diff --git a/src/Standards/Squiz/Sniffs/Scope/MemberVarScopeSniff.php b/src/Standards/Squiz/Sniffs/Scope/MemberVarScopeSniff.php index 3d1c83f019..cccca20fb5 100644 --- a/src/Standards/Squiz/Sniffs/Scope/MemberVarScopeSniff.php +++ b/src/Standards/Squiz/Sniffs/Scope/MemberVarScopeSniff.php @@ -9,6 +9,7 @@ namespace PHP_CodeSniffer\Standards\Squiz\Sniffs\Scope; +use PHP_CodeSniffer\Exceptions\RuntimeException; use PHP_CodeSniffer\Files\File; use PHP_CodeSniffer\Sniffs\AbstractVariableSniff; @@ -26,15 +27,20 @@ class MemberVarScopeSniff extends AbstractVariableSniff */ protected function processMemberVar(File $phpcsFile, $stackPtr) { - $tokens = $phpcsFile->getTokens(); - $properties = $phpcsFile->getMemberProperties($stackPtr); + try { + $properties = $phpcsFile->getMemberProperties($stackPtr); + } catch (RuntimeException $e) { + // Parse error: property in enum. Ignore. + return; + } - if ($properties === [] || $properties['scope_specified'] !== false) { + if ($properties['scope_specified'] !== false) { return; } - $error = 'Scope modifier not specified for member variable "%s"'; - $data = [$tokens[$stackPtr]['content']]; + $tokens = $phpcsFile->getTokens(); + $error = 'Scope modifier not specified for member variable "%s"'; + $data = [$tokens[$stackPtr]['content']]; $phpcsFile->addError($error, $stackPtr, 'Missing', $data); }//end processMemberVar() diff --git a/src/Standards/Squiz/Tests/NamingConventions/ValidVariableNameUnitTest.1.inc b/src/Standards/Squiz/Tests/NamingConventions/ValidVariableNameUnitTest.1.inc index 87c3bdf2e7..016416161e 100644 --- a/src/Standards/Squiz/Tests/NamingConventions/ValidVariableNameUnitTest.1.inc +++ b/src/Standards/Squiz/Tests/NamingConventions/ValidVariableNameUnitTest.1.inc @@ -155,3 +155,8 @@ enum SomeEnum $bar_foo = 3; } } + +interface PHP84HookedProperty { + public $thisisfine { get; } + public $_underscore { get; } +} diff --git a/src/Standards/Squiz/Tests/NamingConventions/ValidVariableNameUnitTest.2.inc b/src/Standards/Squiz/Tests/NamingConventions/ValidVariableNameUnitTest.2.inc new file mode 100644 index 0000000000..b87e48ae71 --- /dev/null +++ b/src/Standards/Squiz/Tests/NamingConventions/ValidVariableNameUnitTest.2.inc @@ -0,0 +1,8 @@ + 1, 152 => 1, 155 => 1, + 161 => 1, ]; default: diff --git a/src/Standards/Squiz/Tests/Scope/MemberVarScopeUnitTest.1.inc b/src/Standards/Squiz/Tests/Scope/MemberVarScopeUnitTest.1.inc index 0a1554ec89..16c14d9e34 100644 --- a/src/Standards/Squiz/Tests/Scope/MemberVarScopeUnitTest.1.inc +++ b/src/Standards/Squiz/Tests/Scope/MemberVarScopeUnitTest.1.inc @@ -68,7 +68,7 @@ class MyClass { } interface Base { - protected $anonymous; + var $anonymous; } class PHP84FinalProperties { diff --git a/src/Standards/Squiz/Tests/Scope/MemberVarScopeUnitTest.2.inc b/src/Standards/Squiz/Tests/Scope/MemberVarScopeUnitTest.2.inc new file mode 100644 index 0000000000..a573cee3a6 --- /dev/null +++ b/src/Standards/Squiz/Tests/Scope/MemberVarScopeUnitTest.2.inc @@ -0,0 +1,8 @@ + 1, 66 => 2, 67 => 1, + 71 => 1, 75 => 1, ]; @@ -59,20 +60,11 @@ public function getErrorList($testFile='') * 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($testFile='') + public function getWarningList() { - switch ($testFile) { - case 'MemberVarScopeUnitTest.1.inc': - // Warning from getMemberProperties() about parse error. - return [71 => 1]; - - default: - return []; - }//end switch + return []; }//end getWarningList() diff --git a/src/Standards/Zend/Sniffs/NamingConventions/ValidVariableNameSniff.php b/src/Standards/Zend/Sniffs/NamingConventions/ValidVariableNameSniff.php index 41b1948101..32cf0ff636 100644 --- a/src/Standards/Zend/Sniffs/NamingConventions/ValidVariableNameSniff.php +++ b/src/Standards/Zend/Sniffs/NamingConventions/ValidVariableNameSniff.php @@ -9,6 +9,7 @@ namespace PHP_CodeSniffer\Standards\Zend\Sniffs\NamingConventions; +use PHP_CodeSniffer\Exceptions\RuntimeException; use PHP_CodeSniffer\Files\File; use PHP_CodeSniffer\Sniffs\AbstractVariableSniff; use PHP_CodeSniffer\Util\Common; @@ -114,15 +115,16 @@ protected function processVariable(File $phpcsFile, $stackPtr) */ protected function processMemberVar(File $phpcsFile, $stackPtr) { - $tokens = $phpcsFile->getTokens(); - $varName = ltrim($tokens[$stackPtr]['content'], '$'); - $memberProps = $phpcsFile->getMemberProperties($stackPtr); - if (empty($memberProps) === true) { - // Exception encountered. + try { + $memberProps = $phpcsFile->getMemberProperties($stackPtr); + } catch (RuntimeException $e) { + // Parse error: property in enum. Ignore. return; } - $public = ($memberProps['scope'] === 'public'); + $tokens = $phpcsFile->getTokens(); + $varName = ltrim($tokens[$stackPtr]['content'], '$'); + $public = ($memberProps['scope'] === 'public'); if ($public === true) { if (substr($varName, 0, 1) === '_') { diff --git a/src/Standards/Zend/Tests/NamingConventions/ValidVariableNameUnitTest.1.inc b/src/Standards/Zend/Tests/NamingConventions/ValidVariableNameUnitTest.1.inc index 3325e1152d..89cb39b5ea 100644 --- a/src/Standards/Zend/Tests/NamingConventions/ValidVariableNameUnitTest.1.inc +++ b/src/Standards/Zend/Tests/NamingConventions/ValidVariableNameUnitTest.1.inc @@ -129,3 +129,8 @@ enum SomeEnum $bar_foo = 3; } } + +interface PHP84HookedProperty { + public $thisisfine { get; } + public $_underscore { get; } +} diff --git a/src/Standards/Zend/Tests/NamingConventions/ValidVariableNameUnitTest.2.inc b/src/Standards/Zend/Tests/NamingConventions/ValidVariableNameUnitTest.2.inc new file mode 100644 index 0000000000..b87e48ae71 --- /dev/null +++ b/src/Standards/Zend/Tests/NamingConventions/ValidVariableNameUnitTest.2.inc @@ -0,0 +1,8 @@ + 1, 96 => 1, 99 => 1, + 107 => 1, 113 => 1, 116 => 1, 121 => 1, 126 => 1, 129 => 1, + 135 => 1, ]; default: @@ -89,23 +91,21 @@ public function getWarningList($testFile='') switch ($testFile) { case 'ValidVariableNameUnitTest.1.inc': return [ - 6 => 1, - 14 => 1, - 20 => 1, - 26 => 1, - 32 => 1, - 39 => 1, - 45 => 1, - 51 => 1, - 64 => 1, - 70 => 1, - 73 => 1, - 76 => 1, - 79 => 1, - 82 => 1, - 94 => 1, - // Warning from getMemberProperties() about parse error. - 107 => 1, + 6 => 1, + 14 => 1, + 20 => 1, + 26 => 1, + 32 => 1, + 39 => 1, + 45 => 1, + 51 => 1, + 64 => 1, + 70 => 1, + 73 => 1, + 76 => 1, + 79 => 1, + 82 => 1, + 94 => 1, ]; default: diff --git a/tests/Core/File/GetMemberPropertiesTest.php b/tests/Core/File/GetMemberPropertiesTest.php index 53cfe5f3d1..402d0b99e5 100644 --- a/tests/Core/File/GetMemberPropertiesTest.php +++ b/tests/Core/File/GetMemberPropertiesTest.php @@ -609,9 +609,19 @@ public static function dataGetMemberProperties() 'nullable_type' => false, ], ], - 'invalid-property-in-interface' => [ + 'property-in-interface' => [ 'identifier' => '/* testInterfaceProperty */', - 'expected' => [], + 'expected' => [ + 'scope' => 'protected', + 'scope_specified' => true, + 'is_static' => false, + 'is_readonly' => false, + 'is_final' => false, + 'type' => '', + 'type_token' => false, + 'type_end_token' => false, + 'nullable_type' => false, + ], ], 'property-in-nested-class-1' => [ 'identifier' => '/* testNestedProperty 1 */', @@ -1006,10 +1016,6 @@ public static function dataGetMemberProperties() 'nullable_type' => false, ], ], - 'invalid-property-in-enum' => [ - 'identifier' => '/* testEnumProperty */', - 'expected' => [], - ], 'php8.1-single-intersection-type' => [ 'identifier' => '/* testPHP81IntersectionTypes */', 'expected' => [ @@ -1375,6 +1381,7 @@ public static function dataNotClassProperty() 'method parameter in anon class nested in ternary' => ['/* testNestedMethodParam 1 */'], 'method parameter in anon class nested in function call' => ['/* testNestedMethodParam 2 */'], 'method parameter in enum' => ['/* testEnumMethodParamNotProperty */'], + 'property in enum (parse error)' => ['/* testEnumProperty */'], ]; }//end dataNotClassProperty()