Search by

msstc4php / phpstan-rules

maxim.shamaev@gmail.com

PHPStan rules and disallowed-calls defaults for Symfony bundles with a hexagonal layer layout.

Package info

github.com/msstc4php/phpstan-rules

Type:phpstan-extension

pkg:composer/msstc4php/phpstan-rules

Statistics

Installs: 274

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

v0.2.1 2026-12-04 11:00 UTC

This package is auto-updated.

Last update: 2026-12-04 11:01:30 UTC


README

PHPStan rules and disallowed-calls defaults for Symfony bundles that follow a hexagonal layer layout (Domain, DomainService, Application, Infrastructure, Presentation, ApiPlatform, Framework, DependencyInjection).

What's inside

Custom rule

  • PresentationCannotCallDomainMutatorsRule — forbids Presentation/ and ApiPlatform/ code from calling state-changing methods on Domain/Entity/ and Domain/Document/ classes. Only getter-/setter-like methods (get*, is*, has*, set* followed by [A-Z_], plus __toString / __get) are allowed.

    Plugs the gap that deptrac cannot cover: the Presentation → Domain edge is intentionally allowed (read access to data shape), but a rich domain entity exposes mutating methods like $order->place() that must not be invoked from outside Application.

Disallowed-calls defaults

A curated disallowedFunctionCalls + disallowedClasses set for spaze/phpstan-disallowed-calls, covering:

  • debug noise (var_dump, dd, dump, print_r, var_export),
  • logging boundaries (error_log → use Psr\Log\LoggerInterface),
  • hard exits (exit, die),
  • weak randomness (rand, uniqid),
  • Doctrine ORM/ODM/DBAL internals leaking outside Infrastructure/,
  • Symfony\Component\HttpFoundation\Request leaking into Application/Domain,
  • Symfony Security helper leaking into Application/Domain,
  • Service Locator anti-pattern (ContainerInterface).

allowIn rules assume that layer layout (src/Infrastructure/**, src/Presentation/**, …). Consumers with a different layout can override individual entries in their own phpstan-ci.neon.

Install

composer require --dev msstc4php/phpstan-rules

This pulls spaze/phpstan-disallowed-calls as a transitive dependency — do not list it separately.

phpstan/extension-installer automatically picks up extension.neon from this package, so the rule and the defaults activate immediately.

Override

To widen an allowIn for one entry, redeclare it in your project's PHPStan config:

parameters:
    disallowedClasses:
        -
            class: 'Doctrine\ORM\EntityManagerInterface'
            message: 'allowed for this legacy adapter only'
            allowIn:
                - 'src/Infrastructure/**'
                - 'src/Legacy/Bridge/**'
                - 'tests/**'

The last definition wins.

To disable a default, override it with an empty allowIn and a sentinel comment, or override the rule's parameters. Disabling is rare — prefer scoping allowIn.

Development

Inside this repository:

composer install
vendor/bin/phpunit
vendor/bin/phpstan analyse --memory-limit=512M

The rule's own test (tests/unit/Rules/PresentationCannotCallDomainMutatorsRuleTest.php) asserts the rule fires on a controlled fixture with 4 known violations and stays silent on the allowed cases.

Versioning

Semver. Patch-level changes never tighten existing rules. A new rule or a stricter default = minor bump. Removing a rule or breaking the extension.neon shape = major bump.

Layout

phpstan-rules/
├── composer.json              # type: phpstan-extension
├── extension.neon             # auto-loaded by phpstan/extension-installer
├── README.md
├── src/
│   └── Rules/
│       └── PresentationCannotCallDomainMutatorsRule.php
└── tests/
    └── unit/
        └── Rules/
            ├── PresentationCannotCallDomainMutatorsRuleTest.php
            └── Fixture/
                └── presentation_calls.php