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

Context::inForeachCondition(): bug fix - improve handling of array declaration in foreach #617

Closed jrfnl closed 1 month ago

jrfnl commented 1 month ago

PHP allows for not just having an (array/iterable object) variable in the "iterable" (before as) part, it also allows for those to be passed as an expression.

While this is surely more rare, it does mean that it is possible for an as keyword to exist in the expression.

This commit makes the Context::inForeachCondition() method more robust by explicitly skipping over array declarations while searching for the as keyword belonging to the foreach. This fixes the bug.

Includes tests.