Search by

ogmdev / symfony-quality-preset

ogmdev

Shared quality tooling for Symfony applications: PHPStan base configs, Rector and PHP CS Fixer config factories, and agent rules synced into AGENTS.md.

Package info

github.com/ogmdev/symfony-quality-preset

pkg:composer/ogmdev/symfony-quality-preset

Statistics

Installs: 2

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

v0.3.0 2026-12-02 21:38 UTC

This package is auto-updated.

Last update: 2026-12-02 21:49:56 UTC


README

Shared quality tooling for Symfony applications, so that several projects stop maintaining diverging copies of the same configs:

  • two PHPStan base configs: level 9 for the code, level 8 for tests/, no baseline;
  • a Rector config factory: PHP, Composer-based, attribute and prepared sets, the compiled container, common skips, cache, opt-in name imports, parallel runs;
  • a PHP CS Fixer config factory: @Symfony, @Symfony:risky, strict types and name imports;
  • agent rules, synced into the project's AGENTS.md.

Install

The preset needs PHP 8.4: its code uses PHP 8.4 syntax, and a Symfony application on PHP 8.2 or 8.3 cannot install it. It requires PHPStan 2.1, Rector 2.9 and PHP CS Fixer 3.75, or a later minor version of the same major: ^2.1, ^2.9 and ^3.75.

composer require --dev ogmdev/symfony-quality-preset --with-all-dependencies

--with-all-dependencies lets Composer raise a PHPStan, Rector or PHP CS Fixer that the project already locks below these versions; without it, the install fails on such a project.

It brings PHPStan with phpstan-symfony and phpstan-deprecation-rules, phpstan/extension-installer, Rector and PHP CS Fixer. Allow the extension installer in composer.json:

{
    "config": {
        "allow-plugins": {
            "phpstan/extension-installer": true
        }
    }
}

The extension installer loads every installed PHPStan extension, its rules included: the project's configs need no includes of vendor/phpstan/*. Add the ones that fit the project: phpstan/phpstan-doctrine, phpstan/phpstan-phpunit, phpstan/phpstan-strict-rules. vincentlanglet/twig-cs-fixer covers the Twig templates.

PHPStan and Rector read the compiled dev container: run bin/console cache:warmup --env=dev before them, in CI too.

File names

The configs take the names the Symfony Flex recipes create. The .dist file is versioned; the same name without .dist is a developer's local override, ignored by git. No script passes --config: each tool finds its own file, and the local override keeps working.

Tool Versioned Local, ignored
PHPStan phpstan.dist.neon phpstan.neon
PHPStan, tests phpstan.tests.dist.neon phpstan.tests.neon
PHP CS Fixer .php-cs-fixer.dist.php .php-cs-fixer.php
Rector rector.php —

phpstan.tests.dist.neon follows no recipe: it takes the same form, with the .tests variant written like .env.test.

Files Symfony owns

Rector and PHP CS Fixer leave alone the files that Symfony tooling writes, or that a Flex recipe copies for the project to keep as is. A rewrite there is undone by the next command or turns into a conflict at the next composer recipes:update:

File Written by
config/bundles.php Flex, at each composer require or remove
config/reference.php cache:warmup
importmap.php importmap:require, which rewrites the whole file
config/preload.php, public/index.php, tests/bootstrap.php their Flex recipe

src/Kernel.php also comes from a recipe, but the project edits it: both tools cover it. SymfonyOwnedFiles::PATHS holds the list both presets read.

PHPStan

The code:

# phpstan.dist.neon
includes:
    - vendor/ogmdev/symfony-quality-preset/config/phpstan.neon

parameters:
    paths:
        - src

The tests, in a second analysis:

# phpstan.tests.dist.neon
includes:
    - phpstan.dist.neon
    - vendor/ogmdev/symfony-quality-preset/config/phpstan-tests.neon

parameters:
    paths!:
        - tests
vendor/bin/phpstan analyse
vendor/bin/phpstan analyse --configuration=phpstan.tests.dist.neon

config/phpstan.neon sets:

  • level: 9;
  • reportUnmatchedIgnoredErrors: true, so an ignored error that no longer matches fails the analysis;
  • tmpDir to the project's var/cache/phpstan;
  • symfony.containerXmlPath to the project's var/cache/dev/App_KernelDevDebugContainer.xml.

config/phpstan-tests.neon sets level: 8 and tmpDir to var/cache/phpstan-tests, so that both analyses keep their own cache. Included after the project's phpstan.dist.neon, it replaces the level that file sets: a project at level 10 still analyses its tests at level 8. To raise the tests level, set level in phpstan.tests.dist.neon itself.

It also empties the ignoreErrors loaded before it: those of phpstan.dist.neon, and those a PHPStan extension ships, since PHPStan loads the extensions before the project's config. None of the extensions the preset installs or suggests ships any. The ignoreErrors of phpstan.dist.neon apply to the code only, and the tests analysis starts with none. An ignored error of the code rarely occurs in tests/, and reportUnmatchedIgnoredErrors would fail the tests analysis on it. A path naming one file spares an entry from that report, but paths, a directory or a glob does not. An error the tests must ignore goes in phpstan.tests.dist.neon, where it is still reported once it no longer matches.

Both paths are relative to the installed config, so they land in the project's var/cache/ whatever directory PHPStan runs from. They expect the default vendor directory. A project whose kernel lives elsewhere, such as a bundle with its test kernel under tests/Application/, sets its own symfony.containerXmlPath.

A project without the extension installer includes the extensions by hand, before the preset: phpstan-symfony/extension.neon sets symfony.containerXmlPath to null, and the last include wins.

The two levels are independent floors, 9 for the code and 8 for the tests: raising the code level leaves the tests at 8, and PHPStan offers no way to derive one level from the other. A project raises either floor in its own file, never lowers it. The baseline policy is to keep none: a new error is fixed, not baselined.

Rector

RectorPreset::configure() returns a RectorConfigBuilder. The project adds its paths, and its own skips and sets:

<?php

declare(strict_types=1);

use Ogmdev\SymfonyQualityPreset\RectorPreset;

return RectorPreset::configure(__DIR__)
    ->withPaths([
        __DIR__.'/src',
        __DIR__.'/tests',
    ]);

The preset:

  • applies the PHP sets of the php constraint in composer.json;
  • applies the Composer-based sets of the installed packages, detected by Rector: Symfony, Doctrine, Twig, PHPUnit;
  • turns annotations into attributes for Symfony, for Doctrine when doctrine/orm is installed and for PHPUnit when phpunit/phpunit is installed;
  • applies the prepared sets deadCode, codeQuality, typeDeclarations and symfonyCodeQuality, plus doctrineCodeQuality when doctrine/orm is installed;
  • reads the compiled container from var/cache/dev/App_KernelDevDebugContainer.xml, and refuses to run without it;
  • skips var/ and the files Symfony owns;
  • caches in var/cache/rector;
  • writes the class names it introduces fully qualified, and leaves PHP CS Fixer to import them. importNames: true stays for the projects that set it in 0.1: Rector then imports names outside docblocks itself;
  • runs 3 processes in parallel. Pass parallelProcesses: to change that, or chain ->withoutParallel().

It leaves out codingStyle, privatization and symfonyConfigs. A project that wants one chains a second withPreparedSets(), which adds to the first:

return RectorPreset::configure(__DIR__)
    ->withPreparedSets(privatization: true)
    ->withPaths([__DIR__.'/src']);

withSkip() adds to the preset's skips; it does not replace them.

Rector refuses some methods once the preset has called them, and fails the run with an InvalidConfigurationException:

Do not chain Because the preset already
withPhpSets(), with or without a version calls withPhpSets(), which Rector accepts only once
withPhpLevel() calls withPhpSets(), which Rector does not combine with it
withDeadCodeLevel(), withCodeQualityLevel(), withTypeCoverageLevel() applies the full deadCode, codeQuality and typeDeclarations sets they would duplicate

The PHP sets follow the php constraint of composer.json: change that constraint to target another version.

symfonyContainerXml: takes another container path, relative to the project root. A project that compiles no container, such as a bundle, passes symfonyContainerXml: null.

Rector reads composer.json and vendor/composer/installed.json from the working directory, so the preset refuses to run from anywhere but the project root it was given.

PHP CS Fixer

<?php

declare(strict_types=1);

use Ogmdev\SymfonyQualityPreset\PhpCsFixerPreset;

return PhpCsFixerPreset::configure(__DIR__);

The config:

  • applies @Symfony, @Symfony:risky and declare_strict_types;
  • imports every fully qualified class name outside the global namespace, those Rector introduces included: imports are the fixer's job;
  • turns static_lambda off when pestphp/pest is installed, since Pest binds $this to its test closures;
  • finds the PHP files of the project root and of the Castor tasks in .castor/, except var/, vendor/, public/assets/, assets/vendor/, the files Symfony owns and the other dot directories.

The project adds its own exclusions through finder(), and its own rules through setRules():

$config = PhpCsFixerPreset::configure(
    __DIR__,
    PhpCsFixerPreset::finder(__DIR__)->notPath('mate/extensions.php'),
);

return $config->setRules([
    ...$config->getRules(),
    'final_class' => true,
]);

@Symfony:risky holds psr_autoloading, which renames a class to match its file: declare test helper classes in their own autoloaded files, not in tests/Pest.php.

Agent rules

ai/quality.md is the quality contract that coding agents follow in every project. sync-agent-rules copies it into the project's AGENTS.md, between two markers:

<!-- BEGIN OGMDEV_QUALITY_PRESET -->
...
<!-- END OGMDEV_QUALITY_PRESET -->
vendor/bin/sync-agent-rules
vendor/bin/sync-agent-rules --check

Run from the project root, it:

  • appends the block to an AGENTS.md that has none, and creates the file if it is missing;
  • replaces the block on the next run, and leaves the rest of the file byte for byte, such as the Flex recipe section and the project's own;
  • writes and compares the block in the line ending of the block already there, or else of the file's first line: an AGENTS.md checked out in CRLF, as Git does on Windows with core.autocrlf, stays in CRLF and passes --check, and a CRLF line pasted into an LF file changes nothing;
  • refuses an AGENTS.md whose markers do not pair, without writing.

--check writes nothing, and exits non-zero when the block is missing or differs from the rules of ai/quality.md written with the project's commands. Run it in the project's quality task, so that a hand edit or an outdated block fails CI.

The block lives in AGENTS.md rather than in vendor/ because every agent reads AGENTS.md and none follows an import into vendor/. It stays versioned, so it is read in review.

The rules name two commands, which each project defines: the check command runs every check read-only, sync-agent-rules --check included; the fix command runs Rector, then PHP CS Fixer, so that the fixer formats what Rector writes. They default to the Castor tasks ./castor quality:check and ./castor quality:fix. A project that runs its checks another way names its own commands in composer.json:

{
    "extra": {
        "symfony-quality-preset": {
            "check-command": "composer quality",
            "fix-command": "composer quality:fix"
        }
    }
}

Either key can be left out to keep its default. ai/quality.md holds the placeholders %check-command% and %fix-command%: read the rules in the synced block, not in vendor/. sync-agent-rules writes these commands into the block, and --check fails when the block names other ones: run sync-agent-rules after changing them. It refuses an unknown key, and a command that is not a string, null included, that is blank, spans several lines, or holds a backtick or a marker of the block.

Adopt on an existing project

The preset offers no baseline and no progressive level: the zero-baseline policy holds from adoption, so the first green check comes once every error is fixed. Adopt it in this order, one commit per step, so that the review tells hand fixes from tool rewrites:

  1. Install the preset and write the configs above.

  2. Compile the container, which PHPStan and Rector read: bin/console cache:warmup --env=dev.

  3. Measure with PHPStan, before changing any code:

    vendor/bin/phpstan analyse
    vendor/bin/phpstan analyse --configuration=phpstan.tests.dist.neon
  4. Fix every error both analyses report.

  5. Run Rector: vendor/bin/rector process. It infers types through PHPStan, so it works on types PHPStan now accepts. Run PHPStan again after it.

  6. Run PHP CS Fixer: vendor/bin/php-cs-fixer fix, last, so that it formats what Rector wrote.

  7. Sync the agent rules with vendor/bin/sync-agent-rules, and define the check and fix commands they name.

Upgrade from 0.2

  • Change the constraint to ^0.3: under 1.0, ^0.2 excludes 0.3. The package is on Packagist from 0.3: drop the vcs repository that 0.2 needed.
  • The agent rules block does not change for a project that names no command: sync-agent-rules --check keeps passing without a sync. A project that does not run Castor names its commands in extra.symfony-quality-preset, then runs vendor/bin/sync-agent-rules.
  • The tests analysis no longer inherits the ignoreErrors of phpstan.dist.neon. A project that keeps none, as the policy asks, has nothing to do. Otherwise, repeat in phpstan.tests.dist.neon the entries that hide an error of tests/, and drop any reportUnmatched: false added only to keep the tests analysis green.

Upgrade from 0.1

  • Run Rector before PHP CS Fixer in the quality:fix Castor task.
  • Run vendor/bin/sync-agent-rules: the block in AGENTS.md changed, and --check fails until it is synced.
  • Run quality:fix once: the fixer now imports every fully qualified class name, those written by hand included.
  • Rector and PHP CS Fixer no longer touch config/preload.php, public/index.php, tests/bootstrap.php and importmap.php. Whatever 0.1 rewrote there stays: restore the recipe version if the project wants it back.

Development

composer install
composer test
composer phpstan
composer php-cs-fixer
composer rector

License

Apache License 2.0, see LICENSE.