Closed antonioeatgoat closed 1 year ago
@antonioeatgoat Eh... In my opinion, that would be a hard "no".
Arrow functions are anonymous by nature, so don't have a name, so a sniff should not try to retrieve one for it.
As far as I know, the ONLY reason closures are handled in the File::getDeclarationName()
function, is because PHP natively tokenizes those tokens as T_FUNCTION
and originally that meant there was no difference between the token for named and the token for anonymous functions.
Since then (and a long time ago now), PHPCS has updated the tokenizer to retokenize T_FUNCTION
tokens for closures to T_CLOSURE
. The only reason that T_CLOSURE
is still handled in the File::getDeclarationName()
is for BC reasons and - again IMO - the handling of T_CLOSURE
should be removed for PHPCS 4.x (actually, it should have been removed for 3.x).
Thank you @jrfnl for the context, I didn't know it, it makes more sense now.
Then I guess that the correct way is to check that position is an actual T_FUNCTION
before trying to get a declaration name.
If so this issue can be closed for me.
Describe the bug If you try to get the declaration name of an arrow function using the method
File::getDeclarationName()
, it will throws an exception.It looks like it is legit to use the method for a closure, since there is this condition here, so probably the same condition should be added also for
T_FN
.Custom code sniff sample
To reproduce Steps to reproduce the behavior:
test.php
which contain a simplefn() => 'x';
phpcs test.php ...
Expected behavior A condition for T_FN is added as explained above, so that the method
File::getDeclarationName()
can returnnull
instead of throwing an exception, when used on an arrow function.Versions (please complete the following information):