PHP-QA-CI uses two tools for coding standards enforcement, both running in Phase 1 (code modification) of the pipeline:
- Rector -- Automated refactoring and PHP version migration
- PHP CS Fixer -- Code style fixing
Both tools run before any linting or analysis, so that later phases validate the final state of the code.
PHP CS Fixer runs as a PHAR from vendor-phar/php-cs-fixer.phar (not as a Composer dependency).
PHP CS Fixer is configured through the php_cs.php file. The default configuration is located at configDefaults/generic/php_cs.php and can be overridden by creating your own qaConfig/php_cs.php file.
The default configuration includes:
@PhpCsFixerand@Symfonyrule sets@PHP84Migration-- PHP 8.4 migration rulesnullable_type_declaration_for_default_null_value-- Required for PHP 8.4 compatibility (implicit nullable parameters are deprecated)nullable_type_declarationwithquestion_marksyntaxdeclare_strict_types-- Enforces strict types in all filesfinal_class-- Makes all classes final by defaultstrict_comparisonandstrict_param-- Enforces strict comparisonsstatic_lambda-- Converts closures to static where possiblenative_function_invocation-- Optimises native function calls
To use a custom PHP CS Fixer configuration:
cd /project/root
mkdir -p qaConfig
# Copy and customize the default config
cp vendor/lts/php-qa-ci/configDefaults/generic/php_cs.php qaConfig/php_cs.php
# Edit the file to your needsYou can also override the file finder by copying php_cs_finder.php:
cp vendor/lts/php-qa-ci/configDefaults/generic/php_cs_finder.php qaConfig/php_cs_finder.phpTo run PHP CS Fixer manually outside of the QA pipeline:
# Via the QA pipeline (recommended -- uses correct config)
vendor/bin/qa -t fixer
# Or directly via the PHAR
php vendor/lts/php-qa-ci/vendor-phar/php-cs-fixer.phar fix --config=qaConfig/php_cs.php --dry-run --diffSee the PHP CS Fixer docs for more information about configuration options.
Rector is installed in an isolated sub-composer project at tools/rector/ to prevent dependency conflicts with PHPStan.
The pipeline runs three Rector configurations in order:
- rector-safe.php -- Converts standard PHP functions to
thecodingmachine/safeequivalents - rector-phpunit.php -- Applies PHPUnit modernisation rules to the test directory
- rector-php84.php -- Applies PHP 8.4 migration rules (skipped if a project-specific
rector.phpexists)
If you have a rector.php in your project root or qaConfig/rector.php, the pipeline will run it instead of the default PHP 8.4 rector. The safe functions and PHPUnit rectors still run.
# Create project-specific rector config
cp vendor/lts/php-qa-ci/configDefaults/generic/rector-php84.php qaConfig/rector.php
# Edit qaConfig/rector.php to your needs# Via the QA pipeline
vendor/bin/qa -t rector