Repository navigation
Description
Context
The principle-7 table says constructors only store dependencies. noHooksInConstructor catches hook calls, but a service constructor can still call get_option(), query the database, read WordPress state or do any other work, and that work then runs whenever the injector builds the service, including when it is built only to be stored lazily. Both samples follow the pattern: src/SampleSubsystem/SampleBackendService.php:60-66 and src/SampleSubsystem/SampleLoopService.php:81- only assign.
Scope
- A PHPStan rule
architecture.constructorsOnlyAssignfor classes implementingService: the constructor body holds only assignments of a parameter to a property, poka-yoke defaults ($this->x = $x ?? new Default(), asnewOnlyWhereAlloweddefines them), and comments. - Configured exceptions in
constructorsOnlyAssign.allowed(Class::__construct), empty by default. - The message says to move the work into
register()or a callback, or into a factory if it is construction logic. TemplatedViewFactory(src/Infrastructure/View/TemplatedViewFactory.php:43-49,:70-80) stops computing its default locations in the constructor: the constructor only stores the optional$locationsargument, and the default locations (get_default_locations(), which readsget_stylesheet_directory()andget_template_directory()) are resolved on first use increate(), then kept for later calls. No signature changes. This is the maintainer's ruling (option 1), soconstructorsOnlyAssign.allowedstays empty.
Out of scope
- Value objects and exceptions, which validate in their constructors.
- The infrastructure's own classes that are not services (
SimpleInjector,ServiceBasedPlugin). - Parent constructor calls in services that extend a WordPress class are allowed (
parent::__construct( … )with parameters).
Acceptance criteria
- Fixtures: a service constructor calling
get_option(), and one with anif, fail with the full message asserted; plain assignments, a poka-yoke default and aparent::__construct()pass. - Both samples pass;
composer lintis green. -
TemplatedViewFactorypasses the rule with no allowance; a unit test shows the theme directories are not read at construction and are read once, on the firstcreate(), and that explicitly passed locations are used unchanged.
Verification
composer test
composer lintImpact on generated plugins
Tests and tool configuration, plus one internal timing change: TemplatedViewFactory reads the theme directories on the first create() instead of at construction. No interface, signature or convention generated plugins rely on changes. Settings read the plugin's namespace and prefixes from configuration, so a generated plugin keeps the check after renaming.
Updated 2026-10-23: folded in the maintainer's ruling (option 1, resolve TemplatedViewFactory's default locations lazily in create()); added it to the scope, acceptance criteria and impact.
Activity
schlessera commentedon Oct 23, 2026
Found while writing docs/patterns.md (#95):
TemplatedViewFactoryimplementsServiceand its constructor does more than assign (src/Infrastructure/View/TemplatedViewFactory.php:43-49: anifthat falls back toget_default_locations(), which calls WordPress functions). The rule as scoped would report it. Either move the default-locations lookup out of the constructor (for example, resolve it lazily increate()), or limit the rule to feature subsystems; decide in this issue before landing the rule.schlessera commentedon Oct 23, 2026
The comment above leaves one question open that the acceptance criteria depend on, so the rule cannot land until it is ruled on. This is the design proposal.
What is decided
Service.TemplatedViewFactoryimplementsService(src/Infrastructure/View/TemplatedViewFactory.php:21), and its constructor has anifthat callsget_default_locations(), which callsget_stylesheet_directory()andget_template_directory()(lines 43-49 and 70-80). The rule as scoped reports it.constructorsOnlyAssign.allowedis empty by default, so the scaffold must not ship an exception for its own infrastructure.get_default_locations()documents the current timing on purpose: the lookup runs once, at construction, and every factory reflects the theme active when it was created. That is not enforced by a test today, butdocs/patterns.mdandtests/php/Integration/TemplatedViewFactoryTest.phprefer to the factory.Options
$locations(possibly[]) in the constructor and resolve the default list on first use increate(). Trade-offs: the constructor becomes assignment only and the rule needs no exception. Default locations are then read when the first view is created rather than when the factory is built, so the "theme active at creation" wording changes to "theme active at first use". No signature changes, so it is notbreaking; the constructor andcreate()keep their shape. It needs a small cache property or a per-call lookup. Fits principles 3 (poka-yoke default stays) and 7.BasicScaffoldPluginbuilds the factory withnew TemplatedViewFactory( $locations )or the injector config computes the locations, and the constructor only assigns. Trade-offs: moves WordPress calls into the composition root, and a generated plugin that constructs the factory itself loses the default, which weakens principle 3. It is the most invasive for generated plugins.Infrastructure\). Trade-offs: no change to the factory, but principle 7's table is then only enforced for sample code, and infrastructure services that copy the pattern go unchecked. The issue's out-of-scope list already excludes infrastructure classes that are not services, so this would widen that exclusion to infrastructure services.MWPD\BasicScaffold\Infrastructure\View\TemplatedViewFactory::__constructin the defaultallowedlist. Trade-offs: simplest, but it contradicts "empty by default" and teaches that exceptions are acceptable for the scaffold's own code.Recommendation (not an approval)
Option 1. It keeps the rule uniform across all
Serviceclasses, keeps the poka-yoke default, changes no public signature and needs no exception. The only behavioural difference is when the theme directories are read, which I would document in the class docblock. If the maintainer prefers to keep "read at creation" semantics, option 3 is the fallback.Question for the maintainer: which option, and if option 1, is "resolved at first use" acceptable for the default locations?
Once ruled, I will record the decision here, fold it into the acceptance criteria (adding a criterion for the chosen option if it touches the factory) and restore
agent-ready.schlessera commentedon Oct 23, 2026
Decision (maintainer, 2026-10-23): option 1.
TemplatedViewFactoryresolves its default locations lazily on first use increate(), so the constructor only stores its arguments, the rule applies uniformly to every service, andconstructorsOnlyAssign.allowedstays empty by default. Reading the theme directories at first use instead of at construction is accepted. The scope and acceptance criteria in the body now include this.schlessera commentedon Oct 23, 2026
Picking this up on branch
test/83-constructors-only-assign.Add a rule that service constructors only store their dependencies (#83)
Add a rule that service constructors only store their dependencies (#126