In the codebase of one of my clients I see a lot of references to a qualified class name as a string;
[
'foobar' => 'My\Namespace\Class'
]
Instead of using:
[
'foobar' => My\Namespace\Class::class
]
For a couple of reasons we want to add a PHP CodeSniffer rule to catch these strings and add a warning so the string can be refactored to the ::class constant. The first part (catching the string) is easy, but because we are doing static code analysis we can't do (for example) a class_exists or looking up the results of get_declared_classes().
Next option could be analyzing the string itself ([A-Za-z0-9]), but this is not very reliable because a lot of strings will match but are not meant to be a classname.
Another option is to first 'collect' all the classnames (based on T_CLASS token) and analyze all strings after that based on the collected list of classes. Not very easy to implement IMHO because CodeSniffer works on a per-file basis.
Last option i could think about is also quite dirty; because we always use composer in our projects we could take the autoloading files of composer and try to match against the classmaps and namespaces. Also not very reliable and clean.
Anybody with another suggestion we didn't think about?!