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

Various sniffs: simplify skipping the rest of the file #2506

Merged
merged 1 commit into from
Nov 8, 2024

Conversation

jrfnl
Copy link
Member

@jrfnl jrfnl commented Nov 8, 2024

This commit updates various sniffs to use return $phpcsFile->numTokens instead of return ($phpcsFile->numTokens + 1).

If a sniff file contains 50 tokens, the last $stackPtr will be 49, so returning 50 will already get us passed the end of the token stack and the + 1 is redundant.

Includes changing a few plain return statements to return $phpcsFile->numTokens for efficiency.

This commit updates various sniffs to use `return $phpcsFile->numTokens` instead of `return ($phpcsFile->numTokens + 1)`.

If a sniff file contains 50 tokens, the last `$stackPtr` will be 49, so returning `50` will already get us passed the end of the token stack and the `+ 1` is redundant.

Includes changing a few plain `return` statements to `return $phpcsFile->numTokens` for efficiency.
@GaryJones
Copy link
Member

If a sniff file contains 50 tokens, the last $stackPtr will be 49

Because it's a 0-indexed count?

@GaryJones GaryJones merged commit a615540 into develop Nov 8, 2024
46 checks passed
@GaryJones GaryJones deleted the feature/minor-simplification branch November 8, 2024 12:53
@jrfnl
Copy link
Member Author

jrfnl commented Nov 8, 2024

If a sniff file contains 50 tokens, the last $stackPtr will be 49

Because it's a 0-indexed count?

Mixing two things here, but yes ;-) The token stack indexes start at 0. A count() always counts from 1.

So a token stack containing 50 items, would have indexes 0 to 49, but the count of that token stack would be 50 ($phpcsFile->numTokens).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants