<?xml version="1.0" encoding="utf-8" standalone="yes"?><?xml-stylesheet href="/feed_style.xsl" type="text/xsl"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:media="https://www.rssboard.org/media-rss"><channel><title>Phpstan on DazzLog</title><link>https://blog.dazzlog.de/tags/phpstan/</link><description>Recent content in Phpstan on DazzLog</description><generator>Hugo -- gohugo.io</generator><language>en-us</language><copyright>dazz - [Creative Commons Attribution 4.0 International License](https://creativecommons.org/licenses/by/4.0/).</copyright><lastBuildDate>Thu, 26 Nov 2026 09:00:00 +0100</lastBuildDate><atom:link href="https://blog.dazzlog.de/tags/phpstan/index.xml" rel="self" type="application/rss+xml"/><icon>https://blog.dazzlog.de/logo.svg</icon><item><title>Green Is Not Done</title><link>https://blog.dazzlog.de/posts/2026-11-26_green-is-not-done/</link><pubDate>Thu, 26 Nov 2026 09:00:00 +0100</pubDate><guid>https://blog.dazzlog.de/posts/2026-11-26_green-is-not-done/</guid><description><![CDATA[<p>Have you ever seen a pull request that was green, and still wrong?</p>
<p>This is the written version of my talk <a href="https://live.symfony.com/2026-warsaw-con/schedule/green-is-not-done"><em>Green Is Not Done</em></a> at SymfonyCon Warsaw 2026 (<a href="https://speakerdeck.com/dazz/green-is-not-done">slides</a>). It is about harness engineering for existing Symfony applications: what happens when a coding agent runs your CI, every check passes, and the code is <em>still</em> wrong. And what you can do about it with the tools you already have.</p>
<h2 id="the-ticket">The ticket</h2>
<p>In one of the Symfony applications I&rsquo;m working on, coding agents write a lot of the code. I write the tickets, and I review the pull requests.</p>
<p>The application fetches video details from an external API, and that API has a rate limit. When we run into it, the request fails, and whoever clicked the button is left with a broken page. So I wrote a ticket for a coding agent:</p>
<blockquote>
<p>Fetching video details hits the rate limit. Handle it, so the page keeps working.</p>
</blockquote>
<p>A clear ticket, a real problem, and a good fit for an agent.</p>
<p>The agent solved the task. And along the way, it wrote this code in a controller:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#282a36;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"><code class="language-php" data-lang="php"><span style="display:flex;"><span><span style="color:#6272a4">// src/UI/Http/FetchVideoDetailsController.php
</span></span></span><span style="display:flex;"><span><span style="color:#ff79c6">try</span> {
</span></span><span style="display:flex;"><span>    <span style="color:#8be9fd;font-style:italic">$this</span><span style="color:#ff79c6">-&gt;</span><span style="color:#50fa7b">fetchVideoDetails</span>(<span style="color:#8be9fd;font-style:italic">$videoId</span>);
</span></span><span style="display:flex;"><span>    <span style="color:#8be9fd;font-style:italic">$this</span><span style="color:#ff79c6">-&gt;</span><span style="color:#50fa7b">addFlash</span>(<span style="color:#f1fa8c">&#39;success&#39;</span>, <span style="color:#f1fa8c">&#39;Video details fetched successfully&#39;</span>);
</span></span><span style="display:flex;"><span>} <span style="color:#ff79c6">catch</span> (Exception <span style="color:#8be9fd;font-style:italic">$exception</span>) {
</span></span><span style="display:flex;"><span>    <span style="color:#8be9fd;font-style:italic">$this</span><span style="color:#ff79c6">-&gt;</span><span style="color:#50fa7b">addFlash</span>(<span style="color:#f1fa8c">&#39;danger&#39;</span>, <span style="color:#f1fa8c">&#39;Could not fetch video details: &#39;</span>
</span></span><span style="display:flex;"><span>        <span style="color:#ff79c6">.</span> <span style="color:#8be9fd;font-style:italic">$exception</span><span style="color:#ff79c6">-&gt;</span><span style="color:#50fa7b">getMessage</span>());
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><p>The controller fetches the video details. If that works, the user gets a green success flash. If anything goes wrong, <em>any</em> exception at all, the controller catches it and shows a red flash with the exception message.</p>
<p>So where does this exception go? Into a red box in the browser. Not into the log, because nothing is logged here. And not to Symfony&rsquo;s error handling, because nobody throws it again.</p>
<p>I wanted <strong>one</strong> failure handled: the rate limit. What I got handles <strong>every</strong> failure in the same way.</p>
<p>Imagine someone introduces a bug here next week. A <code>null</code> where an object should be. This is what the user sees:</p>
<blockquote>
<p>Could not fetch video details: Call to a member function getTitle() on null</p>
</blockquote>
<p>An internal error message in a red box. And nobody in the team finds out. No log entry, no alert in the monitoring.</p>
<p>The bug looks exactly like a rate limit.</p>
<p>The page keeps working. That is exactly what I asked for. And that is exactly the problem.</p>
<h2 id="all-green">All green</h2>
<p>The agent did not just write this code and stop. It had one clear instruction: run <code>make ci</code> before you say you are done, and fix every finding. Not ignore it, not add it to the baseline. Fix it.</p>
<p><code>make ci</code> is one command that runs the whole quality gate: code style, Rector, PHPStan, deptrac, PHPUnit and Infection.</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#282a36;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"><code class="language-text" data-lang="text"><span style="display:flex;"><span>$ make ci
</span></span><span style="display:flex;"><span>cs-check   ✓
</span></span><span style="display:flex;"><span>rector     ✓
</span></span><span style="display:flex;"><span>phpstan    ✓
</span></span><span style="display:flex;"><span>deptrac    ✓
</span></span><span style="display:flex;"><span>phpunit    ✓
</span></span><span style="display:flex;"><span>infection  ✓
</span></span></code></pre></div><p>The agent ran it, and every check passed. Then it opened the pull request, and the pipeline ran the same checks. The pipeline was green too.</p>
<p>The agent did exactly what I had asked. It followed the process. And still, this code was wrong.</p>
<p>When <em>I</em> open a pull request, green CI means: ready for review. I still know a lot of things that no tool checks. I know our conventions. I know what we decided last month. I know what we do with exceptions. (I even gave <a href="https://live.symfony.com/account/replay/video/877">a whole talk about exception handling in Symfony</a> at SymfonyCon Brussels 2023.)</p>
<p>But for a coding agent, green means <strong>done</strong>. Green is the signal to stop.</p>
<blockquote>
<p>When a coding agent writes code, your CI becomes its definition of done.</p>
</blockquote>
<p>And that means: anything your checks cannot see can pass as finished.</p>
<p>So this story leaves two questions. Why did the agent write this code? And why could none of my checks see it?</p>
<h2 id="it-reads-your-code-not-your-head">It reads your code. Not your head.</h2>
<p>A coding agent optimizes for <strong>completion</strong>. It looks for the smallest local change that satisfies the request. That is what it is trained for, and it only knows the part of the code that it has read.</p>
<p>From that point of view, the catch was a very good solution. My ticket said: keep the page working. Catching everything is the shortest way there. The change is small, it looks safe, and it is quick to check.</p>
<p>And there is a second reason. The agent does not only read my ticket. It reads parts of my code. And my code was full of catches like this one. So the agent did not invent anything. It did what my codebase showed it.</p>
<p>What the agent does not know are the rules that only exist in our heads. In my head, there is a rule: we don&rsquo;t catch generic exceptions, we let them surface.</p>
<p>The agent cannot read my head.</p>
<h2 id="agent--model--harness">Agent = Model + Harness</h2>
<p>So how do we get those rules out of our heads? This is where <strong>harness engineering</strong> comes in.</p>
<p>A coding agent is more than a language model. I like this simple formula:</p>
<blockquote>
<p>Agent = Model + Harness</p>
</blockquote>
<p>The model generates the code and reasons about it. Everything around the model is the harness: the context it gets, the tools it can use, its permissions, the checks it runs, and the workflow it follows.</p>
<p>Your coding tool already brings a harness with it. But we can also build an <strong>outer harness for our own project</strong>: <code>AGENTS.md</code>, skills, <code>make ci</code>, custom PHPStan rules. That project-specific part is what this post is about.</p>
<div class="details admonition note open">
  <div class="details-summary admonition-title">
    <i class="icon ">&#Xf040;</i> Where the terms come from<i class="details-icon fas fa-angle-right fa-fw"></i>
  </div>
  <div class="details-content">
    <div class="admonition-content">Guides, sensors, computational, inferential and harnessability all come from Birgitta Böckeler&rsquo;s article <a href="https://martinfowler.com/articles/harness-engineering.html">Harness Engineering for Coding Agent Users</a> on martinfowler.com. Read it, it is worth your time.</div>
  </div>
</div>
<h3 id="guides-before-sensors-after">Guides before. Sensors after.</h3>
<p>Böckeler splits the harness into two parts.</p>
<p><strong>Guides</strong> work <em>before</em> the agent changes the code. They describe our conventions and how we want the agent to work. Most of us have written an <code>AGENTS.md</code> or a skill by now. Those are guides. In control theory, this direction is called <em>feedforward</em>.</p>
<p><strong>Sensors</strong> work <em>after</em> the change. They check the result and give the agent something it can act on. PHPStan is a sensor. Our tests are sensors. This direction is called <em>feedback</em>.</p>
<p>When a sensor finds something, the agent gets the finding, changes the code, and the sensor checks again. That is the feedback loop.</p>
<p>The short version:</p>
<blockquote>
<p>A guide says what we want. A sensor checks what we got.</p>
</blockquote>
<h3 id="computational-or-inferential">Computational or inferential?</h3>
<p>There is a second distinction. Guides and sensors can be <strong>computational</strong> or <strong>inferential</strong>.</p>
<table>
	<thead>
			<tr>
					<th></th>
					<th>Computational</th>
					<th>Inferential</th>
			</tr>
	</thead>
	<tbody>
			<tr>
					<td><strong>Guides</strong></td>
					<td>types, scaffolding</td>
					<td><code>AGENTS.md</code>, skills</td>
			</tr>
			<tr>
					<td><strong>Sensors</strong></td>
					<td>PHPStan, tests, <code>make ci</code></td>
					<td>human review, LLM review</td>
			</tr>
	</tbody>
</table>
<p>Computational controls are executable. They are fast and cheap, and the same input and the same rule always give the same result.</p>
<p>Inferential controls look at meaning and context. That is a human reviewer, or another language model. They are flexible, they can ask whether an abstraction makes sense. But they are slower, they cost more, and the answer may be different next time.</p>
<p>When the result of an agent is not what we wanted, most of us start in the top right corner. We write more into <code>AGENTS.md</code>. We write a skill.</p>
<h3 id="the-guidance-was-there">The guidance was there</h3>
<p>And here is the part that surprised me.</p>
<p>When I looked back, my project <em>already had</em> a skill that warned about almost exactly this pattern: a catch that turns an exception into a string hides a bug. That skill existed when the agent wrote this catch.</p>
<p>The guidance was there. It made a good result more likely. It did not check the result.</p>
<p>What found the problem was me, in the review. An inferential sensor. A reviewer can see a problem that no rule knows yet. But only a computational check makes sure it does not come back.</p>
<h2 id="harnessability">Harnessability</h2>
<p>How easy is it to build sensors? That depends on the codebase. Böckeler calls this <strong>harnessability</strong>.</p>
<p>Some things make a codebase easier to harness:</p>
<ul>
<li><strong>types</strong>, so we can run type checks,</li>
<li><strong>clear module boundaries</strong>, so we can check the architecture,</li>
<li><strong>fast commands</strong>, so the agent can run them again and again,</li>
<li><strong>honest exit codes</strong>, so the agent knows it failed,</li>
<li><strong>clear error messages</strong>, so the agent knows what to change.</li>
</ul>
<p>And their opposites make it harder: mixed conventions, unclear boundaries, slow tests, commands that exit 0 even when they fail, and noisy output.</p>
<p>A well-structured codebase sits on the easy side. New or old does not matter. A big ball of mud sits on the other side. Many legacy applications drift there over the years. But a new project can get there in a few months, too, especially when agents write most of the code.</p>
<blockquote>
<p>Legacy teams face the harder problem: the harness is most needed where it is hardest to build.</p>
<p><em>Birgitta Böckeler</em></p>
</blockquote>
<p>Many legacy Symfony applications started many Symfony versions ago. They have old patterns, half-finished migrations, and an exception to every rule. Remember all those catches the agent learned from? That is what legacy means for an agent: <strong>every old pattern is an example.</strong></p>
<p>But we don&rsquo;t have to clean up everything before we improve the harness. We need a way to add stronger checks without letting every old violation block today&rsquo;s work.</p>
<h2 id="i-stopped-fixing-the-change-i-started-fixing-the-gate">I stopped fixing the change. I started fixing the gate.</h2>
<p>This is the moment where I changed how I work with agents.</p>
<p>Before, when I found a problem in a review, I wrote a comment: please fix this. The agent fixed it. And often, a week later, the same kind of mistake came back in another pull request, and I found it again.</p>
<p>I was the sensor. And I am a slow and expensive sensor, and I get tired.</p>
<p>Now I ask a different question for every review finding: <em>why could my gate not see this? And how can it see it next time?</em></p>
<p>A review comment fixes one change. A rule checks every future change.</p>
<p>So this time I did not ask for a fix. I asked for a rule.</p>
<h2 id="name-the-failure-class">Name the failure class</h2>
<p>The first step is to name the <strong>failure class</strong>. Not &ldquo;this controller has a bad catch&rdquo;. That is just the incident. The failure class is the pattern behind it:</p>
<blockquote>
<p>Catching <code>\Exception</code> or <code>\Throwable</code> hides failures.</p>
</blockquote>
<p>This one sentence is the most important part of the whole process. If the sentence is too vague, the rule will be too vague. If it is too narrow, the rule only finds this one controller.</p>
<h3 id="which-sensor-could-have-seen-it">Which sensor could have seen it?</h3>
<p>With the failure class named, the next question is: which of my sensors could have seen it? Let&rsquo;s go through the gate.</p>
<ul>
<li><strong>cs-check</strong> sees formatting. The catch was nicely formatted.</li>
<li><strong>Rector</strong> sees code that it can upgrade or refactor. There was nothing to upgrade.</li>
<li><strong>PHPStan</strong> sees types. <code>Exception</code> is a perfectly valid type. The code was type-safe.</li>
<li><strong>deptrac</strong> sees dependencies between layers. A controller may depend on <code>Exception</code>. Every layer may.</li>
<li><strong>PHPUnit</strong> sees behaviour. And the behaviour looked fine: a page with a red message.</li>
<li><strong>Infection</strong> sees whether the tests notice changes. It does not ask whether the code should exist at all.</li>
</ul>
<p>None of them. Every sensor did its job. None of them had the job to see this.</p>
<p>So the question is not which sensor saw it. It is which sensor can be given a <strong>new job</strong>. PHPStan reads every statement, and it lets us write our own rules. So the new sensor became a custom PHPStan rule.</p>
<h2 id="a-guide-that-builds-a-sensor">A guide that builds a sensor</h2>
<p>Writing a PHPStan rule sounds like a lot of work. A few years ago, it was. You had to learn PHPStan&rsquo;s API, the node types and the test setup.</p>
<p>Today, I let the agent write it. But I don&rsquo;t let it improvise. I have a skill for this: <strong>phpstan-rule-writer</strong>. It is a guide that builds a sensor.</p>
<p>The skill&rsquo;s workflow is short:</p>
<ol>
<li>Name the mistake in one sentence; check that deptrac doesn&rsquo;t already cover it.</li>
<li>Write the rule, register it.</li>
<li>Write a fixture and a test.</li>
<li>Run the test, fix the expected lines from the actual output.</li>
<li>Run <code>make ci-phpstan</code>. The rule now runs against the real code, and against itself.</li>
<li>Fix what it finds. Never edit a baseline by hand; regenerate it.</li>
</ol>
<p>The rest of the skill is the knowledge I would otherwise have to repeat in every session: where the files go, how error identifiers are named, which node type fits which kind of check, and the quirks of PHPStan&rsquo;s <code>RuleTestCase</code>. Things like <em>&ldquo;attributes count towards the reported line, doc blocks do not&rdquo;</em> or <em>&ldquo;a trailing <code>.php</code> in the <code>rules:</code> list is not rejected, the rule is just silently never executed&rdquo;</em>. Each of those lines saves the agent from rediscovering a quirk the hard way.</p>
<p>My part was one sentence: <em>do not catch <code>\Exception</code> or <code>\Throwable</code></em>.</p>
<div class="details admonition tip open">
  <div class="details-summary admonition-title">
    <i class="icon ">&#xf400;</i> I learned<i class="details-icon fas fa-angle-right fa-fw"></i>
  </div>
  <div class="details-content">
    <div class="admonition-content">Writing the rule was not the hard part. The hard part was naming the mistake.</div>
  </div>
</div>
<h3 id="the-skill">The skill</h3>
<p>Here is the complete skill, as it lives in the project. It is written for <em>my</em> project: the paths, the Docker commands and the ticket numbers will look different in yours. Take the structure, not the details.</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#282a36;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"><code class="language-markdown" data-lang="markdown"><span style="display:flex;"><span><span style="color:#6272a4">&lt;!-- .claude/skills/phpstan-rule-writer/SKILL.md --&gt;</span>
</span></span><span style="display:flex;"><span>---
</span></span><span style="display:flex;"><span>name: phpstan-rule-writer
</span></span><span style="display:flex;"><span>description: &gt;
</span></span><span style="display:flex;"><span>  Write custom PHPStan rules to catch mistakes that were discovered during a coding session.
</span></span><span style="display:flex;"><span>  Use this skill whenever: (1) a coding mistake was corrected in the conversation and you want to prevent it from happening again, (2) the user asks to &#34;add a PHPStan rule&#34; or &#34;codify this as a static analysis check&#34;, (3) a code review reveals a pattern that should be enforced automatically, (4) the user says &#34;turn this mistake into a rule&#34; or similar, (5) an existing rule under app/ci/phpstan/ has to be changed or tested. This skill ensures that lessons learned during development are permanently encoded as deterministic checks. After correcting code, offer in one sentence to codify the lesson as a rule; write it once the user accepts.
</span></span><span style="display:flex;"><span>---
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="font-weight:bold">## Contents
</span></span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#ff79c6">-</span> Layout
</span></span><span style="display:flex;"><span><span style="color:#ff79c6">-</span> Error identifiers: <span style="color:#f1fa8c">`&lt;concern&gt;.&lt;invariant&gt;`</span>
</span></span><span style="display:flex;"><span><span style="color:#ff79c6">-</span> The rule
</span></span><span style="display:flex;"><span><span style="color:#ff79c6">-</span> Registration
</span></span><span style="display:flex;"><span><span style="color:#ff79c6">-</span> The test
</span></span><span style="display:flex;"><span><span style="color:#ff79c6">-</span> Workflow
</span></span><span style="display:flex;"><span><span style="color:#ff79c6">-</span> Context
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="font-weight:bold"># PHPStan Rule Writer
</span></span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>Turns a mistake that was just corrected into a deterministic check, so it cannot slip through again.
</span></span><span style="display:flex;"><span>After any correction, ask: <span style="text-decoration:underline">*could a PHPStan rule have caught this before the code was written?*</span> If yes,
</span></span><span style="display:flex;"><span>offer it in one sentence and write it once the user accepts. For layering violations check
</span></span><span style="display:flex;"><span><span style="color:#f1fa8c">`ci/config/deptrac.yaml`</span> first — a PHPStan rule is only worth it when you need a more targeted message
</span></span><span style="display:flex;"><span>or something deptrac cannot express.
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="font-weight:bold">## Layout
</span></span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#f1fa8c">```
</span></span></span><span style="display:flex;"><span>app/ci/
</span></span><span style="display:flex;"><span>  config/
</span></span><span style="display:flex;"><span>    phpstan.dist.neon              ← bin/ config/ public/ src/ + ci/phpstan/Rules/, level max (TABS under rules:)
</span></span><span style="display:flex;"><span>    phpstan.tests.neon             ← tests/, custom ruleset + extensions, no level (4 SPACES under rules:)
</span></span><span style="display:flex;"><span>    phpstan-baseline.neon          ← generated (make ci-phpstan-baseline)
</span></span><span style="display:flex;"><span>    phpstan-tests-baseline.neon    ← generated
</span></span><span style="display:flex;"><span>    phpstan-ignore.neon            ← hand-written, reasoned exemptions
</span></span><span style="display:flex;"><span>  phpstan/
</span></span><span style="display:flex;"><span>    Rules/MyCustomRule.php         ← namespace CI\PhpStan\Rules
</span></span><span style="display:flex;"><span>    Tests/Rules/MyCustomRule/      ← capital T; one directory per rule, namespace follows the path
</span></span><span style="display:flex;"><span>      MyCustomRuleTest.php         ← namespace CI\PhpStan\Tests\Rules\MyCustomRule
</span></span><span style="display:flex;"><span>      data/my-custom-rule.php      ← the fixture, never shared with another rule&#39;s test
</span></span><span style="display:flex;"><span><span style="color:#f1fa8c">```</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>The <span style="color:#f1fa8c">`Rule`</span> suffix carries through class → directory → test class. PSR-4 prefix is <span style="text-decoration:underline">**</span><span style="color:#f1fa8c">`CI\PhpStan\`</span>**
</span></span><span style="display:flex;"><span>(not <span style="color:#f1fa8c">`App\…`</span>) and lives in <span style="text-decoration:underline">**</span><span style="color:#f1fa8c">`autoload-dev`</span>**, so rules never ship with the runtime autoload;
</span></span><span style="display:flex;"><span>lowercase <span style="color:#f1fa8c">`tests/`</span> breaks the autoloader on Linux.
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>**Your rule is analysed too**, at level max, and by our own rules — <span style="color:#f1fa8c">`SingleUseVariableRule`</span> will tell
</span></span><span style="display:flex;"><span>you to inline that one-shot local. php-cs-fixer covers <span style="color:#f1fa8c">`ci/phpstan/`</span> as well, so <span style="color:#f1fa8c">`make ci-cs-fix`</span>
</span></span><span style="display:flex;"><span>reformats it; <span style="color:#f1fa8c">`data/`</span> is deliberately excluded from the fixer, because reformatting a fixture shifts
</span></span><span style="display:flex;"><span>the line numbers its test asserts.
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="font-weight:bold">## Error identifiers: `&lt;concern&gt;.&lt;invariant&gt;`
</span></span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>Every error carries an <span style="color:#f1fa8c">`-&gt;identifier()`</span> — never <span style="color:#f1fa8c">`custom.`</span>, never a project name, never the rule class.
</span></span><span style="display:flex;"><span>It is what shows up in <span style="color:#f1fa8c">`@phpstan-ignore`</span> lines and baseline entries, so it has to read as an invariant.
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>Pick the **concern from what the rule actually inspects**, not from the ticket it was born in — a
</span></span><span style="display:flex;"><span>concern claiming a narrower scope than the code has is a lie in the error output. The part after the
</span></span><span style="display:flex;"><span>dot names the violated invariant, lower-camel, dropping any word the concern already carries:
</span></span><span style="display:flex;"><span><span style="color:#f1fa8c">`command.namePrefix`</span>, not <span style="color:#f1fa8c">`command.commandNamePrefix`</span>. One identifier per rule, shared by all of its
</span></span><span style="display:flex;"><span>errors.
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>Read the concerns in use off the rules themselves rather than a list here, which only goes stale:
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#f1fa8c">```sh
</span></span></span><span style="display:flex;"><span>grep -ho <span style="color:#f1fa8c">&#34;identifier(&#39;[^.]*&#34;</span> ci/phpstan/Rules/*.php | sort -u
</span></span><span style="display:flex;"><span><span style="color:#f1fa8c">```</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>A new concern is fine — check it does not collide with a PHPStan core identifier
</span></span><span style="display:flex;"><span>(<span style="color:#f1fa8c">`grep -ao &#34;&#39;&lt;concern&gt;\.[a-zA-Z]*&#39;&#34; vendor/phpstan/phpstan/phpstan.phar`</span>) and ask the user first.
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="font-weight:bold">## The rule
</span></span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#f1fa8c">```php
</span></span></span><span style="display:flex;"><span><span style="color:#ff79c6">&lt;?</span>php
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#ff79c6">declare</span>(strict_types<span style="color:#ff79c6">=</span><span style="color:#bd93f9">1</span>);
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#ff79c6">namespace</span> CI\PhpStan\Rules;
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#ff79c6">use</span> PhpParser\Node;
</span></span><span style="display:flex;"><span><span style="color:#ff79c6">use</span> PhpParser\Node\Stmt\Class_;
</span></span><span style="display:flex;"><span><span style="color:#ff79c6">use</span> PHPStan\Analyser\Scope;
</span></span><span style="display:flex;"><span><span style="color:#ff79c6">use</span> PHPStan\Rules\Rule;
</span></span><span style="display:flex;"><span><span style="color:#ff79c6">use</span> PHPStan\Rules\RuleErrorBuilder;
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#f1fa8c">/**
</span></span></span><span style="display:flex;"><span><span style="color:#f1fa8c"> * One-line description of what the rule enforces and why.
</span></span></span><span style="display:flex;"><span><span style="color:#f1fa8c"> *
</span></span></span><span style="display:flex;"><span><span style="color:#f1fa8c"> * @implements Rule&lt;Class_&gt;
</span></span></span><span style="display:flex;"><span><span style="color:#f1fa8c"> */</span>
</span></span><span style="display:flex;"><span><span style="color:#ff79c6">final</span> readonly <span style="color:#ff79c6">class</span> <span style="color:#50fa7b">MyCustomRule</span> <span style="color:#ff79c6">implements</span> Rule
</span></span><span style="display:flex;"><span>{
</span></span><span style="display:flex;"><span>    <span style="color:#ff79c6">public</span> <span style="color:#ff79c6">function</span> <span style="color:#50fa7b">getNodeType</span>()<span style="color:#ff79c6">:</span> string
</span></span><span style="display:flex;"><span>    {
</span></span><span style="display:flex;"><span>        <span style="color:#ff79c6">return</span> Class_<span style="color:#ff79c6">::</span><span style="color:#50fa7b">class</span>;
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#ff79c6">public</span> <span style="color:#ff79c6">function</span> <span style="color:#50fa7b">processNode</span>(Node <span style="color:#8be9fd;font-style:italic">$node</span>, Scope <span style="color:#8be9fd;font-style:italic">$scope</span>)<span style="color:#ff79c6">:</span> <span style="color:#ff79c6">array</span>
</span></span><span style="display:flex;"><span>    {
</span></span><span style="display:flex;"><span>        <span style="color:#6272a4">// ... your checks ...
</span></span></span><span style="display:flex;"><span>        <span style="color:#ff79c6">return</span> [
</span></span><span style="display:flex;"><span>            RuleErrorBuilder<span style="color:#ff79c6">::</span><span style="color:#50fa7b">message</span>(\sprintf(
</span></span><span style="display:flex;"><span>                <span style="color:#f1fa8c">&#39;Class &#34;%s&#34; does X. Do Y instead.&#39;</span>,
</span></span><span style="display:flex;"><span>                <span style="color:#8be9fd;font-style:italic">$node</span><span style="color:#ff79c6">-&gt;</span><span style="color:#50fa7b">namespacedName</span><span style="color:#ff79c6">?-&gt;</span><span style="color:#50fa7b">toString</span>() <span style="color:#ff79c6">??</span> <span style="color:#f1fa8c">&#39;anonymous class&#39;</span>,
</span></span><span style="display:flex;"><span>            ))<span style="color:#ff79c6">-&gt;</span><span style="color:#50fa7b">identifier</span>(<span style="color:#f1fa8c">&#39;architecture.somethingSpecific&#39;</span>)<span style="color:#ff79c6">-&gt;</span><span style="color:#50fa7b">build</span>(),
</span></span><span style="display:flex;"><span>        ];
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span><span style="color:#f1fa8c">```</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#ff79c6">-</span> <span style="color:#f1fa8c">`final readonly class`</span>, <span style="color:#f1fa8c">`@implements Rule&lt;NodeType&gt;`</span> matching <span style="color:#f1fa8c">`getNodeType()`</span>, no <span style="color:#f1fa8c">`@return`</span> docblock
</span></span><span style="display:flex;"><span>  (PHPStan infers it from the interface).
</span></span><span style="display:flex;"><span><span style="color:#ff79c6">-</span> No <span style="color:#f1fa8c">`\assert($node instanceof …)`</span> — PHPStan narrows <span style="color:#f1fa8c">`$node`</span> from <span style="color:#f1fa8c">`@implements`</span> on its own.
</span></span><span style="display:flex;"><span><span style="color:#ff79c6">-</span> No <span style="color:#f1fa8c">`#[AsTaggedItem(&#39;phpstan.rules.rule&#39;)]`</span>. It looks like registration but isn&#39;t: PHPStan builds a
</span></span><span style="display:flex;"><span>  Nette container and never reads Symfony attributes. Eight rules carried it inertly until DAZ-177.
</span></span><span style="display:flex;"><span><span style="color:#ff79c6">-</span> <span style="color:#f1fa8c">`processNode`</span> is a short chain of named predicates (<span style="color:#f1fa8c">`isDoctrineAdapter`</span>, <span style="color:#f1fa8c">`expectedTestPath`</span>), no
</span></span><span style="display:flex;"><span>  flag variables or <span style="color:#f1fa8c">`break 2`</span> — the rule body follows <span style="color:#f1fa8c">`dazzhub-code-shape`</span> like any other class.
</span></span><span style="display:flex;"><span><span style="color:#ff79c6">-</span> Messages built with <span style="color:#f1fa8c">`\sprintf()`</span>, offending class in double quotes, saying what to do — not just
</span></span><span style="display:flex;"><span>  what is wrong. Optionally <span style="color:#f1fa8c">`-&gt;tip(&#39;…&#39;)`</span>. Chain order <span style="color:#f1fa8c">`-&gt;identifier()-&gt;line()-&gt;build()`</span>.
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>**Node type**: <span style="color:#f1fa8c">`MethodCall`</span> / <span style="color:#f1fa8c">`StaticCall`</span> / <span style="color:#f1fa8c">`New_`</span> / <span style="color:#f1fa8c">`FuncCall`</span> for call sites, <span style="color:#f1fa8c">`Class_`</span> for a class
</span></span><span style="display:flex;"><span>declaration, <span style="color:#f1fa8c">`ClassMethod`</span> / <span style="color:#f1fa8c">`Property`</span> / <span style="color:#f1fa8c">`Assign`</span> / <span style="color:#f1fa8c">`Return_`</span> for members, <span style="color:#f1fa8c">`FunctionLike`</span> for data
</span></span><span style="display:flex;"><span>flow inside a body, <span style="color:#f1fa8c">`PHPStan\Node\InClassNode`</span> when the **absence** of something matters (a missing
</span></span><span style="display:flex;"><span>method, an inherited default) — it hands you <span style="color:#f1fa8c">`getClassReflection()`</span> and <span style="color:#f1fa8c">`getOriginalNode()`</span>.
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>On a <span style="color:#f1fa8c">`Class_`</span> node the scope is <span style="text-decoration:underline">*outside*</span> the class, so <span style="color:#f1fa8c">`$scope-&gt;getClassReflection()`</span> is null there —
</span></span><span style="display:flex;"><span>use <span style="color:#f1fa8c">`$node-&gt;namespacedName?-&gt;toString() ?? &#39;anonymous class&#39;`</span>. Everywhere else
</span></span><span style="display:flex;"><span><span style="color:#f1fa8c">`$scope-&gt;getClassReflection()`</span> is the enclosing class, and <span style="color:#f1fa8c">`$scope-&gt;getType($expr)`</span> its inferred type
</span></span><span style="display:flex;"><span>(match a receiver with <span style="color:#f1fa8c">`(new ObjectType(Foo::class))-&gt;isSuperTypeOf(…)-&gt;yes()`</span>). Attributes live in
</span></span><span style="display:flex;"><span><span style="color:#f1fa8c">`$node-&gt;attrGroups[]-&gt;attrs[]`</span>, compared via <span style="color:#f1fa8c">`$attr-&gt;name-&gt;toString()`</span>; each <span style="color:#f1fa8c">`Node\Arg`</span> has a nullable
</span></span><span style="display:flex;"><span><span style="color:#f1fa8c">`name`</span> and a <span style="color:#f1fa8c">`value`</span>, so a positional argument is <span style="color:#f1fa8c">`name === null`</span> at position 0. For promoted
</span></span><span style="display:flex;"><span>constructor params use <span style="color:#f1fa8c">`$param-&gt;isPromoted()`</span> / <span style="color:#f1fa8c">`$param-&gt;isReadonly()`</span>, and the variable is
</span></span><span style="display:flex;"><span><span style="color:#f1fa8c">`$param-&gt;var`</span>, not <span style="color:#f1fa8c">`$param-&gt;name`</span>.
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="font-weight:bold">## Registration
</span></span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>The rule goes into exactly **one** lane: <span style="color:#f1fa8c">`phpstan.dist.neon`</span> for production code, or
</span></span><span style="display:flex;"><span><span style="color:#f1fa8c">`phpstan.tests.neon`</span> for test hygiene (a test-code rule sees nothing in the dist lane). Insert
</span></span><span style="display:flex;"><span>alphabetically, mind the indentation of that file, and write the class name **without** a <span style="color:#f1fa8c">`.php`</span>
</span></span><span style="display:flex;"><span>suffix — a trailing <span style="color:#f1fa8c">`.php`</span> is not rejected, the rule is just silently never executed.
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>There is no autodiscovery: <span style="color:#f1fa8c">`phpstan.rules.rule`</span> is PHPStan&#39;s own DI tag and works via a <span style="color:#f1fa8c">`services:`</span>
</span></span><span style="display:flex;"><span>entry, but Nette&#39;s <span style="color:#f1fa8c">`search:`</span> section is not available (*&#34;Found section &#39;search&#39;, but corresponding
</span></span><span style="display:flex;"><span>extension is missing&#34;*), so every rule needs its own line either way.
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#f1fa8c">`phpunit.dist.xml`</span> needs nothing — the &#34;PHPStan Rules&#34; testsuite picks up new directories recursively.
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="font-weight:bold">## The test
</span></span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#f1fa8c">```php
</span></span></span><span style="display:flex;"><span><span style="color:#ff79c6">&lt;?</span>php
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#ff79c6">declare</span>(strict_types<span style="color:#ff79c6">=</span><span style="color:#bd93f9">1</span>);
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#ff79c6">namespace</span> CI\PhpStan\Tests\Rules\MyCustomRule;
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#ff79c6">use</span> CI\PhpStan\Rules\MyCustomRule;
</span></span><span style="display:flex;"><span><span style="color:#ff79c6">use</span> PHPStan\Rules\Rule;
</span></span><span style="display:flex;"><span><span style="color:#ff79c6">use</span> PHPStan\Testing\RuleTestCase;
</span></span><span style="display:flex;"><span><span style="color:#ff79c6">use</span> PHPUnit\Framework\Attributes\CoversNothing;
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#f1fa8c">/**
</span></span></span><span style="display:flex;"><span><span style="color:#f1fa8c"> * @extends RuleTestCase&lt;MyCustomRule&gt;
</span></span></span><span style="display:flex;"><span><span style="color:#f1fa8c"> */</span>
</span></span><span style="display:flex;"><span>#[CoversNothing]
</span></span><span style="display:flex;"><span><span style="color:#ff79c6">final</span> <span style="color:#ff79c6">class</span> <span style="color:#50fa7b">MyCustomRuleTest</span> <span style="color:#ff79c6">extends</span> RuleTestCase
</span></span><span style="display:flex;"><span>{
</span></span><span style="display:flex;"><span>    <span style="color:#ff79c6">public</span> <span style="color:#ff79c6">function</span> <span style="color:#50fa7b">test_rule</span>()<span style="color:#ff79c6">:</span> void
</span></span><span style="display:flex;"><span>    {
</span></span><span style="display:flex;"><span>        <span style="color:#8be9fd;font-style:italic">$this</span><span style="color:#ff79c6">-&gt;</span><span style="color:#50fa7b">analyse</span>(
</span></span><span style="display:flex;"><span>            [__DIR__ <span style="color:#ff79c6">.</span> <span style="color:#f1fa8c">&#39;/data/my-custom-rule.php&#39;</span>],
</span></span><span style="display:flex;"><span>            [
</span></span><span style="display:flex;"><span>                [<span style="color:#f1fa8c">&#39;Actionable error message.&#39;</span>, <span style="color:#bd93f9">12</span>],  <span style="color:#6272a4">// [message, line]
</span></span></span><span style="display:flex;"><span>            ],
</span></span><span style="display:flex;"><span>        );
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#ff79c6">protected</span> <span style="color:#ff79c6">function</span> <span style="color:#50fa7b">getRule</span>()<span style="color:#ff79c6">:</span> Rule
</span></span><span style="display:flex;"><span>    {
</span></span><span style="display:flex;"><span>        <span style="color:#ff79c6">return</span> <span style="color:#ff79c6">new</span> MyCustomRule();
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span><span style="color:#f1fa8c">```</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#f1fa8c">`RuleTestCase`</span> is loadable although phpstan ships as a phar — its <span style="color:#f1fa8c">`bootstrap.php`</span> registers a phar
</span></span><span style="display:flex;"><span>autoloader, so just <span style="color:#f1fa8c">`use`</span> it and never <span style="color:#f1fa8c">`require_once &#39;phar://…&#39;`</span>. <span style="color:#f1fa8c">`#[CoversNothing]`</span> is right here:
</span></span><span style="display:flex;"><span>PHPStan rule tests are test-harness tests, not focused tests of application behavior, and the coverage
</span></span><span style="display:flex;"><span>source set in <span style="color:#f1fa8c">`phpunit.dist.xml`</span> is <span style="color:#f1fa8c">`src/`</span> only. Test methods snake_case, <span style="color:#f1fa8c">`getRule()`</span> last.
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>The fixture is a plain PHP file with at least one violating and one compliant case; classes it
</span></span><span style="display:flex;"><span>references must be autoloadable from <span style="color:#f1fa8c">`vendor/`</span>, the fixture&#39;s own classes need not be (PHPStan reads
</span></span><span style="display:flex;"><span>them by path — the composer PSR-4 warning is harmless).
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>Quirks of <span style="color:#f1fa8c">`analyse()`</span>:
</span></span><span style="display:flex;"><span><span style="color:#ff79c6">-</span> **Attributes count towards the reported line, doc blocks do not.** Run the test once and read the
</span></span><span style="display:flex;"><span>  real line off the failure diff instead of counting.
</span></span><span style="display:flex;"><span><span style="color:#ff79c6">-</span> <span style="color:#f1fa8c">`-&gt;line($someNode-&gt;getStartLine())`</span> overrides that. Two errors with the same message on the same
</span></span><span style="display:flex;"><span>  line are indistinguishable in the output, so a rule that can fire twice per class must set it.
</span></span><span style="display:flex;"><span><span style="color:#ff79c6">-</span> Messages are matched character-for-character; sort key is <span style="color:#f1fa8c">`(line, message, tip)`</span>, your array order
</span></span><span style="display:flex;"><span>  doesn&#39;t matter.
</span></span><span style="display:flex;"><span><span style="color:#ff79c6">-</span> A failure diff may append <span style="text-decoration:underline">*&#34;… not found in ReflectionProvider. Configure autoload-dev …&#34;*</span>. **That
</span></span><span style="display:flex;"><span>  is noise** — it is only appended when expected and actual already differ, so it can never turn a
</span></span><span style="display:flex;"><span>  green test red. Fix the line/message mismatch and it disappears.
</span></span><span style="display:flex;"><span><span style="color:#ff79c6">-</span> No <span style="color:#f1fa8c">`getAdditionalConfigFiles()`</span> and no <span style="color:#f1fa8c">`data/phpstan-fixtures.neon`</span>: DAZ-153 removed a shared
</span></span><span style="display:flex;"><span>  <span style="color:#f1fa8c">`scanFiles`</span> neon from every test and the suite stayed green with zero <span style="color:#f1fa8c">`classNotFound`</span> errors. If a
</span></span><span style="display:flex;"><span>  new fixture genuinely needs one, scope it to that rule&#39;s own directory and say so — don&#39;t
</span></span><span style="display:flex;"><span>  resurrect the shared file. (It must be <span style="color:#f1fa8c">`public static`</span>; <span style="color:#f1fa8c">`protected`</span> silently does nothing.)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="font-weight:bold">## Workflow
</span></span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>Everything runs in the container — never <span style="color:#f1fa8c">`composer`</span>, <span style="color:#f1fa8c">`phpunit`</span> or <span style="color:#f1fa8c">`bin/console`</span> on the host.
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#ff79c6">1.</span> Name the mistake in one sentence; check deptrac doesn&#39;t already cover it.
</span></span><span style="display:flex;"><span><span style="color:#ff79c6">2.</span> Write the rule, register it in the right lane.
</span></span><span style="display:flex;"><span><span style="color:#ff79c6">3.</span> Write fixture + test.
</span></span><span style="display:flex;"><span><span style="color:#ff79c6">4.</span> <span style="color:#f1fa8c">`docker compose exec -T app vendor/bin/phpunit -c ci/config/phpunit.dist.xml --filter MyCustomRuleTest`</span>
</span></span><span style="display:flex;"><span>   (whole suite: <span style="color:#f1fa8c">`--testsuite=&#34;PHPStan Rules&#34;`</span>), fix the expected lines from the actual output.
</span></span><span style="display:flex;"><span><span style="color:#ff79c6">5.</span> <span style="color:#f1fa8c">`docker compose exec -T app make ci-phpstan`</span> — the rule now runs against real code, and against
</span></span><span style="display:flex;"><span>   itself.
</span></span><span style="display:flex;"><span><span style="color:#ff79c6">6.</span> Fix what it finds. **Never edit a baseline by hand — regenerate it** with
</span></span><span style="display:flex;"><span>   <span style="color:#f1fa8c">`make ci-phpstan-baseline`</span>; appended entries sit in the wrong group, stay green, and then move on
</span></span><span style="display:flex;"><span>   the next regeneration, hiding the real change in the diff. Ask before baselining at all: if a rule
</span></span><span style="display:flex;"><span>   is too noisy to fix in this ticket, narrowing or dropping it is usually the better answer.
</span></span><span style="display:flex;"><span>   <span style="color:#f1fa8c">`phpstan-ignore.neon`</span> is the opposite — hand-written, each entry saying why the code is right and
</span></span><span style="display:flex;"><span>   the rule wrong about it.
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="font-weight:bold">## Context
</span></span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#ff79c6">-</span> <span style="color:#f1fa8c">`phpstan/extension-installer`</span> loads every installed extension in **both** lanes (strict-rules,
</span></span><span style="display:flex;"><span>  deprecation-rules, phpunit, symfony, webmozart-assert, disallowed-calls) — never add a vendor
</span></span><span style="display:flex;"><span>  include to <span style="color:#f1fa8c">`phpstan*.neon`</span>. The exception is phpstan-doctrine: <span style="color:#f1fa8c">`extra.phpstan/extension-installer.ignore`</span>
</span></span><span style="display:flex;"><span>  in <span style="color:#f1fa8c">`composer.json`</span> skips it, and both lanes include only its <span style="color:#f1fa8c">`extension.neon`</span> (type inference) —
</span></span><span style="display:flex;"><span>  its <span style="color:#f1fa8c">`rules.neon`</span> needs an <span style="color:#f1fa8c">`objectManagerLoader`</span> that boots the kernel. Details:
</span></span><span style="display:flex;"><span>  <span style="color:#f1fa8c">`docs/ai_docs/github-ci.md`</span> ▸ <span style="text-decoration:underline">*PHPStan extensions*</span>.
</span></span><span style="display:flex;"><span><span style="color:#ff79c6">-</span> <span style="color:#f1fa8c">`spaze/phpstan-disallowed-calls`</span> (4.15) scopes constructor disallows by argument: <span style="color:#f1fa8c">`allowParamsAnywhereAnyValue`</span> catches a no-arg <span style="color:#f1fa8c">`new DateTimeImmutable()`</span>, <span style="color:#f1fa8c">`disallowParamsAnywhere`</span> with value/typeString literal unions catches <span style="color:#f1fa8c">`now`</span>/<span style="color:#f1fa8c">`today`</span>/<span style="color:#f1fa8c">`tomorrow`</span>; several entries per method coexist, each with its own message. No regex on string params, so other relative strings (<span style="color:#f1fa8c">`-30 days`</span>) need enumerating and dynamic args escape (known locals are constant-folded).
</span></span><span style="display:flex;"><span><span style="color:#ff79c6">-</span> Rules to copy from: <span style="color:#f1fa8c">`NoExtendServiceEntityRepositoryRule`</span> (simplest — <span style="color:#f1fa8c">`Class_::extends`</span>),
</span></span><span style="display:flex;"><span>  <span style="color:#f1fa8c">`CommandNameMustStartWithAppRule`</span> (class attribute + its arguments), <span style="color:#f1fa8c">`DbQueryOnlyInRepositoryRule`</span>
</span></span><span style="display:flex;"><span>  (receiver type + call-site location), <span style="color:#f1fa8c">`SingleUseVariableRule`</span> (<span style="color:#f1fa8c">`FunctionLike`</span>, many errors per node).
</span></span></code></pre></div><h3 id="the-setup">The setup</h3>
<p>All custom rules live outside of <code>src/</code>, under <code>ci/phpstan/</code>, with their own namespace <code>CI\PhpStan\</code> in <code>autoload-dev</code>. So they never ship with the runtime autoloader.</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#282a36;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"><code class="language-text" data-lang="text"><span style="display:flex;"><span>app/ci/
</span></span><span style="display:flex;"><span>├── config/
</span></span><span style="display:flex;"><span>│   ├── phpstan.dist.neon          # production code, level max
</span></span><span style="display:flex;"><span>│   ├── phpstan.tests.neon         # test code
</span></span><span style="display:flex;"><span>│   ├── phpstan-baseline.neon      # generated, never edited by hand
</span></span><span style="display:flex;"><span>│   └── phpstan-ignore.neon        # hand-written, reasoned exemptions
</span></span><span style="display:flex;"><span>└── phpstan/
</span></span><span style="display:flex;"><span>    ├── Rules/
</span></span><span style="display:flex;"><span>    │   └── NoGenericExceptionCatchRule.php
</span></span><span style="display:flex;"><span>    └── Tests/
</span></span><span style="display:flex;"><span>        ├── RuleTestCoverageTest.php
</span></span><span style="display:flex;"><span>        └── Rules/
</span></span><span style="display:flex;"><span>            └── NoGenericExceptionCatchRule/
</span></span><span style="display:flex;"><span>                ├── NoGenericExceptionCatchRuleTest.php
</span></span><span style="display:flex;"><span>                └── data/
</span></span><span style="display:flex;"><span>                    └── no-generic-exception-catch.php
</span></span></code></pre></div><p>The <code>Rule</code> suffix carries through from the class to the test directory to the test class. One directory per rule, one fixture per test, never shared. The agent does not need all rules in its context to know where a new one goes. The directory structure tells it.</p>
<p>And the rules are analysed by PHPStan too, at level max, and by our own rules. A sensor that does not follow the conventions it enforces would be a strange sensor.</p>
<p>Registration is one line per rule. There is no autodiscovery in PHPStan&rsquo;s container:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#282a36;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"><code class="language-yaml" data-lang="yaml"><span style="display:flex;"><span><span style="color:#6272a4"># app/ci/config/phpstan.dist.neon</span>
</span></span><span style="display:flex;"><span><span style="color:#ff79c6">rules</span>:
</span></span><span style="display:flex;"><span>	- CI\PhpStan\Rules\NoGenericExceptionCatchRule
</span></span></code></pre></div><h3 id="the-rule">The rule</h3>
<p>Here is the rule as it runs today. It looks at every <code>catch</code> and reports it, unless it is allowed:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#282a36;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"><code class="language-php" data-lang="php"><span style="display:flex;"><span><span style="color:#6272a4">// app/ci/phpstan/Rules/NoGenericExceptionCatchRule.php
</span></span></span><span style="display:flex;"><span><span style="color:#ff79c6">namespace</span> CI\PhpStan\Rules;
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#ff79c6">use</span> PhpParser\Node;
</span></span><span style="display:flex;"><span><span style="color:#ff79c6">use</span> PhpParser\Node\Arg;
</span></span><span style="display:flex;"><span><span style="color:#ff79c6">use</span> PhpParser\Node\Expr;
</span></span><span style="display:flex;"><span><span style="color:#ff79c6">use</span> PhpParser\Node\Expr\New_;
</span></span><span style="display:flex;"><span><span style="color:#ff79c6">use</span> PhpParser\Node\Expr\Throw_;
</span></span><span style="display:flex;"><span><span style="color:#ff79c6">use</span> PhpParser\Node\Expr\Variable;
</span></span><span style="display:flex;"><span><span style="color:#ff79c6">use</span> PhpParser\Node\Name;
</span></span><span style="display:flex;"><span><span style="color:#ff79c6">use</span> PhpParser\Node\Stmt\Catch_;
</span></span><span style="display:flex;"><span><span style="color:#ff79c6">use</span> PhpParser\Node\Stmt\Expression;
</span></span><span style="display:flex;"><span><span style="color:#ff79c6">use</span> PHPStan\Analyser\Scope;
</span></span><span style="display:flex;"><span><span style="color:#ff79c6">use</span> PHPStan\Rules\Rule;
</span></span><span style="display:flex;"><span><span style="color:#ff79c6">use</span> PHPStan\Rules\RuleErrorBuilder;
</span></span><span style="display:flex;"><span><span style="color:#ff79c6">use</span> PHPStan\Type\ObjectType;
</span></span><span style="display:flex;"><span><span style="color:#ff79c6">use</span> Symfony\Component\Console\Event\ConsoleErrorEvent;
</span></span><span style="display:flex;"><span><span style="color:#ff79c6">use</span> Symfony\Component\HttpKernel\Event\ExceptionEvent;
</span></span><span style="display:flex;"><span><span style="color:#ff79c6">use</span> Symfony\Component\Messenger\Event\WorkerMessageFailedEvent;
</span></span><span style="display:flex;"><span><span style="color:#ff79c6">use</span> Symfony\Component\Messenger\Retry\RetryStrategyInterface;
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#f1fa8c">/**
</span></span></span><span style="display:flex;"><span><span style="color:#f1fa8c"> * Catching \Exception or \Throwable swallows failures nobody anticipated; catch the specific exception instead.
</span></span></span><span style="display:flex;"><span><span style="color:#f1fa8c"> * A catch that ends by rethrowing the caught exception, or a new one carrying it as previous, swallows nothing.
</span></span></span><span style="display:flex;"><span><span style="color:#f1fa8c"> *
</span></span></span><span style="display:flex;"><span><span style="color:#f1fa8c"> * @implements Rule&lt;Catch_&gt;
</span></span></span><span style="display:flex;"><span><span style="color:#f1fa8c"> */</span>
</span></span><span style="display:flex;"><span><span style="color:#ff79c6">final</span> readonly <span style="color:#ff79c6">class</span> <span style="color:#50fa7b">NoGenericExceptionCatchRule</span> <span style="color:#ff79c6">implements</span> Rule
</span></span><span style="display:flex;"><span>{
</span></span><span style="display:flex;"><span>    <span style="color:#ff79c6">private</span> <span style="color:#ff79c6">const</span> array GENERIC_TYPES <span style="color:#ff79c6">=</span> [<span style="color:#f1fa8c">&#39;Exception&#39;</span>, <span style="color:#f1fa8c">&#39;Throwable&#39;</span>];
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#ff79c6">private</span> <span style="color:#ff79c6">const</span> array EXCEPTION_HANDLING_EVENTS <span style="color:#ff79c6">=</span> [
</span></span><span style="display:flex;"><span>        ExceptionEvent<span style="color:#ff79c6">::</span><span style="color:#50fa7b">class</span>,
</span></span><span style="display:flex;"><span>        WorkerMessageFailedEvent<span style="color:#ff79c6">::</span><span style="color:#50fa7b">class</span>,
</span></span><span style="display:flex;"><span>        ConsoleErrorEvent<span style="color:#ff79c6">::</span><span style="color:#50fa7b">class</span>,
</span></span><span style="display:flex;"><span>    ];
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#ff79c6">public</span> <span style="color:#ff79c6">function</span> <span style="color:#50fa7b">getNodeType</span>()<span style="color:#ff79c6">:</span> string
</span></span><span style="display:flex;"><span>    {
</span></span><span style="display:flex;"><span>        <span style="color:#ff79c6">return</span> Catch_<span style="color:#ff79c6">::</span><span style="color:#50fa7b">class</span>;
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#ff79c6">public</span> <span style="color:#ff79c6">function</span> <span style="color:#50fa7b">processNode</span>(Node <span style="color:#8be9fd;font-style:italic">$node</span>, Scope <span style="color:#8be9fd;font-style:italic">$scope</span>)<span style="color:#ff79c6">:</span> <span style="color:#ff79c6">array</span>
</span></span><span style="display:flex;"><span>    {
</span></span><span style="display:flex;"><span>        <span style="color:#ff79c6">if</span> (<span style="color:#ff79c6">!</span><span style="color:#8be9fd;font-style:italic">$this</span><span style="color:#ff79c6">-&gt;</span><span style="color:#50fa7b">isApplicationCode</span>(<span style="color:#8be9fd;font-style:italic">$scope</span>) <span style="color:#ff79c6">||</span> <span style="color:#ff79c6">!</span><span style="color:#8be9fd;font-style:italic">$this</span><span style="color:#ff79c6">-&gt;</span><span style="color:#50fa7b">catchesGenericType</span>(<span style="color:#8be9fd;font-style:italic">$node</span>) <span style="color:#ff79c6">||</span> <span style="color:#8be9fd;font-style:italic">$this</span><span style="color:#ff79c6">-&gt;</span><span style="color:#50fa7b">rethrows</span>(<span style="color:#8be9fd;font-style:italic">$node</span>) <span style="color:#ff79c6">||</span> <span style="color:#8be9fd;font-style:italic">$this</span><span style="color:#ff79c6">-&gt;</span><span style="color:#50fa7b">isExceptionHandlingCode</span>(<span style="color:#8be9fd;font-style:italic">$scope</span>)) {
</span></span><span style="display:flex;"><span>            <span style="color:#ff79c6">return</span> [];
</span></span><span style="display:flex;"><span>        }
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>        <span style="color:#ff79c6">return</span> [
</span></span><span style="display:flex;"><span>            RuleErrorBuilder<span style="color:#ff79c6">::</span><span style="color:#50fa7b">message</span>(<span style="color:#f1fa8c">&#39;Do not catch \Exception or \Throwable. Catch the specific exception you can handle and let the rest surface.&#39;</span>)
</span></span><span style="display:flex;"><span>                <span style="color:#ff79c6">-&gt;</span><span style="color:#50fa7b">identifier</span>(<span style="color:#f1fa8c">&#39;exception.genericCatch&#39;</span>)
</span></span><span style="display:flex;"><span>                <span style="color:#ff79c6">-&gt;</span><span style="color:#50fa7b">build</span>(),
</span></span><span style="display:flex;"><span>        ];
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#ff79c6">private</span> <span style="color:#ff79c6">function</span> <span style="color:#50fa7b">isApplicationCode</span>(Scope <span style="color:#8be9fd;font-style:italic">$scope</span>)<span style="color:#ff79c6">:</span> bool
</span></span><span style="display:flex;"><span>    {
</span></span><span style="display:flex;"><span>        <span style="color:#ff79c6">return</span> \str_starts_with(<span style="color:#8be9fd;font-style:italic">$scope</span><span style="color:#ff79c6">-&gt;</span><span style="color:#50fa7b">getNamespace</span>() <span style="color:#ff79c6">??</span> <span style="color:#f1fa8c">&#39;&#39;</span>, <span style="color:#f1fa8c">&#39;App\\&#39;</span>);
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#ff79c6">private</span> <span style="color:#ff79c6">function</span> <span style="color:#50fa7b">catchesGenericType</span>(Catch_ <span style="color:#8be9fd;font-style:italic">$catch</span>)<span style="color:#ff79c6">:</span> bool
</span></span><span style="display:flex;"><span>    {
</span></span><span style="display:flex;"><span>        <span style="color:#ff79c6">return</span> \array_any(<span style="color:#8be9fd;font-style:italic">$catch</span><span style="color:#ff79c6">-&gt;</span><span style="color:#50fa7b">types</span>, <span style="color:#ff79c6">static</span> fn (Name <span style="color:#8be9fd;font-style:italic">$type</span>)<span style="color:#ff79c6">:</span> bool <span style="color:#ff79c6">=&gt;</span> \in_array(<span style="color:#8be9fd;font-style:italic">$type</span><span style="color:#ff79c6">-&gt;</span><span style="color:#50fa7b">toString</span>(), self<span style="color:#ff79c6">::</span><span style="color:#50fa7b">GENERIC_TYPES</span>, <span style="color:#ff79c6">true</span>));
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#ff79c6">private</span> <span style="color:#ff79c6">function</span> <span style="color:#50fa7b">rethrows</span>(Catch_ <span style="color:#8be9fd;font-style:italic">$catch</span>)<span style="color:#ff79c6">:</span> bool
</span></span><span style="display:flex;"><span>    {
</span></span><span style="display:flex;"><span>        <span style="color:#8be9fd;font-style:italic">$thrown</span> <span style="color:#ff79c6">=</span> <span style="color:#8be9fd;font-style:italic">$this</span><span style="color:#ff79c6">-&gt;</span><span style="color:#50fa7b">thrownAtEnd</span>(<span style="color:#8be9fd;font-style:italic">$catch</span>);
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>        <span style="color:#ff79c6">return</span> <span style="color:#8be9fd;font-style:italic">$thrown</span> <span style="color:#ff79c6">!==</span> <span style="color:#ff79c6">null</span> <span style="color:#ff79c6">&amp;&amp;</span> <span style="color:#8be9fd;font-style:italic">$catch</span><span style="color:#ff79c6">-&gt;</span><span style="color:#50fa7b">var</span> <span style="color:#ff79c6">!==</span> <span style="color:#ff79c6">null</span> <span style="color:#ff79c6">&amp;&amp;</span> <span style="color:#8be9fd;font-style:italic">$this</span><span style="color:#ff79c6">-&gt;</span><span style="color:#50fa7b">carriesCaughtException</span>(<span style="color:#8be9fd;font-style:italic">$thrown</span>, <span style="color:#8be9fd;font-style:italic">$catch</span><span style="color:#ff79c6">-&gt;</span><span style="color:#50fa7b">var</span>);
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#ff79c6">private</span> <span style="color:#ff79c6">function</span> <span style="color:#50fa7b">thrownAtEnd</span>(Catch_ <span style="color:#8be9fd;font-style:italic">$catch</span>)<span style="color:#ff79c6">:</span> <span style="color:#ff79c6">?</span>Expr
</span></span><span style="display:flex;"><span>    {
</span></span><span style="display:flex;"><span>        <span style="color:#8be9fd;font-style:italic">$lastStatement</span> <span style="color:#ff79c6">=</span> \array_last(<span style="color:#8be9fd;font-style:italic">$catch</span><span style="color:#ff79c6">-&gt;</span><span style="color:#50fa7b">stmts</span>);
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>        <span style="color:#ff79c6">return</span> <span style="color:#8be9fd;font-style:italic">$lastStatement</span> instanceof Expression <span style="color:#ff79c6">&amp;&amp;</span> <span style="color:#8be9fd;font-style:italic">$lastStatement</span><span style="color:#ff79c6">-&gt;</span><span style="color:#50fa7b">expr</span> instanceof Throw_ <span style="color:#ff79c6">?</span> <span style="color:#8be9fd;font-style:italic">$lastStatement</span><span style="color:#ff79c6">-&gt;</span><span style="color:#50fa7b">expr</span><span style="color:#ff79c6">-&gt;</span><span style="color:#50fa7b">expr</span> <span style="color:#ff79c6">:</span> <span style="color:#ff79c6">null</span>;
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#ff79c6">private</span> <span style="color:#ff79c6">function</span> <span style="color:#50fa7b">carriesCaughtException</span>(Expr <span style="color:#8be9fd;font-style:italic">$thrown</span>, Variable <span style="color:#8be9fd;font-style:italic">$caught</span>)<span style="color:#ff79c6">:</span> bool
</span></span><span style="display:flex;"><span>    {
</span></span><span style="display:flex;"><span>        <span style="color:#ff79c6">if</span> (<span style="color:#8be9fd;font-style:italic">$thrown</span> instanceof Variable) {
</span></span><span style="display:flex;"><span>            <span style="color:#ff79c6">return</span> <span style="color:#8be9fd;font-style:italic">$thrown</span><span style="color:#ff79c6">-&gt;</span><span style="color:#50fa7b">name</span> <span style="color:#ff79c6">===</span> <span style="color:#8be9fd;font-style:italic">$caught</span><span style="color:#ff79c6">-&gt;</span><span style="color:#50fa7b">name</span>;
</span></span><span style="display:flex;"><span>        }
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>        <span style="color:#ff79c6">return</span> <span style="color:#8be9fd;font-style:italic">$thrown</span> instanceof New_
</span></span><span style="display:flex;"><span>            <span style="color:#ff79c6">&amp;&amp;</span> \array_any(<span style="color:#8be9fd;font-style:italic">$thrown</span><span style="color:#ff79c6">-&gt;</span><span style="color:#50fa7b">args</span>, <span style="color:#ff79c6">static</span> fn (Node <span style="color:#8be9fd;font-style:italic">$argument</span>)<span style="color:#ff79c6">:</span> bool <span style="color:#ff79c6">=&gt;</span> <span style="color:#8be9fd;font-style:italic">$argument</span> instanceof Arg <span style="color:#ff79c6">&amp;&amp;</span> <span style="color:#8be9fd;font-style:italic">$argument</span><span style="color:#ff79c6">-&gt;</span><span style="color:#50fa7b">value</span> instanceof Variable <span style="color:#ff79c6">&amp;&amp;</span> <span style="color:#8be9fd;font-style:italic">$argument</span><span style="color:#ff79c6">-&gt;</span><span style="color:#50fa7b">value</span><span style="color:#ff79c6">-&gt;</span><span style="color:#50fa7b">name</span> <span style="color:#ff79c6">===</span> <span style="color:#8be9fd;font-style:italic">$caught</span><span style="color:#ff79c6">-&gt;</span><span style="color:#50fa7b">name</span>);
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#ff79c6">private</span> <span style="color:#ff79c6">function</span> <span style="color:#50fa7b">isExceptionHandlingCode</span>(Scope <span style="color:#8be9fd;font-style:italic">$scope</span>)<span style="color:#ff79c6">:</span> bool
</span></span><span style="display:flex;"><span>    {
</span></span><span style="display:flex;"><span>        <span style="color:#ff79c6">return</span> <span style="color:#8be9fd;font-style:italic">$this</span><span style="color:#ff79c6">-&gt;</span><span style="color:#50fa7b">isRetryStrategy</span>(<span style="color:#8be9fd;font-style:italic">$scope</span>) <span style="color:#ff79c6">||</span> <span style="color:#8be9fd;font-style:italic">$this</span><span style="color:#ff79c6">-&gt;</span><span style="color:#50fa7b">handlesExceptionEvent</span>(<span style="color:#8be9fd;font-style:italic">$scope</span>);
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#ff79c6">private</span> <span style="color:#ff79c6">function</span> <span style="color:#50fa7b">isRetryStrategy</span>(Scope <span style="color:#8be9fd;font-style:italic">$scope</span>)<span style="color:#ff79c6">:</span> bool
</span></span><span style="display:flex;"><span>    {
</span></span><span style="display:flex;"><span>        <span style="color:#8be9fd;font-style:italic">$class</span> <span style="color:#ff79c6">=</span> <span style="color:#8be9fd;font-style:italic">$scope</span><span style="color:#ff79c6">-&gt;</span><span style="color:#50fa7b">getClassReflection</span>();
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>        <span style="color:#ff79c6">return</span> <span style="color:#8be9fd;font-style:italic">$class</span> <span style="color:#ff79c6">!==</span> <span style="color:#ff79c6">null</span> <span style="color:#ff79c6">&amp;&amp;</span> <span style="color:#8be9fd;font-style:italic">$class</span><span style="color:#ff79c6">-&gt;</span><span style="color:#50fa7b">is</span>(RetryStrategyInterface<span style="color:#ff79c6">::</span><span style="color:#50fa7b">class</span>);
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#ff79c6">private</span> <span style="color:#ff79c6">function</span> <span style="color:#50fa7b">handlesExceptionEvent</span>(Scope <span style="color:#8be9fd;font-style:italic">$scope</span>)<span style="color:#ff79c6">:</span> bool
</span></span><span style="display:flex;"><span>    {
</span></span><span style="display:flex;"><span>        <span style="color:#8be9fd;font-style:italic">$function</span> <span style="color:#ff79c6">=</span> <span style="color:#8be9fd;font-style:italic">$scope</span><span style="color:#ff79c6">-&gt;</span><span style="color:#50fa7b">getFunction</span>();
</span></span><span style="display:flex;"><span>        <span style="color:#ff79c6">if</span> (<span style="color:#8be9fd;font-style:italic">$function</span> <span style="color:#ff79c6">===</span> <span style="color:#ff79c6">null</span>) {
</span></span><span style="display:flex;"><span>            <span style="color:#ff79c6">return</span> <span style="color:#ff79c6">false</span>;
</span></span><span style="display:flex;"><span>        }
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>        <span style="color:#ff79c6">foreach</span> (<span style="color:#8be9fd;font-style:italic">$function</span><span style="color:#ff79c6">-&gt;</span><span style="color:#50fa7b">getVariants</span>() <span style="color:#ff79c6">as</span> <span style="color:#8be9fd;font-style:italic">$variant</span>) {
</span></span><span style="display:flex;"><span>            <span style="color:#ff79c6">foreach</span> (<span style="color:#8be9fd;font-style:italic">$variant</span><span style="color:#ff79c6">-&gt;</span><span style="color:#50fa7b">getParameters</span>() <span style="color:#ff79c6">as</span> <span style="color:#8be9fd;font-style:italic">$parameter</span>) {
</span></span><span style="display:flex;"><span>                <span style="color:#ff79c6">if</span> (\array_any(self<span style="color:#ff79c6">::</span><span style="color:#50fa7b">EXCEPTION_HANDLING_EVENTS</span>, <span style="color:#ff79c6">static</span> fn (string <span style="color:#8be9fd;font-style:italic">$event</span>)<span style="color:#ff79c6">:</span> bool <span style="color:#ff79c6">=&gt;</span> (<span style="color:#ff79c6">new</span> ObjectType(<span style="color:#8be9fd;font-style:italic">$event</span>))<span style="color:#ff79c6">-&gt;</span><span style="color:#50fa7b">isSuperTypeOf</span>(<span style="color:#8be9fd;font-style:italic">$parameter</span><span style="color:#ff79c6">-&gt;</span><span style="color:#50fa7b">getType</span>())<span style="color:#ff79c6">-&gt;</span><span style="color:#50fa7b">yes</span>())) {
</span></span><span style="display:flex;"><span>                    <span style="color:#ff79c6">return</span> <span style="color:#ff79c6">true</span>;
</span></span><span style="display:flex;"><span>                }
</span></span><span style="display:flex;"><span>            }
</span></span><span style="display:flex;"><span>        }
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>        <span style="color:#ff79c6">return</span> <span style="color:#ff79c6">false</span>;
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><p><code>processNode()</code> reads like the failure class: a generic catch in application code is a violation, unless it rethrows, or unless the code around it <em>is</em> exception handling: a Symfony exception listener or a Messenger retry strategy. Those exist to deal with arbitrary failures.</p>
<p>It did not look like this on the first day. I&rsquo;ll come back to that.</p>
<h2 id="a-sensor-is-code-test-it-like-code">A sensor is code. Test it like code.</h2>
<p>A custom rule is part of the harness. If it is wrong, the agent gets wrong feedback. So I test it like production code.</p>
<p>A rule can go wrong in three ways:</p>
<ul>
<li>A <strong>false alarm</strong>: the rule reports code that is fine.</li>
<li>A <strong>blind spot</strong>: the rule misses code that should fail.</li>
<li>A <strong>useless message</strong>: the rule is right, but the agent cannot tell what to change.</li>
</ul>
<p>So every rule gets a test. It runs the rule against one small PHP file that holds both: code that must pass and code that must fail. And it checks the exact message and the exact line.</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#282a36;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"><code class="language-php" data-lang="php"><span style="display:flex;"><span><span style="color:#6272a4">// app/ci/phpstan/Tests/Rules/NoGenericExceptionCatchRule/NoGenericExceptionCatchRuleTest.php
</span></span></span><span style="display:flex;"><span><span style="color:#ff79c6">namespace</span> CI\PhpStan\Tests\Rules\NoGenericExceptionCatchRule;
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#ff79c6">use</span> CI\PhpStan\Rules\NoGenericExceptionCatchRule;
</span></span><span style="display:flex;"><span><span style="color:#ff79c6">use</span> PHPStan\Rules\Rule;
</span></span><span style="display:flex;"><span><span style="color:#ff79c6">use</span> PHPStan\Testing\RuleTestCase;
</span></span><span style="display:flex;"><span><span style="color:#ff79c6">use</span> PHPUnit\Framework\Attributes\CoversNothing;
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#f1fa8c">/**
</span></span></span><span style="display:flex;"><span><span style="color:#f1fa8c"> * @extends RuleTestCase&lt;NoGenericExceptionCatchRule&gt;
</span></span></span><span style="display:flex;"><span><span style="color:#f1fa8c"> */</span>
</span></span><span style="display:flex;"><span>#[CoversNothing]
</span></span><span style="display:flex;"><span><span style="color:#ff79c6">final</span> <span style="color:#ff79c6">class</span> <span style="color:#50fa7b">NoGenericExceptionCatchRuleTest</span> <span style="color:#ff79c6">extends</span> RuleTestCase
</span></span><span style="display:flex;"><span>{
</span></span><span style="display:flex;"><span>    <span style="color:#ff79c6">public</span> <span style="color:#ff79c6">function</span> <span style="color:#50fa7b">test_rule</span>()<span style="color:#ff79c6">:</span> void
</span></span><span style="display:flex;"><span>    {
</span></span><span style="display:flex;"><span>        <span style="color:#8be9fd;font-style:italic">$message</span> <span style="color:#ff79c6">=</span> <span style="color:#f1fa8c">&#39;Do not catch \Exception or \Throwable. Catch the specific exception you can handle and let the rest surface.&#39;</span>;
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>        <span style="color:#8be9fd;font-style:italic">$this</span><span style="color:#ff79c6">-&gt;</span><span style="color:#50fa7b">analyse</span>(
</span></span><span style="display:flex;"><span>            [__DIR__ <span style="color:#ff79c6">.</span> <span style="color:#f1fa8c">&#39;/data/no-generic-exception-catch.php&#39;</span>],
</span></span><span style="display:flex;"><span>            [
</span></span><span style="display:flex;"><span>                [<span style="color:#8be9fd;font-style:italic">$message</span>, <span style="color:#bd93f9">23</span>],
</span></span><span style="display:flex;"><span>                [<span style="color:#8be9fd;font-style:italic">$message</span>, <span style="color:#bd93f9">28</span>],
</span></span><span style="display:flex;"><span>                [<span style="color:#8be9fd;font-style:italic">$message</span>, <span style="color:#bd93f9">33</span>],
</span></span><span style="display:flex;"><span>                [<span style="color:#8be9fd;font-style:italic">$message</span>, <span style="color:#bd93f9">105</span>],
</span></span><span style="display:flex;"><span>            ],
</span></span><span style="display:flex;"><span>        );
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#ff79c6">protected</span> <span style="color:#ff79c6">function</span> <span style="color:#50fa7b">getRule</span>()<span style="color:#ff79c6">:</span> Rule
</span></span><span style="display:flex;"><span>    {
</span></span><span style="display:flex;"><span>        <span style="color:#ff79c6">return</span> <span style="color:#ff79c6">new</span> NoGenericExceptionCatchRule();
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><p>The fixture is plain PHP. Every case in it is a decision about what the rule should and should not see. Here is an excerpt:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#282a36;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"><code class="language-php" data-lang="php"><span style="display:flex;"><span><span style="color:#6272a4">// app/ci/phpstan/Tests/Rules/NoGenericExceptionCatchRule/data/no-generic-exception-catch.php
</span></span></span><span style="display:flex;"><span><span style="color:#ff79c6">final</span> <span style="color:#ff79c6">class</span> <span style="color:#50fa7b">Controller</span>
</span></span><span style="display:flex;"><span>{
</span></span><span style="display:flex;"><span>    <span style="color:#ff79c6">public</span> <span style="color:#ff79c6">function</span> <span style="color:#50fa7b">plain</span>()<span style="color:#ff79c6">:</span> void
</span></span><span style="display:flex;"><span>    {
</span></span><span style="display:flex;"><span>        <span style="color:#ff79c6">try</span> {
</span></span><span style="display:flex;"><span>            \strlen(<span style="color:#f1fa8c">&#39;x&#39;</span>);
</span></span><span style="display:flex;"><span>        } <span style="color:#ff79c6">catch</span> (\Throwable <span style="color:#8be9fd;font-style:italic">$e</span>) {                    <span style="color:#6272a4">// fails
</span></span></span><span style="display:flex;"><span>        }
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>        <span style="color:#ff79c6">try</span> {
</span></span><span style="display:flex;"><span>            \strlen(<span style="color:#f1fa8c">&#39;x&#39;</span>);
</span></span><span style="display:flex;"><span>        } <span style="color:#ff79c6">catch</span> (YoutubeRateLimitException <span style="color:#ff79c6">|</span> \Throwable <span style="color:#8be9fd;font-style:italic">$e</span>) {  <span style="color:#6272a4">// fails
</span></span></span><span style="display:flex;"><span>        }
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#ff79c6">public</span> <span style="color:#ff79c6">function</span> <span style="color:#50fa7b">specific</span>()<span style="color:#ff79c6">:</span> void
</span></span><span style="display:flex;"><span>    {
</span></span><span style="display:flex;"><span>        <span style="color:#ff79c6">try</span> {
</span></span><span style="display:flex;"><span>            \strlen(<span style="color:#f1fa8c">&#39;x&#39;</span>);
</span></span><span style="display:flex;"><span>        } <span style="color:#ff79c6">catch</span> (YoutubeRateLimitException <span style="color:#8be9fd;font-style:italic">$e</span>) {     <span style="color:#6272a4">// passes
</span></span></span><span style="display:flex;"><span>        }
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#ff79c6">final</span> <span style="color:#ff79c6">class</span> <span style="color:#50fa7b">Rethrower</span>
</span></span><span style="display:flex;"><span>{
</span></span><span style="display:flex;"><span>    <span style="color:#ff79c6">public</span> <span style="color:#ff79c6">function</span> <span style="color:#50fa7b">rethrow</span>()<span style="color:#ff79c6">:</span> void
</span></span><span style="display:flex;"><span>    {
</span></span><span style="display:flex;"><span>        <span style="color:#ff79c6">try</span> {
</span></span><span style="display:flex;"><span>            \strlen(<span style="color:#f1fa8c">&#39;x&#39;</span>);
</span></span><span style="display:flex;"><span>        } <span style="color:#ff79c6">catch</span> (\Throwable <span style="color:#8be9fd;font-style:italic">$e</span>) {                    <span style="color:#6272a4">// passes
</span></span></span><span style="display:flex;"><span>            \strlen(<span style="color:#f1fa8c">&#39;record&#39;</span>);
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>            <span style="color:#ff79c6">throw</span> <span style="color:#8be9fd;font-style:italic">$e</span>;
</span></span><span style="display:flex;"><span>        }
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#ff79c6">public</span> <span style="color:#ff79c6">function</span> <span style="color:#50fa7b">wrapWithPrevious</span>()<span style="color:#ff79c6">:</span> void
</span></span><span style="display:flex;"><span>    {
</span></span><span style="display:flex;"><span>        <span style="color:#ff79c6">try</span> {
</span></span><span style="display:flex;"><span>            \strlen(<span style="color:#f1fa8c">&#39;x&#39;</span>);
</span></span><span style="display:flex;"><span>        } <span style="color:#ff79c6">catch</span> (\Exception <span style="color:#8be9fd;font-style:italic">$e</span>) {                    <span style="color:#6272a4">// passes
</span></span></span><span style="display:flex;"><span>            <span style="color:#ff79c6">throw</span> <span style="color:#ff79c6">new</span> \RuntimeException(<span style="color:#f1fa8c">&#39;wrapped&#39;</span>, previous<span style="color:#ff79c6">:</span> <span style="color:#8be9fd;font-style:italic">$e</span>);
</span></span><span style="display:flex;"><span>        }
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#ff79c6">public</span> <span style="color:#ff79c6">function</span> <span style="color:#50fa7b">wrapWithoutPrevious</span>()<span style="color:#ff79c6">:</span> void
</span></span><span style="display:flex;"><span>    {
</span></span><span style="display:flex;"><span>        <span style="color:#ff79c6">try</span> {
</span></span><span style="display:flex;"><span>            \strlen(<span style="color:#f1fa8c">&#39;x&#39;</span>);
</span></span><span style="display:flex;"><span>        } <span style="color:#ff79c6">catch</span> (\Exception <span style="color:#8be9fd;font-style:italic">$e</span>) {                    <span style="color:#6272a4">// fails
</span></span></span><span style="display:flex;"><span>            <span style="color:#ff79c6">throw</span> <span style="color:#ff79c6">new</span> \RuntimeException(<span style="color:#8be9fd;font-style:italic">$e</span><span style="color:#ff79c6">-&gt;</span><span style="color:#50fa7b">getMessage</span>());
</span></span><span style="display:flex;"><span>        }
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><p>Look at the last case. Wrapping the exception without passing it as <code>previous</code> throws something, but it still loses the original stack trace. The rule treats that as hiding.</p>
<h3 id="a-sensor-for-the-sensors">A sensor for the sensors</h3>
<p>Forgetting that test is easy. So one more test watches for that. It fails when a rule has no test:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#282a36;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"><code class="language-php" data-lang="php"><span style="display:flex;"><span><span style="color:#6272a4">// app/ci/phpstan/Tests/RuleTestCoverageTest.php
</span></span></span><span style="display:flex;"><span><span style="color:#ff79c6">namespace</span> CI\PhpStan\Tests;
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#ff79c6">use</span> PHPUnit\Framework\Attributes\CoversNothing;
</span></span><span style="display:flex;"><span><span style="color:#ff79c6">use</span> PHPUnit\Framework\TestCase;
</span></span><span style="display:flex;"><span><span style="color:#ff79c6">use</span> Webmozart\Assert\Assert;
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>#[CoversNothing]
</span></span><span style="display:flex;"><span><span style="color:#ff79c6">final</span> <span style="color:#ff79c6">class</span> <span style="color:#50fa7b">RuleTestCoverageTest</span> <span style="color:#ff79c6">extends</span> TestCase
</span></span><span style="display:flex;"><span>{
</span></span><span style="display:flex;"><span>    <span style="color:#ff79c6">public</span> <span style="color:#ff79c6">function</span> <span style="color:#50fa7b">test_every_rule_has_a_test</span>()<span style="color:#ff79c6">:</span> void
</span></span><span style="display:flex;"><span>    {
</span></span><span style="display:flex;"><span>        <span style="color:#8be9fd;font-style:italic">$rulesDir</span> <span style="color:#ff79c6">=</span> \dirname(__DIR__) <span style="color:#ff79c6">.</span> <span style="color:#f1fa8c">&#39;/Rules&#39;</span>;
</span></span><span style="display:flex;"><span>        <span style="color:#8be9fd;font-style:italic">$testsDir</span> <span style="color:#ff79c6">=</span> __DIR__ <span style="color:#ff79c6">.</span> <span style="color:#f1fa8c">&#39;/Rules&#39;</span>;
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>        <span style="color:#8be9fd;font-style:italic">$ruleFiles</span> <span style="color:#ff79c6">=</span> \glob(<span style="color:#8be9fd;font-style:italic">$rulesDir</span> <span style="color:#ff79c6">.</span> <span style="color:#f1fa8c">&#39;/*Rule.php&#39;</span>);
</span></span><span style="display:flex;"><span>        Assert<span style="color:#ff79c6">::</span><span style="color:#50fa7b">notFalse</span>(<span style="color:#8be9fd;font-style:italic">$ruleFiles</span>);
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>        <span style="color:#8be9fd;font-style:italic">$missing</span> <span style="color:#ff79c6">=</span> [];
</span></span><span style="display:flex;"><span>        <span style="color:#ff79c6">foreach</span> (<span style="color:#8be9fd;font-style:italic">$ruleFiles</span> <span style="color:#ff79c6">as</span> <span style="color:#8be9fd;font-style:italic">$ruleFile</span>) {
</span></span><span style="display:flex;"><span>            <span style="color:#8be9fd;font-style:italic">$name</span> <span style="color:#ff79c6">=</span> \basename(<span style="color:#8be9fd;font-style:italic">$ruleFile</span>, <span style="color:#f1fa8c">&#39;.php&#39;</span>);
</span></span><span style="display:flex;"><span>            <span style="color:#ff79c6">if</span> (<span style="color:#ff79c6">!</span>\is_file(<span style="color:#8be9fd;font-style:italic">$testsDir</span> <span style="color:#ff79c6">.</span> <span style="color:#f1fa8c">&#39;/&#39;</span> <span style="color:#ff79c6">.</span> <span style="color:#8be9fd;font-style:italic">$name</span> <span style="color:#ff79c6">.</span> <span style="color:#f1fa8c">&#39;/&#39;</span> <span style="color:#ff79c6">.</span> <span style="color:#8be9fd;font-style:italic">$name</span> <span style="color:#ff79c6">.</span> <span style="color:#f1fa8c">&#39;Test.php&#39;</span>)) {
</span></span><span style="display:flex;"><span>                <span style="color:#8be9fd;font-style:italic">$missing</span>[] <span style="color:#ff79c6">=</span> <span style="color:#8be9fd;font-style:italic">$name</span>;
</span></span><span style="display:flex;"><span>            }
</span></span><span style="display:flex;"><span>        }
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>        self<span style="color:#ff79c6">::</span><span style="color:#50fa7b">assertSame</span>(
</span></span><span style="display:flex;"><span>            [],
</span></span><span style="display:flex;"><span>            <span style="color:#8be9fd;font-style:italic">$missing</span>,
</span></span><span style="display:flex;"><span>            \sprintf(
</span></span><span style="display:flex;"><span>                <span style="color:#f1fa8c">&#39;Missing RuleTestCase test(s) for: %s&#39;</span>,
</span></span><span style="display:flex;"><span>                \implode(<span style="color:#f1fa8c">&#39;, &#39;</span>, <span style="color:#8be9fd;font-style:italic">$missing</span>)
</span></span><span style="display:flex;"><span>            )
</span></span><span style="display:flex;"><span>        );
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><p>It does not tell me whether a test is <em>good</em>. But it enforces one useful rule: every PHPStan rule has its own test.</p>
<h2 id="on-success-silence-on-failure-direction">On success: silence. On failure: direction.</h2>
<p>This is what the agent sees now when it writes that catch:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#282a36;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"><code class="language-text" data-lang="text"><span style="display:flex;"><span>$ make ci
</span></span><span style="display:flex;"><span>cs-check   ✓
</span></span><span style="display:flex;"><span>rector     ✓
</span></span><span style="display:flex;"><span>phpstan    ✗
</span></span><span style="display:flex;"><span>  FetchVideoDetailsController.php
</span></span><span style="display:flex;"><span>  Do not catch \Exception or \Throwable. Catch the specific exception
</span></span><span style="display:flex;"><span>  you can handle and let the rest surface.
</span></span></code></pre></div><p>One short line per tool. Then PHPStan fails, and the gate stops.</p>
<p>The message is not written for humans. It is written for the agent that has to fix the code. It does not only say what is wrong. It says what to do instead.</p>
<p>For the agent, the command output becomes part of its context. So I want two things from my gate:</p>
<ul>
<li><strong>On success: silence.</strong> A short line per tool is enough. Hundreds of lines saying that everything is fine only clutter the context.</li>
<li><strong>On failure: direction.</strong> Where is the problem, which rule failed, and what should the agent do next?</li>
</ul>
<p>The output of a quality tool, down to a single message, is part of the harness.</p>
<h2 id="the-old-code-was-full-of-this-pattern">The old code was full of this pattern</h2>
<p>Then I ran the new rule against the whole application. The old code was full of this pattern. Here is one of them. It is not a controller. It is a service that asks a graph database for statistics: how many chunks, entities and relationships there are.</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#282a36;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"><code class="language-php" data-lang="php"><span style="display:flex;"><span><span style="color:#ff79c6">try</span> {
</span></span><span style="display:flex;"><span>    <span style="color:#6272a4">// ask the graph database for statistics
</span></span></span><span style="display:flex;"><span>} <span style="color:#ff79c6">catch</span> (Throwable <span style="color:#8be9fd;font-style:italic">$e</span>) {
</span></span><span style="display:flex;"><span>    <span style="color:#8be9fd;font-style:italic">$this</span><span style="color:#ff79c6">-&gt;</span><span style="color:#50fa7b">logger</span><span style="color:#ff79c6">-&gt;</span><span style="color:#50fa7b">error</span>(<span style="color:#f1fa8c">&#39;Stats query failed&#39;</span>, [<span style="color:#6272a4">/* … */</span>]);
</span></span><span style="display:flex;"><span>    <span style="color:#ff79c6">return</span> [<span style="color:#f1fa8c">&#39;chunks&#39;</span> <span style="color:#ff79c6">=&gt;</span> <span style="color:#bd93f9">0</span>, <span style="color:#f1fa8c">&#39;entities&#39;</span> <span style="color:#ff79c6">=&gt;</span> <span style="color:#bd93f9">0</span>, <span style="color:#f1fa8c">&#39;relationships&#39;</span> <span style="color:#ff79c6">=&gt;</span> <span style="color:#bd93f9">0</span>];
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><p>If the query fails, it catches everything and logs the error. And then it returns zeros.</p>
<p>Think about what that means. The statistics now say: there is nothing. Not because the data is empty, but because the query failed. This catch does not only hide the error. It <strong>invents data</strong>.</p>
<p>But now we had a new problem. If we switched the rule on, the pipeline would be red before anyone changed a single line of code.</p>
<p>A sensor we cannot switch on does not help us yet.</p>
<h3 id="the-baseline">The baseline</h3>
<p>So we generated a <strong>PHPStan baseline</strong> and switched the rule on. Most of you know the baseline from raising the PHPStan level in an old project. Here it works as a ratchet:</p>
<ol>
<li>A <strong>known</strong> violation passes. <code>make ci</code> is green on the day we switch the rule on.</li>
<li>A <strong>new</strong> violation fails. We accept the old problems for now. We do not allow new ones.</li>
<li>A <strong>fixed</strong> violation has to leave the baseline, otherwise the check stays red. So the problem cannot come back unnoticed.</li>
</ol>
<p>The baseline is generated with <code>make ci-phpstan-baseline</code>, never edited by hand. Hand-appended entries end up in the wrong group, stay green, and move around on the next regeneration, hiding the real change in the diff.</p>
<h3 id="my-rule-was-wrong">My rule was wrong</h3>
<p>Then something happened that I had <strong>not</strong> planned. The rule went live, and the first thing we learned: some of them were not violations at all.</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#282a36;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"><code class="language-php" data-lang="php"><span style="display:flex;"><span>} <span style="color:#ff79c6">catch</span> (Exception <span style="color:#8be9fd;font-style:italic">$e</span>) {
</span></span><span style="display:flex;"><span>    <span style="color:#8be9fd;font-style:italic">$this</span><span style="color:#ff79c6">-&gt;</span><span style="color:#50fa7b">lifecycle</span><span style="color:#ff79c6">-&gt;</span><span style="color:#50fa7b">fail</span>(<span style="color:#8be9fd;font-style:italic">$generation</span>, <span style="color:#8be9fd;font-style:italic">$e</span><span style="color:#ff79c6">-&gt;</span><span style="color:#50fa7b">getMessage</span>());
</span></span><span style="display:flex;"><span>    <span style="color:#ff79c6">throw</span> <span style="color:#8be9fd;font-style:italic">$e</span>;
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><p>This one catches the exception, marks the job as failed, and then throws the exception again. The failure still surfaces. It still ends up in the log. Nothing is hidden.</p>
<p>The others were different. They caught the exception and then <strong>carried on</strong>: they returned zeros, showed a message, or simply continued. Those are the ones that hide failures.</p>
<p>So my rule was wrong. A false alarm, one of the three ways a sensor can go wrong.</p>
<p>And the rule was wrong because my <em>sentence</em> was wrong. The failure class &ldquo;catching <code>\Exception</code> hides failures&rdquo; is too broad. A condition was missing:</p>
<blockquote>
<p>Catching <code>\Exception</code> or <code>\Throwable</code> <em>without handling or rethrowing</em> hides failures.</p>
</blockquote>
<p>Catching is not the problem. Hiding is.</p>
<p>So I fixed the sentence first, and then the rule. The fixture got both cases: a catch that rethrows has to pass, a catch that carries on has to fail. That is the <code>Rethrower</code> class above.</p>
<div class="details admonition tip open">
  <div class="details-summary admonition-title">
    <i class="icon ">&#xf400;</i> I learned<i class="details-icon fas fa-angle-right fa-fw"></i>
  </div>
  <div class="details-content">
    <div class="admonition-content">Naming the failure class is the hard part. A false alarm is usually not a bug in the code of the rule. It is a bug in the sentence behind it.</div>
  </div>
</div>
<h3 id="gone-in-one-day">Gone in one day</h3>
<p>Then an agent worked through the rest. Most catches were removed completely. The failures now go where they belong: to Symfony&rsquo;s error handling. The others now catch the specific exception that the call really throws.</p>
<p>With every fix, the baseline entry had to go. The number went down, one fix at a time. All of it happened in one afternoon. The baseline was empty that evening. I reviewed everything and merged it the next day.</p>
<p>I don&rsquo;t open a ticket for every group of fixes anymore. Agents are good at this kind of work, and every run goes through the whole gate.</p>
<p>The limiting factor is not the agent anymore. It is my review attention.</p>
<h2 id="when-a-bad-change-gets-through">When a bad change gets through</h2>
<p>Let me take a step back. This is the method behind the story. It starts every time a bad change passes the gate:</p>
<ol>
<li><strong>Name the failure class.</strong> The pattern, not the incident.</li>
<li><strong>Can the gate see it?</strong> If an existing sensor can, improve that sensor. If not, build and test the smallest new sensor that can.</li>
<li><strong>Does old code fail?</strong> Put it into the baseline, switch the protection on now, and clean up step by step.</li>
</ol>
<p>And it does not matter who wrote the bad change.</p>
<h3 id="cleanup-takes-time-the-baseline-protects-now">Cleanup takes time. The baseline protects now.</h3>
<p>Same method, a different review finding. An agent wrote this code, and I was not paying attention. That is how it got in.</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#282a36;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"><code class="language-php" data-lang="php"><span style="display:flex;"><span><span style="color:#ff79c6">foreach</span> (<span style="color:#8be9fd;font-style:italic">$command</span><span style="color:#ff79c6">-&gt;</span><span style="color:#50fa7b">videoIds</span> <span style="color:#ff79c6">as</span> <span style="color:#8be9fd;font-style:italic">$videoId</span>) {
</span></span><span style="display:flex;"><span>    <span style="color:#ff79c6">if</span> (<span style="color:#8be9fd;font-style:italic">$this</span><span style="color:#ff79c6">-&gt;</span><span style="color:#50fa7b">videoRepository</span><span style="color:#ff79c6">-&gt;</span><span style="color:#50fa7b">findById</span>(<span style="color:#8be9fd;font-style:italic">$videoId</span>) <span style="color:#ff79c6">===</span> <span style="color:#ff79c6">null</span>) {
</span></span><span style="display:flex;"><span>        <span style="color:#6272a4">// …
</span></span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><p>One query to the database in every loop iteration. A classic <strong>N+1</strong>.</p>
<p>The failure class: <em>no repository read calls inside a loop</em>. The rule, <code>NoRepositoryCallInLoopRule</code>, found more places in the codebase.</p>
<p>And here is the difference to the catches. A catch is a local fix, and they were gone in one day. An N+1 needs different database queries. That takes attention, and it does not happen in an afternoon.</p>
<p>So most of these places are still in the baseline. And that is fine:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#282a36;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"><code class="language-text" data-lang="text"><span style="display:flex;"><span>phpstan  ✓  # 12 in baseline since August · 0 new
</span></span></code></pre></div><p>The protection is on anyway: twelve entries since then, and no new ones. That is what the baseline is for. It protects you even when the cleanup stalls.</p>
<h2 id="the-harness-grows-one-rule-at-a-time">The harness grows one rule at a time</h2>
<p>If you repeat this method, the harness grows. This is my project today:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#282a36;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"><code class="language-text" data-lang="text"><span style="display:flex;"><span>CommandHandlerMustDispatchDomainEvent     NoIsset
</span></span><span style="display:flex;"><span>CommandNameMustStartWithApp               NoMagicNumber
</span></span><span style="display:flex;"><span>CompareDateTime                           NonAbstractClassMustBeFinal
</span></span><span style="display:flex;"><span>ControllerMustNotExtendAbstractController NoNativeAssert
</span></span><span style="display:flex;"><span>ControllerMustNotInjectRepository         NoNewDoctrineRepositoryInTests
</span></span><span style="display:flex;"><span>ControllerMustResolveUserViaCurrentUser   NoNewInMemoryInTests
</span></span><span style="display:flex;"><span>ControllerNameMustStartWithUseCase        NoParameterPassedByReference
</span></span><span style="display:flex;"><span>CoverageAttributeMustTargetClass          NoProtectedMemberInFinalClass
</span></span><span style="display:flex;"><span>DbQueryOnlyInRepository                   NoRepositoryCallInLoop
</span></span><span style="display:flex;"><span>DoctrineRepositoryMustHaveTest            PreferGetServiceOverNewInTest
</span></span><span style="display:flex;"><span>EntityMustBeInMappedNamespace             PreferQueryBuilderOverDql
</span></span><span style="display:flex;"><span>IdValueObjectMustBeStringable             RepositoryMethodNaming
</span></span><span style="display:flex;"><span>ImmutableValueObject                      ServiceMustHaveReadonlyProperties
</span></span><span style="display:flex;"><span>InvokableCommandMustNotExtendCommand      SingleUseVariable
</span></span><span style="display:flex;"><span>KnownRoleInIsGranted                      TestClassNameMustEndWithTest
</span></span><span style="display:flex;"><span>MutationControllerMustBeGranted           TestHookMustCallParent
</span></span><span style="display:flex;"><span>NoErrorSuppression                        UseCaseMustNotDependOnOtherUseCase
</span></span><span style="display:flex;"><span>NoExtendServiceEntityRepository           WebTestMultiRequestMustDisableReboot
</span></span><span style="display:flex;"><span>NoGenericExceptionCatch
</span></span></code></pre></div><p>Every name is a failure class, written as a sentence.</p>
<p>You don&rsquo;t need mine. Most of them would not fit your project. But look where they come from. A few came from bugs and review findings, like the catch and the N+1. Most of them are conventions: things every senior in my team would say in a review.</p>
<p>Your team has those too. They live in your heads and in your review comments. They are just not executable yet.</p>
<p>And I did not start with all of them. They came one review finding at a time.</p>
<h2 id="getting-started">Getting started</h2>
<p>You don&rsquo;t need a new platform for any of this. Your existing CI is the starting point.</p>
<ol>
<li><strong>One command for the whole gate.</strong> I use <code>make ci</code>. Composer scripts or a shell script work just as well. What matters: it never waits for input, it returns the right exit code, it is fast, and it prints only useful output. My full gate takes seconds.</li>
<li><strong>The agent runs it before &ldquo;done&rdquo;.</strong> Put it into your <code>AGENTS.md</code>, and tell the agent to fix every finding: not ignore it, and not add it to the baseline. Don&rsquo;t make the agent wait for the remote pipeline. Let it run the same checks inside its own loop.</li>
<li><strong>Green but wrong? Ask a different question.</strong> You will notice that a change is wrong, because you know how your code should look. That is your experience. Your gate does not have it yet. So don&rsquo;t only ask why the agent wrote it. Ask: <em>what is invisible to my gate?</em> Then let the agent turn your experience into a rule that a machine can check.</li>
</ol>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#282a36;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"><code class="language-markdown" data-lang="markdown"><span style="display:flex;"><span><span style="color:#6272a4">&lt;!-- AGENTS.md --&gt;</span>
</span></span><span style="display:flex;"><span>Run <span style="color:#f1fa8c">`make ci`</span> before you say you are done. Fix every finding.
</span></span></code></pre></div><h2 id="the-bug-was-a-sensor-that-could-not-see-it">The bug was a sensor that could not see it</h2>
<p>Back to the beginning. The agent did what I asked. It solved the task. It ran the gate. And every check passed.</p>
<p>The catch made the gap visible to me. The rule made it visible to the agent.</p>
<p>The bug was not the catch. The bug was a sensor that could not see it.</p>
<p>Green is not done. Green only means: nothing that your sensors can see is wrong. So make your sensors see more.</p>
<p>The agent can write the rules. Knowing which rule to write is still <strong>our</strong> job.</p>
<h3 id="more-sources">More sources</h3>
<ul>
<li><a href="https://martinfowler.com/articles/harness-engineering.html">Harness Engineering for Coding Agent Users</a> by Birgitta Böckeler</li>
<li><a href="https://phpstan.org/developing-extensions/rules">PHPStan: Custom rules</a></li>
<li><a href="https://phpstan.org/user-guide/baseline">PHPStan: The baseline</a></li>
<li><a href="https://github.com/deptrac/deptrac">deptrac</a></li>
<li><a href="https://infection.github.io/">Infection</a></li>
<li><a href="https://docs.phpunit.de/">PHPUnit</a></li>
</ul>
<h3 id="slides">Slides</h3>
<ul>
<li><a href="https://speakerdeck.com/dazz/green-is-not-done">Slides of the talk</a></li>
</ul>
]]></description><media:thumbnail url="https://blog.dazzlog.de/hero.png"/></item></channel></rss>