diff --git a/CHANGELOG.md b/CHANGELOG.md index 7197ee5e81..6cebe514f7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -99,6 +99,8 @@ The file documents changes to the PHP_CodeSniffer project. - Thanks to Juliette Reinders Folmer (@jrfnl) for the patch - Runtime performance improvement for PHPCS CLI users. The improvement should be most noticeable for users on Windows. - Thanks to Juliette Reinders Folmer (@jrfnl) for the patch +- The following sniffs have received performance related improvements: + - Generic.PHP.LowerCaseType - The -e (explain) command will now list sniffs in natural order - Thanks to Juliette Reinders Folmer (@jrfnl) for the patch - Tests using the PHPCS native test framework with multiple test case files will now run the test case files in numeric order. diff --git a/src/Standards/Generic/Sniffs/PHP/LowerCaseTypeSniff.php b/src/Standards/Generic/Sniffs/PHP/LowerCaseTypeSniff.php index daa6f0f2e9..f309960d12 100644 --- a/src/Standards/Generic/Sniffs/PHP/LowerCaseTypeSniff.php +++ b/src/Standards/Generic/Sniffs/PHP/LowerCaseTypeSniff.php @@ -51,10 +51,10 @@ class LowerCaseTypeSniff implements Sniff public function register() { $tokens = Tokens::$castTokens; + $tokens += Tokens::$ooScopeTokens; $tokens[] = T_FUNCTION; $tokens[] = T_CLOSURE; $tokens[] = T_FN; - $tokens[] = T_VARIABLE; return $tokens; }//end register() @@ -90,40 +90,66 @@ public function process(File $phpcsFile, $stackPtr) * Check property types. */ - if ($tokens[$stackPtr]['code'] === T_VARIABLE) { - try { - $props = $phpcsFile->getMemberProperties($stackPtr); - } catch (RuntimeException $e) { - // Not an OO property. + if (isset(Tokens::$ooScopeTokens[$tokens[$stackPtr]['code']]) === true) { + if (isset($tokens[$stackPtr]['scope_opener'], $tokens[$stackPtr]['scope_closer']) === false) { return; } - if (empty($props) === true) { - // Parse error - property in interface or enum. Ignore. - return; - } + for ($i = ($tokens[$stackPtr]['scope_opener'] + 1); $i < $tokens[$stackPtr]['scope_closer']; $i++) { + // Skip over potentially large docblocks. + if ($tokens[$i]['code'] === \T_DOC_COMMENT_OPEN_TAG + && isset($tokens[$i]['comment_closer']) === true + ) { + $i = $tokens[$i]['comment_closer']; + continue; + } - // Strip off potential nullable indication. - $type = ltrim($props['type'], '?'); + // Skip over function declarations and everything nested within. + if ($tokens[$i]['code'] === \T_FUNCTION + && isset($tokens[$i]['scope_closer']) === true + ) { + $i = $tokens[$i]['scope_closer']; + continue; + } - if ($type !== '') { - $error = 'PHP property type declarations must be lowercase; expected "%s" but found "%s"'; - $errorCode = 'PropertyTypeFound'; + if ($tokens[$i]['code'] !== \T_VARIABLE) { + continue; + } - if ($props['type_token'] === T_TYPE_INTERSECTION) { - // Intersection types don't support simple types. - } else if (strpos($type, '|') !== false) { - $this->processUnionType( - $phpcsFile, - $props['type_token'], - $props['type_end_token'], - $error, - $errorCode - ); - } else if (isset($this->phpTypes[strtolower($type)]) === true) { - $this->processType($phpcsFile, $props['type_token'], $type, $error, $errorCode); + try { + $props = $phpcsFile->getMemberProperties($i); + } catch (RuntimeException $e) { + // Not an OO property. + continue; } - } + + if (empty($props) === true) { + // Parse error - property in interface or enum. Ignore. + return; + } + + // Strip off potential nullable indication. + $type = ltrim($props['type'], '?'); + + if ($type !== '') { + $error = 'PHP property type declarations must be lowercase; expected "%s" but found "%s"'; + $errorCode = 'PropertyTypeFound'; + + if ($props['type_token'] === T_TYPE_INTERSECTION) { + // Intersection types don't support simple types. + } else if (strpos($type, '|') !== false) { + $this->processUnionType( + $phpcsFile, + $props['type_token'], + $props['type_end_token'], + $error, + $errorCode + ); + } else if (isset($this->phpTypes[strtolower($type)]) === true) { + $this->processType($phpcsFile, $props['type_token'], $type, $error, $errorCode); + } + } + }//end for return; }//end if