Page MenuHomePhabricator

Add sniff to detect class_exists( 'string' )
Open, MediumPublic

Description

Add sniff to detect class_exists( 'string' ) and optionally replace with class_exists( string::class )

https://codesearch.wmflabs.org/search/?q=class_exists%5C(%20%27.*%3F%27%20%5C)&i=nope&files=&repos=

Event Timeline

Could conflict with the extension registration replacements - T183096

Certainly useful in core, and it shouldn't be a "YOU MUST CHANGE THEM ALL" replacement..

thiemowmde triaged this task as Medium priority.May 31 2018, 1:00 PM
thiemowmde subscribed.

Can we expand this ticket to all other cases with the same problem?

  • Strings like 'TheClass::theMethod' can be replaced with either [ TheClass::class, 'theMethod' ] or TheClass::class . '::theMethod', depending on the context, e.g. in method_exists or is_callable.
  • array_map and many other PHP functions accept callbacks. If a string like above is detected, it might be worth replacing it.
  • new ReflectionMethod( TheClass::class, … )
  • PHPUnit:
    • $this->createMock( TheClass::class ), as well as getMock and getMockBuilder, as long as they exist.
    • setExpectedException
    • assertInstanceOf, assertContainsOnlyInstancesOf, and isInstanceOf
  • $maintClass = TheClass::class in maintenance scripts.
  • Assert::parameterType( TheClass::class ) and a few more from our very own assert component.

There is also stuff like 'class' => TheClass:class in API and ResouceLoader module registration and such, but it's probably super-hard to detect these in an automatic sniff.

Change 494036 had a related patch set uploaded (by Umherirrender; owner: Umherirrender):
[mediawiki/tools/phan@master] [PoC] Add ClassNameResolutionPlugin

https://gerrit.wikimedia.org/r/494036

Change 494036 abandoned by Umherirrender:
[PoC] Add ClassNameResolutionPlugin

https://gerrit.wikimedia.org/r/494036