token_get_all incorrectly identifies matching named parameters as PHP keywords
Description
The following code:
$code ='<?php foo(echo: 4);';
$tokens = token_get_all($code);
var_dump(token_name($tokens[3][0]), $tokens[3][1]);
Resulted in this output:
string(6) "T_ECHO"
string(4) "echo"
But I expected this output instead:
string(8) "T_STRING"
string(4) "echo"
https://3v4l.org/Kr7R5#v8.1.2
PHP Version
PHP 8.1.2
Operating System
No response
This is correct. Tokenization (mostly) is context independent. The parser accepts identifiers as named params.
https://github.com/php/php-src/blob/706398fa9fb081c6385c66ac8af1fe952c80f17d/Zend/zend_language_parser.y#L875-L876
Which allow both T_STRINGs and semi_reserved.
https://github.com/php/php-src/blob/706398fa9fb081c6385c66ac8af1fe952c80f17d/Zend/zend_language_parser.y#L318-L325
By passing TOKEN_PARSE to the flags parameter, the parser "Recognizes the ability to use reserved words in specific contexts"
https://3v4l.org/DPalK#v8.1.2
Thanks, I totally missed that parameter.