Skip to content

Tokenizer detects wrong token for php ::class feature with spaces #2138

@umherirrender

Description

@umherirrender

The following code:

class Foo {
}

if ( class_exists( Test :: class ) ) {
	Test :: callStuff();
}
if ( class_exists( Test2 ::class ) ) {
	Test2 :: callStuff();
}

the "class" in line 6 is token T_CLASS, while the "class" in line 9 is T_STRING. php lint is okay with a space there
This results in false positive on line 6 with the code ":: class" for some sniffs. The use of "::class" is okay in line 9.

  6 | ERROR | [ ] Only one object structure is allowed in a file
    |       |     (Generic.Files.OneObjectStructurePerFile.MultipleFound)
  6 | ERROR | [ ] Class name ")" is not in PascalCase format
    |       |     (Squiz.Classes.ValidClassName.NotCamelCaps)

It seems you have just to use $lastNotEmptyToken instead of -1 in PHP.php (search for classname::class)

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