squizlabs / PHP_CodeSniffer

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

PHP 8.3 | Generic/ScopeIndent: bug fix - missing defensive coding #3857

Closed jrfnl closed 9 months ago

jrfnl commented 1 year ago

Description

As of PHP 8.3, PHP will throw a Warning: Decrement on type bool has no effect, this will change in the next major version of PHP notice.

A test run with PHP 8.3 showed this deprecation notice being thrown in the Generic.WhiteSpace.ScopeIndent sniff.

Investigation of the notice showed that this was actually a bug due to too little defensive coding.

The sniff tries to skip over multi-line/multi-token text strings, but the findNext() will return false for a single-line/single-token text string, which would lead to $i being reset to 0.

This commit fixes this by only changing $i when the return from the call to findNext() is not false.

Ref: https://wiki.php.net/rfc/saner-inc-dec-operators

Note: I've not added any extra tests as this issue was discovered via the pre-existing tests, so is already covered.

Suggested changelog entry

Generic.WhiteSpace.ScopeIndent: bug fix preventing a deprecation notice on PHP 8.3

Types of changes

jrfnl commented 9 months ago

Closing as replaced by https://github.com/PHPCSStandards/PHP_CodeSniffer/pull/76

jrfnl commented 8 months ago

FYI: this fix is included in today's PHP_CodeSniffer 3.8.0 release.

As per #3932, development on PHP_CodeSniffer will continue in the PHPCSStandards/PHP_CodeSniffer repository. If you want to stay informed, you may want to start "watching" that repo (or watching releases from that repo).