Reputation: 4783
I'm writing a Sniff in PHP_CodeSniffer for checking class names, and my abstract class names have some different naming requirements to non-abstract classes.
How can I identify if current Sniff is sniffing an abstract class or not?
Upvotes: 1
Views: 248
Reputation: 4783
Thanks to zerkms for pointing me in the right direction.
To check if the current Sniff is reading an abstract
or final
class, use the following code:
if (in_array(
$tokens[($stackPtr - 2)]['code'],
array(T_ABSTRACT, T_FINAL)
) === true
) {
// TRUE - class is abstract or final
} else {
// FALSE - class is NOT abstract or final
}
Removing either T_ABSTRACT
T_FINAL
would remove the check for that type.
i.e. Without the T_ABSTRACT
in the code above, it would return FALSE for an abstract
class, and still TRUE for a final
class.
You will also need this following code in the class, in order for the above code to work:
$tokens = $phpcsFile->getTokens();
I have tested the above code and it works as I have outlined in this answer.
With the limited info on the internet at present for PHP_CodeSniffer, hopefully this will help someone else.
Feel free to suggest edits or edit this answer.
Upvotes: 1