Skip to content

Commit

Permalink
Merge pull request #63 from PHPCSStandards/feature/generic-lowercaset…
Browse files Browse the repository at this point in the history
…ype-improve-performance

Generic/LowerCaseType: improve performance
  • Loading branch information
jrfnl authored Dec 5, 2023
2 parents 38c2caf + ce365fd commit b2464f7
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 28 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
82 changes: 54 additions & 28 deletions src/Standards/Generic/Sniffs/PHP/LowerCaseTypeSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit b2464f7

Please sign in to comment.