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.
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.
Add more defensive coding against incorrect stack pointers being passed.
It is common to pass the result of a call to
File::findPrevious()
orFile::findNext()
to functions expecting a stack pointer, but theseFile
functions can returnfalse
, which would be juggled to0
when used in the typicalisset($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.