Skip to content

Generic/LowerCaseType: improve performance #3839

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 50 additions & 24 deletions src/Standards/Generic/Sniffs/PHP/LowerCaseTypeSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,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()
Expand Down Expand Up @@ -89,35 +89,61 @@ 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;
}

// Strip off potential nullable indication.
$type = ltrim($props['type'], '?');
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;
}

if ($type !== '') {
$error = 'PHP property type declarations must be lowercase; expected "%s" but found "%s"';
$errorCode = 'PropertyTypeFound';
// 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 ($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);
if ($tokens[$i]['code'] !== \T_VARIABLE) {
continue;
}
}

try {
$props = $phpcsFile->getMemberProperties($i);
} catch (RuntimeException $e) {
// Not an OO property.
continue;
}

// 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
Expand Down