PHPCSStandards / PHP_CodeSniffer

PHP_CodeSniffer tokenizes PHP files and detects violations of a defined set of coding standards.
BSD 3-Clause "New" or "Revised" License
880 stars 54 forks source link

AbstractPatternSniff: prevent PHP notice #579

Closed jrfnl closed 1 month ago

jrfnl commented 1 month ago

Description

Squiz/FunctionDeclaration: rename test case file

.... to allow for adding additional test case files.

AbstractPatternSniff: prevent PHP notice

In a live coding situation, the token triggering the pattern being looked for could be at or near the end of the file. This could lead to a situation where the pattern could never match anyhow as there are not enough tokens left in the file to match against.

In this situation, the sniff could trigger the following PHP error:

Increment on type bool has no effect, this will change in the next major version of PHP in path/to/phpcs/src/Sniffs/AbstractPatternSniff.php on line 627

This commit prevents this error by bowing out early if there are not enough tokens in the file under scan to match the pattern.

Tested via the Squiz.Functions.FunctionDeclaration sniff via which this issue was discovered.

Suggested changelog entry

AbstractPatternSniff: bug fix for potential PHP notice during live coding

Related issues/external references

Loosely related to #152 as this issue was discovered when creating an update for the fixer conflict list.

Types of changes

jrfnl commented 1 month ago

Rebased without changes. Merging once the build has passed.