Page MenuHomePhabricator

ObjectFactory doesn't use strict_types (in a released version)
Closed, ResolvedPublic

Description

ObjectFactory (src/ObjectFactory.php) doesn't use declare( strict_types = 1 ); and so no objects constructors called by ObjectFactory will/can enforce strict typing. Arguments given to those constructors will always be weakly-coerced, potentially masking real errors and causing confusion while debugging.

For example: T438174: WatchlistExpiryJob is given a Title rather than a string as first parameter

Event Timeline

This was actually fixed upstream as part of T422722 but has not made it into a released version of ObjectFactory yet. It should be in ObjectFactory 7.0.0.

cscott renamed this task from ObjectFactory doesn't use strict_types to ObjectFactory doesn't use strict_types (in a released version).Sep 16 2026, 2:43 PM

Change #1342282 had a related patch set uploaded (by C. Scott Ananian; author: C. Scott Ananian):

[mediawiki/libs/ObjectFactory@master] Release v7.0.0

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

Change #1342282 merged by jenkins-bot:

[mediawiki/libs/ObjectFactory@master] Release v7.0.0

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

Tagged, needs to bump alongside Parsoid.

Change #1342329 had a related patch set uploaded (by Jforrester; author: Jforrester):

[mediawiki/services/parsoid@master] Require ObjectFactory 7.0.0+, with strict typing

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

Change #1342338 had a related patch set uploaded (by C. Scott Ananian; author: C. Scott Ananian):

[mediawiki/services/parsoid@master] Allow wikimedia/object-factory 7.0.0

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

Change #1342338 merged by jenkins-bot:

[mediawiki/services/parsoid@master] Allow wikimedia/object-factory 7.0.0

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

Change #1342352 had a related patch set uploaded (by C. Scott Ananian; author: C. Scott Ananian):

[mediawiki/libs/ParamValidator@master] Update wikimedia/object-factory to 7.0.0

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

Change #1342357 had a related patch set uploaded (by C. Scott Ananian; author: C. Scott Ananian):

[mediawiki/core@master] Update wikimedia/object-factory to v7.0.0

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

Change #1342329 abandoned by C. Scott Ananian:

[mediawiki/services/parsoid@master] Require ObjectFactory 7.0.0+, with strict typing

Reason:

Abandoned in favor of I3e8230e8ad2999f935b2af1a75a6d54cb4874799

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

Change #1342352 merged by jenkins-bot:

[mediawiki/libs/ParamValidator@master] Update wikimedia/object-factory to 7.0.0

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

This bug is invalid as written, because strict_types in the php file is not relevant for the code calling this class (like ObjectFactory).
The strict type only handles the code that is called from this class, that is mention in the linked bug with the sentence "function calls to php internal functions or on function calls within the library" or with "Strict typing applies to function calls made from within the file with strict typing enabled, not to the functions declared within that file. If a file without strict typing enabled makes a call to a function that was defined in a file with strict typing, the caller's preference (weak typing) will be respected, and the value will be coerced.".

So the strict type is not needed in the file as written in the task description, add the strict type to your php file and when any function called from your php file has type declaration, it would resolved strict.

So on the other side adding strict type is not a breaking change for any use of the library and does not need a major version bump, that is also the reason why no releases are requested for this change (as also written in the task).

Strict types does not apply to the object construction as mention in the change log (180aa507750a8b5ba7d363f78357621fddb3cc51)

BREAKING CHANGE: use strict_types during object construction

I don't think this is correct. ObjectFactory *calls methods in other code*, that's its entire thing -- ObjectFactory::getObjectFromSpec(), calls new $clazz(...$args) or $spec['factory'](...$args). *That* is the invocation which is affected by strict/weak typing. If ObjectFactory uses strict types, then that invocation is done with strict types. "Function calls made from within this file", "this file" being ObjectFactory.php. That is a major-version-worthy change.

The PHP semantics are confusing, for sure, but that's how it works. It is the *calling file* which determines the strictness, and in this case the *calling file* is ObjectFactory.php when the object specification is constructed.

This is an unusual property of ObjectFactory in particular which is not common in most libraries. Most libraries you (mostly!) call *into*. ObjectFactory exists to call *out* in order to construct your own objects.

Change #1343538 had a related patch set uploaded (by Isabelle Hurbain-Palatin; author: Isabelle Hurbain-Palatin):

[mediawiki/vendor@master] Bump wikimedia/parsoid to 0.24.0-a24

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

Libraries usually only calls own code or php internal functions, but ObjectFactory is a bit different as it also calls back into non-library code via provided factory or class constructor and yes, for this calls the strict types makes a different. So the release notes is correct.

Change #1343538 merged by jenkins-bot:

[mediawiki/vendor@master] Bump wikimedia/parsoid to 0.24.0-a24

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

Change #1342357 merged by jenkins-bot:

[mediawiki/core@master] Update wikimedia/object-factory to v7.0.0

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

Jdforrester-WMF claimed this task.
Jdforrester-WMF reassigned this task from Jdforrester-WMF to cscott.