Page MenuHomePhabricator

Adding namespaces to PHP `use` statements
Open, Needs TriagePublicFeature

Description

Feature summary (what you would like to be able to do and where):
If it's in scope for LibUp mediawiki-tools-codesniffer, consider adding a feature that mass replaces something like use Message; with use MediaWiki\Message\Message;. This would be in support of the work at T278278. We could feed it a map of unnamespaced classes => namespaced classes.

Example patch: https://gerrit.wikimedia.org/r/c/mediawiki/extensions/SecurePoll/+/1037849

Each MW version, we namespace a dozen or so more classes, and leave an alias behind for backwards compatibility. But it should be really easy to automate converting from the alias to the actual namespace.

I think phan will Verified-1 anything that we get wrong, so this should be a really safe refactor.

Should also add code to read and increase the version number in extension.json/skin.json, if needed.

Use case(s) (list the steps that you performed to discover that problem, and describe the actual underlying problem which you want to solve. Do not describe only a solution):

Benefits (why should this be implemented?):

  • reduce technical debt
  • save developer time
  • reduce IDE linter warnings
  • fix broken IDE type hinting

Notes

  • algorithm 1 - as described above. fix existing use statements
  • algorithm 2 - detect when there is no use statement at all and a namespaced class is used. the autofix will be to add the use statement. old extensions may not have any use statements at all because everything used to be globally namespaced, so use statements were not mandatory. example: CollaborationKit

image.png (1×2 px, 499 KB)

Event Timeline

I'm not interested in maintaining logic for this in LibUp itself (nor does it belong in there imo), but I guess something like this could be implemented as an auto-fixing PHPCS sniff which would have the same effect.

Thanks Taavi. I'll redo this ticket for mediawiki-tools-codesniffer instead. If I get positive feedback, maybe I'll write a phpcs rule patch for this.

Maybe we should do a mediawiki-tools-codesniffer rule that warns (rather than errors) when this is found, and has an autofix. That'd be a good way to get developer attention and autofixes without breaking CI.