Skip to content

Use function/const not tokenized as T_STRING when preceded by comment #2431

Closed
@jrfnl

Description

@jrfnl

The function and const keywords used in use statements are normally tokenized as T_STRING.

However, when there is a comment between the use keyword and the function/const keyword, this fails and they are tokenized as T_FUNCTION/T_CONST.

Example code where the tokenizer correction fails:

use /* comment */ function bar\math\{
    Msin,
    level\Mcos as BarCos,
    Mcosh,
};

use // phpcs:ignore Standard.Category
    const
	    bar\math\{ BGAMMA as BAR_GAMMA, BGOLDEN_RATIO };

Relevant code in the Tokenizer:

// This is a special case for PHP 5.6 use function and use const
// where "function" and "const" should be T_STRING instead of T_FUNCTION
// and T_CONST.
if (($newToken['code'] === T_FUNCTION
|| $newToken['code'] === T_CONST)
&& ($finalTokens[$lastNotEmptyToken]['code'] === T_USE || $insideUseGroup === true)
) {
$newToken['code'] = T_STRING;
$newToken['type'] = 'T_STRING';
}

As that code appears to look at the $lastNonEmptyToken, I'm not sure how to fix this....

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions