1

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?

James
  • 4,644
  • 5
  • 37
  • 48
  • If you're implementing your own sniff - the `abstract` keyword will be in the tokens stream. – zerkms Mar 09 '15 at 20:58
  • What do you currently have? Are you implementing your own sniff? – zerkms Mar 09 '15 at 21:09
  • http://pear.php.net/manual/en/package.php.php-codesniffer.coding-standard-tutorial.php ? – zerkms Mar 09 '15 at 21:21
  • It is the "The Token Stack" section. Read it and check some bundled sniffs. – zerkms Mar 09 '15 at 21:29
  • Well, I clearly see the "abstract" in the token stream: http://ideone.com/pH4DG8 "Have I not jumped through enough hoops yet for my question to be answered?" --- your question is **TOO VAGUE**. `abstract` **IS** in the token stream. You provided NOTHING for what you currently have. "found nothing useful" --- is not useful, see the link - it **IS THERE**. – zerkms Mar 09 '15 at 23:11
  • I spent couple more minutes and found example in the phpcs source: https://github.com/squizlabs/PHP_CodeSniffer/blob/master/CodeSniffer%2FStandards%2FPSR2%2FSniffs%2FClasses%2FClassDeclarationSniff.php – zerkms Mar 09 '15 at 23:15
  • Please provide the source class, the corresponding token stream and the sniff (since I cannot see how it's possible for a token to not be presented in the stream). Also please check the links in both my comments. "My question is too vague?" --- it is. "How can I identify if current Sniff is sniffing an abstract class or not?" --- check if the `abstract` token is in the token stream. – zerkms Mar 09 '15 at 23:16
  • "You've spent more time in comments than it would have taken just to provide an answer" --- the answer was provided in the very first comment, 2 hours ago :-S Is this correct to use, and reliable?" --- It is (if you also want to check if it's `final`). "Please don't tell me to RTFM to find out" --- I never did - I always provided the accurate links to what you exactly asked. – zerkms Mar 09 '15 at 23:37
  • @zerkms thanks for the help. I've deleted my comments here as they only got us to the answer and don't really add anything of value to anyone else viewing this Q&A. – James Mar 10 '15 at 06:25

1 Answers1

1

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.

Community
  • 1
  • 1
James
  • 4,644
  • 5
  • 37
  • 48