Page MenuHomePhabricator

Update Translate to use PHP 8.3 features
Open, LowPublic

Description

What needs to be prioritized:

Update the Translate extension code to use PHP language features from versions 8.0 to 8.3. The extension now requires MediaWiki 1.46, which requires PHP 8.3, so all these features are safe to use.

A quick code review shows constructor property promotion and readonly properties are already widely used. The remaining work is:

  • Replace class constants that act as fixed value sets with enums (only 1 enum exists today)
  • Use match instead of switch where it fits, and ?-> instead of null checks (both barely used today)
  • Add types to class constants (~200 constants are untyped)
  • Add #[\Override] to methods that override a parent method (not used today)
  • Replace doc-comment-only types (@param, @return unions) with native types
  • Use first-class callable syntax instead of string/array callables

This work is complete when the features above are used consistently in src/, PHPCS/phan rules enforce them where sniffs exist, and CI passes on PHP 8.3 without warnings.

Why this is essential:

  • Native types let phan catch bugs that doc-comment types cannot
  • Enums and match make state handling type-safe instead of string-based
  • #[\Override] and typed constants catch silent breakage when MediaWiki core classes change
  • Less boilerplate makes the code easier to read and review

What happens if we do not do it now:

  • The code stays split between old and new styles, which makes reviews inconsistent
  • Bugs that native types would catch stay invisible to static analysis
  • The modernization backlog grows with each PHP version bump, so the eventual cleanup gets bigger and riskier
  • PHP 8.2/8.3 deprecation warnings surface as production log spam later

Dependencies / constraints:

  • Turning constants into enums changes the public interface.

Quick review by Claude:

  • Mostly done: constructor property promotion (~445 promoted parameters, from the January 2026 patch) and readonly properties (116 files)
  • Barely started: enums (1 file), match (1 file), nullsafe ?-> (5 files)
  • Not started: typed class constants (206 constants untyped), #[\Override], first-class callable syntax

Event Timeline

We'll also have to see if MW 1.44 will drop support for PHP 7.4.

Release support for PHP 7.4 and 8.0 was already dropped in MW 1.42; both 1.42 (b3990e92902df845705e8ec6d8379b330c0d3ede) and 1.43 (5f1bb64d89e2d86146937fdcbeb0c533caa83391) received patches after branching from master that made it impossible to use them on PHP 7.4/8.0. So I’m pretty sure the same will happen with 1.44 as well (if the support isn’t dropped in master before it branches off).

Change #1224898 had a related patch set uploaded (by Thiemo Kreuz (WMDE); author: Thiemo Kreuz (WMDE)):

[mediawiki/extensions/Translate@master] Use PHP8 constructor property promotion

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

Change #1224898 merged by jenkins-bot:

[mediawiki/extensions/Translate@master] Use PHP8 constructor property promotion

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

abi_ renamed this task from Update Translate to use PHP 8.1 features to Update Translate to use PHP 8.3 features.Jul 9 2026, 5:54 AM
abi_ updated the task description. (Show Details)

Thanks for the update.

This work is complete when the features above are used consistently in src/

Are we intentionally ruling out PHP code outside of src? There are some classes still in scripts/ messagegroups/ and of course tests/ which lies outside of src/.

There are some classes still in […] messagegroups/ […].

Is there any particular reason T340723: Modernize code under messagegroups/ directory was not done, or just lack of time? Finishing it would make PHPCS/Phan configuration simpler.

  • Replace class constants that act as fixed value sets with enums (only 1 enum exists today)

Enums are great for type safety (I was the one who advocated for that one enum), but unlike the other improvements listed, they actually increase boilerplate: due to how they are designed and how PSR-4 autoloading works, they require separate files rather than just a few lines in an existing class. Type safety may not be worth the boilerplate, especially if constants are used only within a class (private method parameters, private properties etc.).

  • Replace doc-comment-only types (@param, @return unions) with native types

When doing this, be aware that some types shouldn’t be used as type declarations. Translate already does use some of these as type declarations (e.g. Title is used a lot), but preferably this wouldn’t spread further. And, of course, one should be aware that the PHPDoc types can lie, so care should be taken – it’s good that type declarations make discrepancies visible in production by crashing, but it’s even better if these crashes are prevented in the patch introducing type declarations.

  • Use first-class callable syntax instead of string/array callables

Be aware that string/array callables are serializable but first-class callables aren’t, so there can be places where we have to keep the string/array callables.

Nikerabbit lowered the priority of this task from Medium to Low.Jul 14 2026, 12:20 PM

There are some classes still in […] messagegroups/ […].

Is there any particular reason T340723: Modernize code under messagegroups/ directory was not done, or just lack of time? Finishing it would make PHPCS/Phan configuration simpler.

Just lack of time.

I see, thanks for the response!