You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
PHP 8.0 | Tokenizer/PHP: add support for match expressions in combination with arrow functions
As a match expression can be nested in an arrow function and visa versa, setting the scope openers/closers becomes _interesting_.
If the arrow function would be allowed to take over, it would break the scope setting for match expression, making the `scope_closer` and `scope_condition` indexes next to useless.
So in the interest of making things easier for sniff writers and keeping in mind that scope setting for arrow functions isn't perfect anyway, preference is given to keeping the scope setting for match structures intact.
This means that if a match expression is in the return value of an arrow function, like so:
```php
$fn = fn($x) => match($y) {...};
```
... the token _after_ the closing curly belonging to the `match` will be considered the scope closer for the arrow function. In this example, that is the `;` (semicolon).
While, if an arrow function is the return value of one of the match expression cases, generally speaking the comma at the end of the case will be the scope closer for the arrow function.
There is one exception to this: the comma after each case is optional for the last case in a match expression.
In that situation, the last non-empty token will be considered the scope closer for the arrow function.
Example:
```php
$match = match($y) {
default => fn($x) => $y * $x
};
```
In this example, the `$x` at the end of the arrow expression would be considered the scope closer for the arrow function.
```php
$match = match($y) {
default => fn($x) => ($y * $x)
};
```
And in this example, the parenthesis closer at the end of the arrow expression would be considered the scope closer for the arrow function.
0 commit comments