Skip to content

Commit 73f7080

Browse files
committed
Classes/RestrictedExtendClasses: efficiency fix
The `AbstractClassRestrictionsSniff` class does quite a lot of processing before passing off to the `process_matched_token()` method, which can cause quite some overhead for a sniff like this, which is only interested in a class being extended, so let's make it so this sniff only listens to the `T_EXTENDS` token. This should make the sniff significantly faster.
1 parent dfe6a2c commit 73f7080

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

WordPressVIPMinimum/Sniffs/Classes/RestrictedExtendClassesSniff.php

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,20 @@ public function getGroups() {
3535
];
3636
}
3737

38+
/**
39+
* Returns an array of tokens this test wants to listen for.
40+
*
41+
* @return array<int|string>
42+
*/
43+
public function register() {
44+
$targets = parent::register();
45+
if ( empty( $targets ) ) {
46+
return $targets;
47+
}
48+
49+
return [ T_EXTENDS ];
50+
}
51+
3852
/**
3953
* Process a matched token.
4054
*
@@ -46,13 +60,6 @@ public function getGroups() {
4660
* @return void
4761
*/
4862
public function process_matched_token( $stackPtr, $group_name, $matched_content ) {
49-
$tokens = $this->phpcsFile->getTokens();
50-
51-
if ( $tokens[ $stackPtr ]['code'] !== T_EXTENDS ) {
52-
// If not extending, bail.
53-
return;
54-
}
55-
5663
foreach ( $this->getGroups() as $group => $group_args ) {
5764
$this->phpcsFile->{ 'add' . $group_args['type'] }( $group_args['message'], $stackPtr, $group );
5865
}

0 commit comments

Comments
 (0)