Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve sniff implementation to be code style agnostic #2

Open
Rarst opened this issue Feb 19, 2020 · 1 comment · May be fixed by #5
Open

Improve sniff implementation to be code style agnostic #2

Rarst opened this issue Feb 19, 2020 · 1 comment · May be fixed by #5
Labels
bug Something isn't working help wanted Extra attention is needed

Comments

@Rarst
Copy link
Owner

Rarst commented Feb 19, 2020

Extracted from #1

Sniff code:

  • $method: you are presuming a code style. Best practice sniffs should be code style agnostic.
    While not very common, this will break on:
public function // some comment
   functionName() {}

Analyzer code:

  • $nextToken in isIncrementingToken() is not code style agnostic.

I am bad with parsers and have no idea how to approach it, so this might take a while. 😅

@Rarst Rarst added bug Something isn't working help wanted Extra attention is needed labels Feb 19, 2020
@Rarst Rarst self-assigned this Feb 19, 2020
@jrfnl
Copy link

jrfnl commented Feb 19, 2020

@Rarst Hopefully this will help:

Never presume code-style and do things like ($stackPtr + 1) if you want to examine the next effective token.
Always use findNext()/findPrevious() in combination with Tokens::$emptyTokens instead. That will disregard whitespace, comment and PHPCS native whitelist comments (which have a separate token).

As for the above:

To get the name of a function based on the T_FUNCTION token - use $phpcsFile->getDeclarationName(). That function exists for a reason ;-)

To get the next effective token in isIncrementingToken():

$nextToken = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true);
if ($nextToken === false || $tokens[$nextToken]['code'] !== T_SEMICOLON) {
    return true;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Extra attention is needed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants