Skip to content

Commit 6b82714

Browse files
committed
Generic/LowerCaseType: improve performance
Someone reported a performance issue with the `Generic.PHP.LowerCaseType` sniff to me. Running the Performance report (PR 3810) over a number of codebases, confirmed that the sniff ranked in the top 10 of "slow" sniffs. As it was, the sniff would examine all variables it comes across and disregard them if they are not properties or not typed. The "disregard when not a property" was done by catching an exception thrown by the `File::getMemberProperties()` method. As the majority of `T_VARIABLE` tokens in the average file are not property declarations, the `File::getMemberProperties()` method would be triggered lots and lots of times, with the majority of those times resulting in the need for creating and then catching and throwing away the above mentioned exception. By changing the logic for the sniff to only look within OO constructs and skip over anything non-property, thus avoiding the unnecessary exception creation, I can see a significant difference in the sniff run time. Using the test file which was originally shared with me and running the below command on PHP 7.4: ```bash phpcs -ps db.php --standard=Generic --report=source -vvv --sniffs=Generic.PHP.LowercaseType ``` ... yielded the following difference in runtime: Base time: ``` *** START SNIFF PROCESSING REPORT *** PHP_CodeSniffer\Standards\Generic\Sniffs\PHP\LowerCaseTypeSniff: 0.3802 secs *** END SNIFF PROCESSING REPORT *** ``` Time with the performance tweak included in this PR: ``` *** START SNIFF PROCESSING REPORT *** PHP_CodeSniffer\Standards\Generic\Sniffs\PHP\LowerCaseTypeSniff: 0.0113 secs *** END SNIFF PROCESSING REPORT *** ``` Using the performance report to benchmark the improvement with a larger number of files, I see improvement across the board as well: Command used: `phpcs -ps . --extensions=php --ignore=/vendor/ --report=performance --standard=psr12` Output for the `Generic.PHP.LowercaseType` sniff: Result | PHPCS itself | Set of Projects A | Set of Projects B | Set of Projects C | ------ | ------------------ | ------------------ | ------------------ | ----------------- | Nr of Files Scanned | 614 | 4115 | 25546 | 2250 | Before | 0.131587 ( 3.9 %) | 1.514729 ( 3.0 %) | 5.390167 ( 3.4 %) | 0.359674 ( 4.2 %) After | 0.029166 ( 0.9 %) | 0.449517 ( 0.9 %) | 1.917077 ( 1.2 %) | 0.181097 ( 2.2 %) --- I've also had a quick look at all other PHPCS native sniffs using the `File::getMemberProperties()` method. As those are all based on the `AbstractVariableSniff`, they don't seem to suffer from the same issue, or at least, nowhere near as badly. I also considered changing the setup of the sniff to start using the `AbstractVariableSniff`, but considering this particular sniff is also examining functions and type casts, I believe that would make the sniff more complex than necessary.
1 parent 4ac5785 commit 6b82714

File tree

1 file changed

+50
-24
lines changed

1 file changed

+50
-24
lines changed

src/Standards/Generic/Sniffs/PHP/LowerCaseTypeSniff.php

Lines changed: 50 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,10 @@ class LowerCaseTypeSniff implements Sniff
5050
public function register()
5151
{
5252
$tokens = Tokens::$castTokens;
53+
$tokens += Tokens::$ooScopeTokens;
5354
$tokens[] = T_FUNCTION;
5455
$tokens[] = T_CLOSURE;
5556
$tokens[] = T_FN;
56-
$tokens[] = T_VARIABLE;
5757
return $tokens;
5858

5959
}//end register()
@@ -89,35 +89,61 @@ public function process(File $phpcsFile, $stackPtr)
8989
* Check property types.
9090
*/
9191

92-
if ($tokens[$stackPtr]['code'] === T_VARIABLE) {
93-
try {
94-
$props = $phpcsFile->getMemberProperties($stackPtr);
95-
} catch (RuntimeException $e) {
96-
// Not an OO property.
92+
if (isset(Tokens::$ooScopeTokens[$tokens[$stackPtr]['code']]) === true) {
93+
if (isset($tokens[$stackPtr]['scope_opener'], $tokens[$stackPtr]['scope_closer']) === false) {
9794
return;
9895
}
9996

100-
// Strip off potential nullable indication.
101-
$type = ltrim($props['type'], '?');
97+
for ($i = ($tokens[$stackPtr]['scope_opener'] + 1); $i < $tokens[$stackPtr]['scope_closer']; $i++) {
98+
// Skip over potentially large docblocks.
99+
if ($tokens[$i]['code'] === \T_DOC_COMMENT_OPEN_TAG
100+
&& isset($tokens[$i]['comment_closer']) === true
101+
) {
102+
$i = $tokens[$i]['comment_closer'];
103+
continue;
104+
}
102105

103-
if ($type !== '') {
104-
$error = 'PHP property type declarations must be lowercase; expected "%s" but found "%s"';
105-
$errorCode = 'PropertyTypeFound';
106+
// Skip over function declarations and everything nested within.
107+
if ($tokens[$i]['code'] === \T_FUNCTION
108+
&& isset($tokens[$i]['scope_closer']) === true
109+
) {
110+
$i = $tokens[$i]['scope_closer'];
111+
continue;
112+
}
106113

107-
if ($props['type_token'] === T_TYPE_INTERSECTION) {
108-
// Intersection types don't support simple types.
109-
} else if (strpos($type, '|') !== false) {
110-
$this->processUnionType(
111-
$phpcsFile,
112-
$props['type_token'],
113-
$props['type_end_token'],
114-
$error,
115-
$errorCode
116-
);
117-
} else if (isset($this->phpTypes[strtolower($type)]) === true) {
118-
$this->processType($phpcsFile, $props['type_token'], $type, $error, $errorCode);
114+
if ($tokens[$i]['code'] !== \T_VARIABLE) {
115+
continue;
119116
}
120-
}
117+
118+
try {
119+
$props = $phpcsFile->getMemberProperties($i);
120+
} catch (RuntimeException $e) {
121+
// Not an OO property.
122+
continue;
123+
}
124+
125+
// Strip off potential nullable indication.
126+
$type = ltrim($props['type'], '?');
127+
128+
if ($type !== '') {
129+
$error = 'PHP property type declarations must be lowercase; expected "%s" but found "%s"';
130+
$errorCode = 'PropertyTypeFound';
131+
132+
if ($props['type_token'] === T_TYPE_INTERSECTION) {
133+
// Intersection types don't support simple types.
134+
} else if (strpos($type, '|') !== false) {
135+
$this->processUnionType(
136+
$phpcsFile,
137+
$props['type_token'],
138+
$props['type_end_token'],
139+
$error,
140+
$errorCode
141+
);
142+
} else if (isset($this->phpTypes[strtolower($type)]) === true) {
143+
$this->processType($phpcsFile, $props['type_token'], $type, $error, $errorCode);
144+
}
145+
}
146+
}//end for
121147

122148
return;
123149
}//end if

0 commit comments

Comments
 (0)