msstc4php / phpstan-rules
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
Requires
- php: >=8.2
- phpstan/phpstan: ^2.1
- spaze/phpstan-disallowed-calls: ^4.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.55
- phpstan/extension-installer: ^1.4
- phpunit/phpunit: >=10.5
Suggests
None
Provides
None
Conflicts
None
Replaces
None
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— forbidsPresentation/andApiPlatform/code from calling state-changing methods onDomain/Entity/andDomain/Document/classes. Only getter-/setter-like methods (get*,is*,has*,set*followed by[A-Z_], plus__toString/__get) are allowed.Plugs the gap that
deptraccannot cover: thePresentation → Domainedge 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→ usePsr\Log\LoggerInterface), - hard exits (
exit,die), - weak randomness (
rand,uniqid), - Doctrine ORM/ODM/DBAL internals leaking outside
Infrastructure/, Symfony\Component\HttpFoundation\Requestleaking intoApplication/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