Skip to content

tooling: service constructors only store their dependencies #83

Closed

Description

@schlessera

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.constructorsOnlyAssign for classes implementing Service: the constructor body holds only assignments of a parameter to a property, poka-yoke defaults ($this->x = $x ?? new Default(), as newOnlyWhereAllowed defines 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 $locations argument, and the default locations (get_default_locations(), which reads get_stylesheet_directory() and get_template_directory()) are resolved on first use in create(), then kept for later calls. No signature changes. This is the maintainer's ruling (option 1), so constructorsOnlyAssign.allowed stays 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 an if, fail with the full message asserted; plain assignments, a poka-yoke default and a parent::__construct() pass.
  • Both samples pass; composer lint is green.
  • TemplatedViewFactory passes the rule with no allowance; a unit test shows the theme directories are not read at construction and are read once, on the first create(), and that explicitly passed locations are used unchanged.

Verification

composer test
composer lint

Impact 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

  1. added
    type: test
    area: tooling
    priority: p3
    on Oct 23, 2026
  2. added
    agent-ready
    on Oct 23, 2026
  3. schlessera commented on Oct 23, 2026

    @schlessera
    MemberAuthor

    Found while writing docs/patterns.md (#95): TemplatedViewFactory implements Service and its constructor does more than assign (src/Infrastructure/View/TemplatedViewFactory.php:43-49: an if that falls back to get_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 in create()), or limit the rule to feature subsystems; decide in this issue before landing the rule.

  4. schlessera commented on Oct 23, 2026

    @schlessera
    MemberAuthor

    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

    • Principle 7 and the principle-7 table: service constructors only store their dependencies.
    • The rule applies to classes implementing Service. TemplatedViewFactory implements Service (src/Infrastructure/View/TemplatedViewFactory.php:21), and its constructor has an if that calls get_default_locations(), which calls get_stylesheet_directory() and get_template_directory() (lines 43-49 and 70-80). The rule as scoped reports it.
    • constructorsOnlyAssign.allowed is empty by default, so the scaffold must not ship an exception for its own infrastructure.
    • The docblock on 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, but docs/patterns.md and tests/php/Integration/TemplatedViewFactoryTest.php refer to the factory.

    Options

    1. Resolve the defaults lazily. Store the passed $locations (possibly []) in the constructor and resolve the default list on first use in create(). 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 not breaking; the constructor and create() keep their shape. It needs a small cache property or a per-call lookup. Fits principles 3 (poka-yoke default stays) and 7.
    2. Move the default into a poka-yoke at the composition root. BasicScaffoldPlugin builds the factory with new 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.
    3. Limit the rule to feature subsystems (everything outside 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.
    4. Ship MWPD\BasicScaffold\Infrastructure\View\TemplatedViewFactory::__construct in the default allowed list. 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 Service classes, 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.

  5. added
    needs: decision
    agent-ready
    and removed
    agent-ready
    needs: decision
    on Oct 23, 2026
  6. schlessera commented on Oct 23, 2026

    @schlessera
    MemberAuthor

    Decision (maintainer, 2026-10-23): option 1. TemplatedViewFactory resolves its default locations lazily on first use in create(), so the constructor only stores its arguments, the rule applies uniformly to every service, and constructorsOnlyAssign.allowed stays 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.

  7. self-assigned this
    on Oct 23, 2026
  8. schlessera commented on Oct 23, 2026

    @schlessera
    MemberAuthor

    Picking this up on branch test/83-constructors-only-assign.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

agent-readyarea: toolingpriority: p3type: test

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions