ogmdev / symfony-quality-preset
Shared quality tooling for Symfony applications: PHPStan base configs, Rector and PHP CS Fixer config factories, and agent rules synced into AGENTS.md.
Requires
- php: ^8.4
- friendsofphp/php-cs-fixer: ^3.75
- phpstan/extension-installer: ^1.4
- phpstan/phpstan: ^2.1
- phpstan/phpstan-deprecation-rules: ^2.0
- phpstan/phpstan-symfony: ^2.1
- rector/rector: ^2.9
Requires (Dev)
- pestphp/pest: ^5.0
- pestphp/pest-plugin-phpstan: ^5.0
Suggests
- phpstan/phpstan-doctrine: Doctrine-aware PHPStan rules, loaded through phpstan/extension-installer
- phpstan/phpstan-phpunit: PHPUnit-aware PHPStan rules, loaded through phpstan/extension-installer
- phpstan/phpstan-strict-rules: Stricter PHPStan rules, loaded through phpstan/extension-installer
- vincentlanglet/twig-cs-fixer: A coding standard for Twig templates
Provides
None
Conflicts
None
Replaces
None
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;tmpDirto the project'svar/cache/phpstan;symfony.containerXmlPathto the project'svar/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
phpconstraint incomposer.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/ormis installed and for PHPUnit whenphpunit/phpunitis installed; - applies the prepared sets
deadCode,codeQuality,typeDeclarationsandsymfonyCodeQuality, plusdoctrineCodeQualitywhendoctrine/ormis 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: truestays 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:riskyanddeclare_strict_types; - imports every fully qualified class name outside the global namespace, those Rector introduces included: imports are the fixer's job;
- turns
static_lambdaoff whenpestphp/pestis installed, since Pest binds$thisto its test closures; - finds the PHP files of the project root and of the Castor tasks in
.castor/, exceptvar/,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.mdthat 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.mdchecked out in CRLF, as Git does on Windows withcore.autocrlf, stays in CRLF and passes--check, and a CRLF line pasted into an LF file changes nothing; - refuses an
AGENTS.mdwhose 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:
-
Install the preset and write the configs above.
-
Compile the container, which PHPStan and Rector read:
bin/console cache:warmup --env=dev. -
Measure with PHPStan, before changing any code:
vendor/bin/phpstan analyse vendor/bin/phpstan analyse --configuration=phpstan.tests.dist.neon
-
Fix every error both analyses report.
-
Run Rector:
vendor/bin/rector process. It infers types through PHPStan, so it works on types PHPStan now accepts. Run PHPStan again after it. -
Run PHP CS Fixer:
vendor/bin/php-cs-fixer fix, last, so that it formats what Rector wrote. -
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.2excludes 0.3. The package is on Packagist from 0.3: drop thevcsrepository that 0.2 needed. - The agent rules block does not change for a project that names no command:
sync-agent-rules --checkkeeps passing without a sync. A project that does not run Castor names its commands inextra.symfony-quality-preset, then runsvendor/bin/sync-agent-rules. - The tests analysis no longer inherits the
ignoreErrorsofphpstan.dist.neon. A project that keeps none, as the policy asks, has nothing to do. Otherwise, repeat inphpstan.tests.dist.neonthe entries that hide an error oftests/, and drop anyreportUnmatched: falseadded only to keep the tests analysis green.
Upgrade from 0.1
- Run Rector before PHP CS Fixer in the
quality:fixCastor task. - Run
vendor/bin/sync-agent-rules: the block inAGENTS.mdchanged, and--checkfails until it is synced. - Run
quality:fixonce: 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.phpandimportmap.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.