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.inc b/src/Standards/Generic/Tests/PHP/LowerCaseTypeUnitTest.1.inc similarity index 97% rename from src/Standards/Generic/Tests/PHP/LowerCaseTypeUnitTest.inc rename to src/Standards/Generic/Tests/PHP/LowerCaseTypeUnitTest.1.inc index fb5b1fd5a8..021b1b08c5 100644 --- a/src/Standards/Generic/Tests/PHP/LowerCaseTypeUnitTest.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.inc.fixed b/src/Standards/Generic/Tests/PHP/LowerCaseTypeUnitTest.1.inc.fixed similarity index 97% rename from src/Standards/Generic/Tests/PHP/LowerCaseTypeUnitTest.inc.fixed rename to src/Standards/Generic/Tests/PHP/LowerCaseTypeUnitTest.1.inc.fixed index 10be06b0bb..f9e7ecc89f 100644 --- a/src/Standards/Generic/Tests/PHP/LowerCaseTypeUnitTest.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 @@ + */ - 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, + 143 => 1, + ]; + + default: + return []; + }//end switch }//end getErrorList() @@ -108,8 +117,7 @@ public function getErrorList() */ public function getWarningList() { - // Warning from getMemberProperties() about parse error. - return [144 => 1]; + 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.inc b/src/Standards/PEAR/Tests/NamingConventions/ValidVariableNameUnitTest.1.inc similarity index 94% rename from src/Standards/PEAR/Tests/NamingConventions/ValidVariableNameUnitTest.inc rename to src/Standards/PEAR/Tests/NamingConventions/ValidVariableNameUnitTest.1.inc index 3c03da3fd2..44bb869888 100644 --- a/src/Standards/PEAR/Tests/NamingConventions/ValidVariableNameUnitTest.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 @@ + */ - 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, + 105 => 1, + ]; + + default: + return []; + }//end switch }//end getErrorList() 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.inc b/src/Standards/PSR2/Tests/Classes/PropertyDeclarationUnitTest.1.inc similarity index 94% rename from src/Standards/PSR2/Tests/Classes/PropertyDeclarationUnitTest.inc rename to src/Standards/PSR2/Tests/Classes/PropertyDeclarationUnitTest.1.inc index 4db25459cc..9fe4f72631 100644 --- a/src/Standards/PSR2/Tests/Classes/PropertyDeclarationUnitTest.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.inc.fixed b/src/Standards/PSR2/Tests/Classes/PropertyDeclarationUnitTest.1.inc.fixed similarity index 94% rename from src/Standards/PSR2/Tests/Classes/PropertyDeclarationUnitTest.inc.fixed rename to src/Standards/PSR2/Tests/Classes/PropertyDeclarationUnitTest.1.inc.fixed index fd5d9fa59e..a89de7dfb1 100644 --- a/src/Standards/PSR2/Tests/Classes/PropertyDeclarationUnitTest.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 @@ + */ - 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,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 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, + 102 => 1, + ]; + + default: + return []; + }//end switch }//end getWarningList() 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.inc b/src/Standards/Squiz/Tests/NamingConventions/ValidVariableNameUnitTest.1.inc similarity index 96% rename from src/Standards/Squiz/Tests/NamingConventions/ValidVariableNameUnitTest.inc rename to src/Standards/Squiz/Tests/NamingConventions/ValidVariableNameUnitTest.1.inc index 87c3bdf2e7..016416161e 100644 --- a/src/Standards/Squiz/Tests/NamingConventions/ValidVariableNameUnitTest.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 @@ + */ - 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, + 161 => 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 98% rename from src/Standards/Squiz/Tests/Scope/MemberVarScopeUnitTest.inc rename to src/Standards/Squiz/Tests/Scope/MemberVarScopeUnitTest.1.inc index 0a1554ec89..16c14d9e34 100644 --- a/src/Standards/Squiz/Tests/Scope/MemberVarScopeUnitTest.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 @@ + */ - 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, + 71 => 1, + 75 => 1, + ]; + + default: + return []; + }//end switch }//end getErrorList() @@ -55,8 +64,7 @@ public function getErrorList() */ public function getWarningList() { - // Warning from getMemberProperties() about parse error. - return [71 => 1]; + 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.inc b/src/Standards/Zend/Tests/NamingConventions/ValidVariableNameUnitTest.1.inc similarity index 96% rename from src/Standards/Zend/Tests/NamingConventions/ValidVariableNameUnitTest.inc rename to src/Standards/Zend/Tests/NamingConventions/ValidVariableNameUnitTest.1.inc index 3325e1152d..89cb39b5ea 100644 --- a/src/Standards/Zend/Tests/NamingConventions/ValidVariableNameUnitTest.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 @@ + */ - 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, + 107 => 1, + 113 => 1, + 116 => 1, + 121 => 1, + 126 => 1, + 129 => 1, + 135 => 1, + ]; + + default: + return []; + }//end switch }//end getErrorList() @@ -72,29 +82,35 @@ 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, + ]; + + default: + return []; + }//end switch }//end getWarningList() 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()