AI engineering

A verification gate for AI-written PHP: lint, PHPStan, tests

5 min read

Green gate or not done

Hi folks,

The dangerous thing about AI-written PHP is not that it is bad. It is that it looks good. Clean names, nice docblocks, a confident summary at the end: “Implemented the repository method and updated the tests.” And then a method is called that does not exist in your TYPO3 version, or the test was never run.

I do not solve this with better prompts. I solve it with a gate: one command that must be green before anybody, human or agent, says “done”. This post shows the command and a Claude Code hook that enforces it.

What the gate checks

Four checks, from cheap to expensive:

  1. Syntax with php -l. Catches broken files before slower tools choke on them.
  2. Coding style with PHP-CS-Fixer in dry-run mode. It does not change anything, it only fails if something would change.
  3. Static analysis with PHPStan. This is the most valuable one for AI code, because it finds calls to methods and classes that do not exist, wrong argument types and missing return types.
  4. Tests with PHPUnit. Unit tests always, functional tests where the extension has them.

None of this is new. What is new is who runs it. When an agent writes the code, the agent must run the gate too, and show the output.

Step 1: One Composer script

Put the checks into composer.json of the extension or project. Composer adds vendor/bin to the PATH for scripts, and it stops at the first command that fails.

{
    "require-dev": {
        "friendsofphp/php-cs-fixer": "^3.64",
        "phpstan/phpstan": "^2.1",
        "phpunit/phpunit": "^11.5"
    },
    "scripts": {
        "gate:lint": "find Classes Configuration Tests -name '*.php' -print0 | xargs -0 -n1 php -l",
        "gate:cs": "php-cs-fixer fix --dry-run --diff",
        "gate:stan": "phpstan analyse --no-progress",
        "gate:test": "phpunit -c Build/phpunit/UnitTests.xml",
        "gate": [
            "@gate:lint",
            "@gate:cs",
            "@gate:stan",
            "@gate:test"
        ]
    },
    "scripts-descriptions": {
        "gate": "Lint, coding style, static analysis and tests. Must be green before done."
    }
}

The version constraints are examples; use what fits your PHP and TYPO3 versions. Inside DDEV, the whole gate is one line: ddev composer gate.

A minimal phpstan.neon to start with:

parameters:
  level: 6
  paths:
    - Classes
    - Configuration
    - Tests

Start at a level your existing code passes, then go up. A gate that is red from the beginning gets ignored, and then it is worth nothing. If you have old errors you cannot fix now, use a PHPStan baseline file, so that only new problems fail the gate.

Step 2: Tell the agent about it

The project instructions get one short rule:

## Definition of done
Run `ddev composer gate` after changing PHP files. Paste the last lines of
its output in your final message. If it is red, fix the cause; never weaken
a check, skip a test or add an ignore rule without asking.

The second sentence matters more than it looks. Without it, an agent under pressure may “fix” PHPStan by adding an ignore rule or “fix” a test by changing the expected value. Both make the gate green and the code wrong. Humans are tempted by the same shortcuts on a Friday afternoon, so this is not about blaming the model; it is about closing the door.

Step 3: A Stop hook that refuses to finish

Instructions are a request. A hook is a rule. Claude Code runs Stop hooks when the agent wants to end its turn. If the hook exits with code 2, Claude Code does not stop; it gives the hook’s stderr back to the model as the reason, and the agent continues.

The script, in .claude/hooks/gate-on-stop.sh:

#!/usr/bin/env bash
# Stop hook: keep Claude working while the verification gate is red.
cat > /dev/null   # hook input (JSON) is not needed here

cd "$CLAUDE_PROJECT_DIR" || exit 0

# Nothing to check when no PHP file changed or was added
if git diff --quiet HEAD -- '*.php' 
   && [ -z "$(git ls-files --others --exclude-standard -- '*.php')" ]; then
  exit 0
fi

if ! output=$(ddev composer gate 2>&1); then
  echo "The verification gate (ddev composer gate) is red. Fix the cause, do not weaken checks:" >&2
  echo "$output" | tail -n 40 >&2
  exit 2
fi
exit 0

And the registration in .claude/settings.json:

{
    "hooks": {
        "Stop": [
            {
                "hooks": [
                    {
                        "type": "command",
                        "command": ""$CLAUDE_PROJECT_DIR"/.claude/hooks/gate-on-stop.sh",
                        "timeout": 600
                    }
                ]
            }
        ]
    }
}

Make the script executable with chmod +x. The tail -n 40 keeps the feedback short: the agent needs the failing check and the first errors, not three hundred lines of PHPUnit dots.

Things to know before you switch it on

  • Loops are limited. The Stop hook input has a field stop_hook_active, which is true when Claude is already continuing because of a stop hook. Claude Code also caps how many times in a row stop hooks can continue the turn. So a gate that can never become green will not run forever, but it will waste time. Make sure the gate is green on a clean checkout first.
  • It must be fast enough. A gate that takes ten minutes after every small answer is annoying. Keep the heavy functional tests in CI, and only the fast checks in the hook, or skip the hook when no PHP file changed, as the script does.
  • It does not replace review. A green gate says “nothing obviously broken”. It does not say “this is the right solution”. I still read the diff. The gate only means I read it with less noise.

What changes with a gate

The final message is no longer a claim like “all tests pass”. It ends with the real tail of the gate output, and when something is red, the agent has to say what and why. The hook catches the cases where it would otherwise stop too early. And as a reviewer, you start from facts instead of from a nice summary.

It is a boring setup: four tools that PHP developers have used for years, one Composer script and a ten-line shell script. But boring is exactly what I want between AI-written code and a production server. Cheers!

Further reading

Leave a comment

Your email address will not be published.