PHPCSStandards / PHPCSUtils

A suite of utility functions for use with PHP_CodeSniffer
https://phpcsutils.com/
GNU Lesser General Public License v3.0
53 stars 7 forks source link

Add more defensive coding / improve type checking #600

Closed jrfnl closed 3 months ago

jrfnl commented 4 months ago

Add more defensive coding against incorrect stack pointers being passed.

It is common to pass the result of a call to File::findPrevious() or File::findNext() to functions expecting a stack pointer, but these File functions can return false, which would be juggled to 0 when used in the typical isset($tokens[$stackPtr]) checks. This would then lead to that check passing, while the value should have been rejected, as the method may now try to act on a completely different token than intended (and more defensive coding should have been added to the originating sniff).

Adding a preliminary check to make sure the received parameter is an integer prevents this problem and should surface any such bugs in sniffs using the updated PHPCSUtils methods.

Includes tests.

jrfnl commented 3 months ago

Rebased without changes, other than squashing the "catch change/test" commit into the main commit. Marking as ready as #598 and #599 have been merged now. Merging once the build passes.