This document is the contract. Every rule below names what enforces it, and
tests/Arch/TheRulesAreRealTest.php fails when a rule names nothing that
carries its identifier, or when something enforces an identifier this document
does not hold. tests/Guards plants a violation of every rule in a throwaway
copy of the tree and fails when the rule does not report it.
Three machines enforce what follows, and they overlap on purpose:
Machine
Enforces
Fails at
PHPStan at level max, with disallowed-calls, ergebnis, shipmonk, type coverage, cognitive complexity and the rules in phpstan/Rules
what code may call, and how
static analysis
Pest (tests/Arch)
layers, shapes, names, the tests themselves
the test suite
composer-dependency-analyser
what the shipped code may depend on
the dependency check
The shape
src/
Core/ decides. Values and the steps that turn one into the next. No I/O.
Attribute/ #[Holds], which tests write and the gate reads from their tokens. Public API.
Port/ the interfaces the core asks. Public API.
Mutator/ the SDK a mutator is written against, on php-parser, and its testing kit. Public API,
but for the engine in Mutator/Engine.
Config/ the typed config and its builder. Public API.
Extension/ the Extension interface, the Extensions registry, and the kits that
hold an adapter to its port's contract. Public API, but for the
kits' checks in Extension/Checks.
Adapter/ one directory per outside thing: Pest, Infection, git, GitHub, S3.
Cli/ the composition root and the command line.
plugins/
<name>/ a first-party mutator set: a package in all but publication, with
its own composer.json, src and tests, which leaves by moving its
directory (ADR-0021).
phpstan/Rules/ this repository's own analyser rules.
tests/
Arch/ the rules in this document that the suite enforces.
Unit/ one test file per source file, mirroring src.
Process/ the tests that start a real process, mirroring src.
PHPStan/ the tests of the rules in phpstan/Rules, mirroring phpstan.
Contract/ one directory per port: its suite, run against its fake and every adapter,
through the port's kit where the package ships one.
Fakes/ a hand-written fake of every port.
Guards/ the planted violation of every rule.
Support/ what the Arch suite and the guards read the tree with, and what other tests share.
Docs/ the documentation held to the code.
Fixtures/ the projects, files and recorded output the tests read.
The layers are ordered: Core, Attribute, Port, Mutator, Config, Extension,
Adapter, Cli.
A layer names only itself and the layers before it, so nothing but an adapter
and the CLI can name an adapter, and the CLI is the only place adapters are
wired. Attribute names nothing but PHP and nothing else names it, because the
gate reads #[Holds] from a test's tokens without loading it.
The CLI never decides anything by an adapter's name. Where the flows must know
how a runner behaves, they ask the port: Runner::behaviour() answers a
RunnerBehaviour, which says whether the runner reads #[Holds] as it loads
its test files, whether a timeout's limit can be raised, which groups every
proof key reads, whether each shard pays its own opening run, and why a kill
matrix of its run holds first killers only. RunnerBehaviour::standard() is the answer of a runner that does none of
these differently.
A — layers
Rule
Says
Enforced by
A1
Core, Port, Mutator, Config and Extension name nothing outside the package but PHP and Psr\Clock, and Mutator, the SDK a mutator is written against, names php-parser too (ADR-0021)
arch: every class name each of their files writes
A2
A port is an interface
arch: every declaration under src/Port
A3
An adapter names no other adapter, but an adapter that is the composition root of code the gate writes into another program's process may name the adapters it hands in, each listed by file and name: Pest\Patch's hunks, which run in Pest's own process, name Process\LiveProcesses (ADR-0312)
arch: every class name each adapter's files write, and the list of roots is exactly what they name
A4
A layer names only itself and the layers before it
arch: every class name each file under src writes
A5
Attribute names nothing but PHP, and nothing else under src names it but the Pest plugin's filter, which reads #[Holds] from test closures (ADR-0004), the PhpSpec and Codeception runners' tests, which read it from specs, test classes and their methods, and the Testo runner's tests, which read it from a test's function and class (ADR-0027)
arch: every class name each file under src writes
A6
Nothing under src names a plugin (ADR-0021)
arch: every class name each file under src writes
A7
A plugin names only PHP, php-parser, the Mutator layer, Extension\Extension, Extension\Extensions and the core values the public API's signatures reach, and never another plugin (ADR-0021)
arch: every class name each file under plugins/*/src and plugins/*/tests writes
A8
Each plugin's composer.json validates and is normalized on its own, names the plugin nightworksio/mutation-gate-<directory>, maps its namespaces, lists an extension the root lists too, and requires the gate, php-parser and every package its code names; its package is listed in Core\Registry\FirstPartyPackage (ADR-0021, ADR-0023)
arch: each plugin's manifest and every class name its src writes; ci: composer validate and composer normalize over each plugin's manifest
B — input and output
The core is handed everything it reads. An adapter or the CLI reads the world
and hands the core a value, which is what lets every decision be tested
without a repository, a runner or a CI.
Rule
Says
Enforced by
B1
Time is read through Psr\Clock\ClockInterface; only an adapter or the CLI reads the system clock
phpstan disallowed-calls, scoped by path
B2
Nothing in src draws a random number, but the seed a forked child gives PHP's generator, so it runs as a fresh process would
phpstan disallowed-calls
B3
The filesystem and the standard streams belong to adapters and the CLI
phpstan disallowed-calls, scoped by path
B4
Running a program belongs to adapters
phpstan disallowed-calls, scoped by path
B5
The network belongs to adapters
phpstan disallowed-calls, scoped by path
B6
The environment is read by the CLI and the adapters only
phpstan disallowed-calls, scoped by path
B7
Only an adapter waits
phpstan disallowed-calls, scoped by path
C — refusals and absence
Rule
Says
Enforced by
C1
A port answers with a value or an outcome, never with nothing
arch: no port method answers void
C2
No null crosses the public API, in either direction, and no type in the core is nullable
arch: every public method of the API surface, and every method and property in the core
C3
Every thrown exception is the package's own
phpstan disallowed-calls on the bare exceptions' constructors
No empty catch, and no Throwable or Exception caught without rethrowing, except where a file boots a project's own application (allowIn)
phpstan: own rule
C7
No empty()
phpstan: own rule
C8
No ?-> in the core
phpstan: own rule, scoped by path
C9
No nested ternary, and no ?? on an array subscript outside the tests
phpstan: own rule
D — types
The API surface is every class in Attribute, Port, Mutator but its engine
(Mutator\Engine), Config and Extension, and every core type their public
faces reach: their parents and interfaces, and every type their public methods
and properties write, natively or in a doc block (ADR-0001, ADR-0043). It is
what semver covers of the code.
Rule
Says
Enforced by
D1
No array in a public signature on the API surface
arch: every public method of the API surface
D2
A string, int or float is taken only by a named constructor on the API surface, or by an attribute's constructor, which PHP calls with what the attribute is written with
arch: every public method of the API surface
D3
Nothing is untyped, and nothing on the API surface is mixed
phpstan: type coverage at 100, plus arch for mixed itself
D4
A closed set is an enum, not a class named for being one
arch: no class named Status, State, Type or Kind
D5
No bare true or false at a call site
phpstan: own rule
D6
No unnamed number in a method body
phpstan: own rule
D7
Every class is final, and every class on the API surface or in the core that is not an exception is readonly
arch: reflection over every class under src
D8
A closed set of strings is an enum: no match over three or more strings, and no constant of three or more strings that in_array or array_key_exists asks about, outside the files that translate an outside vocabulary
phpstan: own rule, over src; review for the rest
D9
One value has one home: a value two classes declare as constants, whole or as an item of an array constant, is declared once and referred to, unless the two only coincide
phpstan: own rule with a collector, over src; arch: every exemption names a file or a constant that is there
D10
mixed appears only in a decoder of untrusted input, which hands on typed values, or in a class whose signatures a PHP protocol fixes, which holds what PHP hands it; the files still to type are listed, and the list only shrinks
phpstan: own rule, over src and tests/Support; arch: every listed file still holds a mixed
D11
A public property is readonly, but for one PHP itself writes
phpstan: own rule, with those files named in phpstan.neon
D12
A parameter that two classes hand string literals takes an enum or a constant they share, but for a method whose parameters take prose, keys or an outside vocabulary; those methods are listed, and the list only shrinks
phpstan: own rule with a collector, over calls under src to methods declared there, which on a run over every configured path refuses a listed method no two classes need any more; arch: every listed method is there
D13
A type outside the API surface says @internal in its doc block, and a type on it does not (ADR-0043)
arch: every class-like under src and each plugin's src, against the API surface computed
H — names, size and layout
Rule
Says
Enforced by
H1
No Manager, Helper, Util, Service, Data or Info suffix
arch
H2
No Interface suffix and no Abstract prefix
arch
H3
A class declares at most twenty methods, but for one whose methods a PHP protocol fixes, and nothing is too complex to read
phpstan: own rule, with those files named in phpstan.neon, + cognitive_complexity
H4
A test file mirrors the source file it tests, or, split by concern under the line cap, sits in a directory named for it
arch: an orphan test fails, a class without one does not
H5
A string with a value in it is built with sprintf, and a message is one literal
phpstan: own rule, one per node type
H6
An exception is named for what happened, not for being an exception
arch
H7
A test is named for the behaviour it pins, never with an identifier
arch: every test description
H8
A method returns from at most three places
phpstan: own rule
H9
A list is on one line or fully split, as SonarCloud's S1808 requires
phpstan: own rule, over src
H10
No line in src is longer than 120 characters, as SonarCloud's S103 requires
phpstan: own rule, over src
H11
A class, interface, trait or enum is named in letters only, starting with a capital, as SonarCloud's S101 requires
phpstan: own rule
W1
A file under src declares one class, and it is the one its path names
arch
K — comments
Rule
Says
Enforced by
K1
A comment states what is true now; it tells no history and makes no promise
arch: the markers of history and promise, over every comment; review for the rest
P, Q, L — the language
Rule
Says
Enforced by
P1
No __get, __set, __isset, __unset, __call or __callStatic
phpstan: own rule
P2
No variable variable and no dynamic class, method or property name, but for the three files that construct the extension classes Composer names, the classes a config names and the mutators a set registers by class
phpstan: own rule, with those files named in phpstan.neon
P3
No func_get_args(), no #[AllowDynamicProperties]
phpstan disallowed-calls
P4
No reflection in src, but for the Pest plugin's filter, which reads #[Holds] from test closures (ADR-0004)
phpstan disallowed-calls, scoped by path
Q1
Nothing reconfigures the runtime: ini_set, setlocale, error handlers
phpstan disallowed-calls
Q3
No static:: and no new static
phpstan: own rule
Q4
No echo or print in src
phpstan: own rule
L3
Multibyte-safe string functions only
phpstan disallowed-calls
S — security
Rule
Says
Enforced by
S1
The dangerous, execution, insecure and non-timing-safe call bundles are on
phpstan disallowed-calls, the four shipped bundles
S2
No package with a published advisory resolves
roave/security-advisories + composer audit --locked in ci
S3
A signal reaches another process only through Core\Runner\SparedProcesses, whose refusals spare the system's first process, the gate itself, every process above it and their groups and session: only it calls the raw send RunningProcesses::signalledUnspared, which Process\LiveProcesses sends by posix_kill, or by the kill program where PHP has no posix and pcntl (ADR-0310, ADR-0312)
arch: every method call each file under src and each PHP script under bin makes, and the list of senders is exactly what calls it
Secrets and vulnerable dependencies are scanned in CI by gitleaks and
osv-scanner, which read the repository rather than the code.
G — tests
Rule
Says
Enforced by
G1
No mocking library; every port has a hand-written fake
arch: over the text of every test
G2
Every port has a fake in tests/Fakes and one contract suite in tests/Contract/<Port>, run against the fake and every adapter
arch: every interface under src/Port
G4
No dev dependency is reachable from src, and every dependency is used
composer-dependency-analyser
G5
One assertion idiom: Pest's expect(), never PHPUnit's assert*
arch: over the text of every test
G6
No committed ->only(), and no ->skip() without the reason
arch: over the text of every test
G7
Every line of src and of each plugin's src is covered, but the files scripts/coverage-exemptions.php names
ci: the coverage job, by scripts/process-coverage.php
G8
Every mutant of src and of each plugin's src is killed, or its tree holds the floor mutation-gate.baseline.json records
ci: the mutation job, the package's own gate through its reusable workflow and action
G9
A diagnostic fails the run, and no setting exempts one
arch: the settings, read out of phpunit.xml
G10
Every test file is inert: loading it runs nothing, so a mutant's narrowed run need not load it (ADR-0004)
arch: every test file, read as the Pest adapter reads it
G11
The package's own floors only rise: they live in mutation-gate.baseline.json alone, none carries lowered, and mutation-gate.json declares none (ADR-0003)
arch: mutation-gate.json and mutation-gate.baseline.json, read; ci: the mutation job fails a tree below its floor, and a floor lower than the base branch's
V — CI and the bot
The contributor bot explains a red CI job only in words from tables in
main, filled with what the job's own tools reported, and runs nothing a
pull request brings where it holds a token that can write (ADR-0019).
Rule
Says
Enforced by
V1
Every CI job has an entry in .github/gates.json saying what it checks and how to reproduce it, and every entry is a job
arch: .github/gates.json against the workflows
V2
Every CI job writes and uploads its evidence and ends by explaining its failure with gate_summary.py, or its entry says why it leaves none
arch: .github/gates.json against the workflows
V3
Every rule the recorded evidence names, and every step that can fail with none, has its explanation in .github/bot/rules, ARCHITECTURE.md or its gate's rules
arch: tests/Fixtures/Evidence, read by evidence.py
V4
The generated rule tables are what the locked tools say of their rules
arch: scripts/bot-rules run and compared; the hygiene / markdown job for markdownlint
V5
A job of a bot workflow (bot-*.yml), whatever starts it, and a job that issue_comment, issues, pull_request_target or workflow_run starts, names the permissions it holds, under a workflow whose own are {}, and runs only a checkout of main, the stdlib scripts and the pinned token action, with no expression in a run
arch: the workflows, read
R — the rules themselves
Rule
Says
Enforced by
R1
Every rule here names an artifact carrying its identifier, and every identifier an artifact carries is here
test: TheRulesAreRealTest
R2
Every rule refuses a planted violation
test: the Guards suite
R3
The analyser, the refactorer and the Arch suite read the same trees