Skip to the content
Howdah

being built · no release yet

Search

Skip the list of pages

Architecture

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:

MachineEnforcesFails at
PHPStan at level max, with disallowed-calls, ergebnis, shipmonk, type coverage, cognitive complexity and the rules in phpstan/Ruleswhat code may call, and howstatic analysis
Pest (tests/Arch)layers, shapes, names, the tests themselvesthe test suite
composer-dependency-analyserwhat the shipped code may depend onthe 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

RuleSaysEnforced by
A1Core, 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
A2A port is an interfacearch: every declaration under src/Port
A3An 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
A4A layer names only itself and the layers before itarch: every class name each file under src writes
A5Attribute 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
A6Nothing under src names a plugin (ADR-0021)arch: every class name each file under src writes
A7A 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
A8Each 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.

RuleSaysEnforced by
B1Time is read through Psr\Clock\ClockInterface; only an adapter or the CLI reads the system clockphpstan disallowed-calls, scoped by path
B2Nothing in src draws a random number, but the seed a forked child gives PHP's generator, so it runs as a fresh process wouldphpstan disallowed-calls
B3The filesystem and the standard streams belong to adapters and the CLIphpstan disallowed-calls, scoped by path
B4Running a program belongs to adaptersphpstan disallowed-calls, scoped by path
B5The network belongs to adaptersphpstan disallowed-calls, scoped by path
B6The environment is read by the CLI and the adapters onlyphpstan disallowed-calls, scoped by path
B7Only an adapter waitsphpstan disallowed-calls, scoped by path

C — refusals and absence

RuleSaysEnforced by
C1A port answers with a value or an outcome, never with nothingarch: no port method answers void
C2No null crosses the public API, in either direction, and no type in the core is nullablearch: every public method of the API surface, and every method and property in the core
C3Every thrown exception is the package's ownphpstan disallowed-calls on the bare exceptions' constructors
C4No @ suppressionphpstan: ergebnis NoErrorSuppressionRule
C5match, never switch; and no elsephpstan: ergebnis NoSwitchRule + disallowedControlStructures
C6No empty catch, and no Throwable or Exception caught without rethrowing, except where a file boots a project's own application (allowIn)phpstan: own rule
C7No empty()phpstan: own rule
C8No ?-> in the corephpstan: own rule, scoped by path
C9No nested ternary, and no ?? on an array subscript outside the testsphpstan: 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.

RuleSaysEnforced by
D1No array in a public signature on the API surfacearch: every public method of the API surface
D2A 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 witharch: every public method of the API surface
D3Nothing is untyped, and nothing on the API surface is mixedphpstan: type coverage at 100, plus arch for mixed itself
D4A closed set is an enum, not a class named for being onearch: no class named Status, State, Type or Kind
D5No bare true or false at a call sitephpstan: own rule
D6No unnamed number in a method bodyphpstan: own rule
D7Every class is final, and every class on the API surface or in the core that is not an exception is readonlyarch: reflection over every class under src
D8A 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 vocabularyphpstan: own rule, over src; review for the rest
D9One 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 coincidephpstan: own rule with a collector, over src; arch: every exemption names a file or a constant that is there
D10mixed 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 shrinksphpstan: own rule, over src and tests/Support; arch: every listed file still holds a mixed
D11A public property is readonly, but for one PHP itself writesphpstan: own rule, with those files named in phpstan.neon
D12A 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 shrinksphpstan: 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
D13A 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

RuleSaysEnforced by
H1No Manager, Helper, Util, Service, Data or Info suffixarch
H2No Interface suffix and no Abstract prefixarch
H3A class declares at most twenty methods, but for one whose methods a PHP protocol fixes, and nothing is too complex to readphpstan: own rule, with those files named in phpstan.neon, + cognitive_complexity
H4A test file mirrors the source file it tests, or, split by concern under the line cap, sits in a directory named for itarch: an orphan test fails, a class without one does not
H5A string with a value in it is built with sprintf, and a message is one literalphpstan: own rule, one per node type
H6An exception is named for what happened, not for being an exceptionarch
H7A test is named for the behaviour it pins, never with an identifierarch: every test description
H8A method returns from at most three placesphpstan: own rule
H9A list is on one line or fully split, as SonarCloud's S1808 requiresphpstan: own rule, over src
H10No line in src is longer than 120 characters, as SonarCloud's S103 requiresphpstan: own rule, over src
H11A class, interface, trait or enum is named in letters only, starting with a capital, as SonarCloud's S101 requiresphpstan: own rule
W1A file under src declares one class, and it is the one its path namesarch

K — comments

RuleSaysEnforced by
K1A comment states what is true now; it tells no history and makes no promisearch: the markers of history and promise, over every comment; review for the rest

P, Q, L — the language

RuleSaysEnforced by
P1No __get, __set, __isset, __unset, __call or __callStaticphpstan: own rule
P2No 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 classphpstan: own rule, with those files named in phpstan.neon
P3No func_get_args(), no #[AllowDynamicProperties]phpstan disallowed-calls
P4No reflection in src, but for the Pest plugin's filter, which reads #[Holds] from test closures (ADR-0004)phpstan disallowed-calls, scoped by path
Q1Nothing reconfigures the runtime: ini_set, setlocale, error handlersphpstan disallowed-calls
Q3No static:: and no new staticphpstan: own rule
Q4No echo or print in srcphpstan: own rule
L3Multibyte-safe string functions onlyphpstan disallowed-calls

S — security

RuleSaysEnforced by
S1The dangerous, execution, insecure and non-timing-safe call bundles are onphpstan disallowed-calls, the four shipped bundles
S2No package with a published advisory resolvesroave/security-advisories + composer audit --locked in ci
S3A 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

RuleSaysEnforced by
G1No mocking library; every port has a hand-written fakearch: over the text of every test
G2Every port has a fake in tests/Fakes and one contract suite in tests/Contract/<Port>, run against the fake and every adapterarch: every interface under src/Port
G4No dev dependency is reachable from src, and every dependency is usedcomposer-dependency-analyser
G5One assertion idiom: Pest's expect(), never PHPUnit's assert*arch: over the text of every test
G6No committed ->only(), and no ->skip() without the reasonarch: over the text of every test
G7Every line of src and of each plugin's src is covered, but the files scripts/coverage-exemptions.php namesci: the coverage job, by scripts/process-coverage.php
G8Every mutant of src and of each plugin's src is killed, or its tree holds the floor mutation-gate.baseline.json recordsci: the mutation job, the package's own gate through its reusable workflow and action
G9A diagnostic fails the run, and no setting exempts onearch: the settings, read out of phpunit.xml
G10Every 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
G11The 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).

RuleSaysEnforced by
V1Every CI job has an entry in .github/gates.json saying what it checks and how to reproduce it, and every entry is a jobarch: .github/gates.json against the workflows
V2Every CI job writes and uploads its evidence and ends by explaining its failure with gate_summary.py, or its entry says why it leaves nonearch: .github/gates.json against the workflows
V3Every 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 rulesarch: tests/Fixtures/Evidence, read by evidence.py
V4The generated rule tables are what the locked tools say of their rulesarch: scripts/bot-rules run and compared; the hygiene / markdown job for markdownlint
V5A 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 runarch: the workflows, read

R — the rules themselves

RuleSaysEnforced by
R1Every rule here names an artifact carrying its identifier, and every identifier an artifact carries is heretest: TheRulesAreRealTest
R2Every rule refuses a planted violationtest: the Guards suite
R3The analyser, the refactorer and the Arch suite read the same treesarch: phpstan.neon and rector.php, compared