adachsoft / php-code-style
Shared PHP code style (PHP-CS-Fixer) and PHPStan baseline for PHP 8.3+ projects
Requires
- php: ^8.3
Requires (Dev)
- adachsoft/changelog-linter: ^0.3.0
- adachsoft/collection: ^2.5.0
- friendsofphp/php-cs-fixer: ^3.68
- justinrainbow/json-schema: ^5.2
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^12
- rector/rector: ^2.3
- twig/twig: ^3.0
README
Shared PHP code style (PHP-CS-Fixer), PHPStan configuration and Rector base config for PHP 8.3+ projects.
Namespaces used in this package follow the AdachSoft\... vendor prefix.
What this package provides
- PHP-CS-Fixer ruleset aligned with PSR-12 plus project conventions (method naming, strict types, imports, trailing commas, etc.).
- PHPStan baseline config (level 10) ready to analyse
src/andtests/(compatible with PHPStan 2.x, with sensibleexcludePathsfor vendor and generated directories), including custom rules:MinVariableLengthRuleenforcing minimal variable name length (configurable viaadachsoftMinVariableLengthparameter).SingleClassPerFileRuleenforcing a single named class per PHP file.CliBootstrapRuleenforcing that CLI scripts inbin/includebootstrap.php.
- Rector 2.x base configuration:
- PHP 8.3 set
- code quality, type declarations, early return, coding style, naming, privatization
FinalizeTestCaseClassRectorfor PHPUnit test classes- sensible defaults for skips (vendor, var, node_modules, build, .code-style-backups, storage, cache, bootstrap/cache, tests/_output,
FlipTypeControlToUseExclusiveTypeRector), cache and parallel processing.
- CLI helpers:
vendor/bin/code-style-setup
Generates.php-cs-fixer.php,phpstan.neonandrector.phpin your project, importing this package's defaults.vendor/bin/install-cli-bootstrap
Installs a reusablebin/bootstrap.phpin your project for robust CLI autoloading.
Requirements
- PHP 8.3+
- Your project should have dev tools installed:
friendsofphp/php-cs-fixer,phpstan/phpstanandrector/rector(this package only provides configuration).
Install
Require this package as a dev dependency in your project:
composer require --dev adachsoft/php-code-style
If needed, also require the tools in your project:
composer require --dev friendsofphp/php-cs-fixer phpstan/phpstan rector/rector
How to use in your project
1) .php-cs-fixer.php
In your project root, create a .php-cs-fixer.php file that:
- loads the config from this package,
- replaces the Finder to point at your source tree (so it scans your code, not
vendor), - optionally extends/overrides rules.
Minimal example:
<?php
declare(strict_types=1);
use PhpCsFixer\Config;
use PhpCsFixer\Finder;
// 1. Configure which paths from YOUR project should be scanned
$finder = (new Finder())
->in([
__DIR__ . '/src',
__DIR__ . '/tests',
])
->exclude([
'vendor',
'var',
'node_modules',
'storage',
'cache',
'bootstrap/cache',
'tests/_output',
])
->ignoreDotFiles(true)
->ignoreVCS(true);
// 2. Load base config (ruleset) from this package
/** @var Config $config */
$config = require __DIR__ . '/vendor/adachsoft/php-code-style/.php-cs-fixer.dist.php';
// 3. Attach your Finder and (optionally) override rules
return $config
->setFinder($finder);
// ->setRules(array_merge($config->getRules(), [
// // your project-specific overrides here
// ]));
The exact rule set is not important in this README.
What matters is that.php-cs-fixer.phploads the configuration fromvendor/adachsoft/php-code-styleand points the Finder to your project directories.
2) phpstan.neon
This package ships a phpstan.neon.dist with:
- level 10,
- default paths
srcandtests, - ready-to-use
excludePathsandbootstrapFiles, - registration of rules
MinVariableLengthRule,SingleClassPerFileRuleandCliBootstrapRule.
In your project you should have your own phpstan.neon (or phpstan.neon.dist) which:
- includes the configuration from this library,
- sets project-specific paths (
paths), - optionally overrides parameters.
Minimal example of phpstan.neon in your project:
includes:
- vendor/adachsoft/php-code-style/phpstan.neon.dist
parameters:
paths:
- src
- tests
# Example of overriding parameter used by MinVariableLengthRule
adachsoftMinVariableLength: 3
If you are using phpstan/extension-installer, the entry in this package's composer.json:
{
"extra": {
"phpstan": {
"includes": [
"phpstan.neon.dist"
]
}
}
}
will make phpstan.neon.dist from this package automatically included.
In that case your phpstan.neon can contain only project settings:
parameters:
paths:
- src
- tests
# Optional override of parameters defined by this package
adachsoftMinVariableLength: 3
The key point: your
phpstan.neonmust includevendor/adachsoft/php-code-style/phpstan.neon.dist(manually or via extension-installer) and then add your ownpaths/parameters.
3) rector.php
This repository contains rector.dist.php with the base Rector configuration (sets, rules, skips, cache, parallel).
In your project you should have a rector.php file which:
- loads
rector.dist.phpfrom this package, - sets the paths to your code (
src,tests, ...), - optionally adds/changes configuration.
Example rector.php in a project:
<?php
declare(strict_types=1);
use Rector\Configuration\RectorConfigBuilder;
/** @var RectorConfigBuilder $rectorConfigBuilder */
$rectorConfigBuilder = require __DIR__ . '/vendor/adachsoft/php-code-style/rector.dist.php';
return $rectorConfigBuilder
->withPaths([
__DIR__ . '/src',
__DIR__ . '/tests',
]);
// ->withPaths([...other directories...])
// ->withSkip([...additional files/directories to skip...]);
The important part is that
rector.phpimports configuration fromvendor/adachsoft/php-code-style/rector.dist.phpand then adds your project directories.
CLI bootstrap for scripts in bin/
This package contains a generic bin/bootstrap.php which locates the vendor/autoload.php file regardless of whether a script is executed:
- from this package's repository,
- as a script installed in another project's
vendor/bin, - in more complex layouts (e.g. monorepo).
bin/bootstrap.php checks several common locations for autoload.php and exits with status code 1 if none of them is found.
Script: vendor/bin/code-style-setup
The vendor/bin/code-style-setup script uses bin/bootstrap.php to load Composer's autoloader in a way that is resilient to different installation scenarios. As a result, you can run it both in this package's repository and as a vendor script in another project.
Bootstrap installer for other libraries: vendor/bin/install-cli-bootstrap
If you are building your own library with CLI scripts under bin/, you can reuse this package's bootstrap:
- Add this package as a dev dependency:
composer require --dev adachsoft/php-code-style
- Run the bootstrap installer:
vendor/bin/install-cli-bootstrap
This command will:
- create the
bin/directory in your project (if it does not exist yet), - copy
vendor/adachsoft/php-code-style/bin/bootstrap.phpto./bin/bootstrap.php, - not overwrite an existing
bin/bootstrap.php(if you already have one).
- In your own CLI scripts under
bin/, use this bootstrap, for example:
#!/usr/bin/env php
<?php
declare(strict_types=1);
require __DIR__ . '/bootstrap.php';
// your CLI code here
With this setup:
bin/bootstrap.phpis responsible for locating and loadingvendor/autoload.php,- your CLI scripts will work both in the library's repository and when installed as a dependency in another project.
Composer scripts (recommended in your project)
In your project (not in this library) you can configure convenient aliases in composer.json:
{
"scripts": {
"cs:check": "php-cs-fixer fix --dry-run --diff --config=.php-cs-fixer.php",
"cs:fix": "php-cs-fixer fix --config=.php-cs-fixer.php",
"stan": "phpstan analyse",
"rector": "rector"
}
}
Setup script (automatic configuration)
This package provides a script to quickly configure your project:
vendor/bin/code-style-setup
The script will:
- back up existing
.php-cs-fixer(.dist).php,phpstan(.neon|.neon.dist)andrector(.php|.dist.php)files (unless you use--no-backup), - write
.php-cs-fixer.phpinto your project which imports this package's rules and sets the Finder to your paths (by default:src,tests), - write
phpstan.neonthat includesphpstan.neon.distfrom this package and setspaths, - write
rector.phpthat importsrector.dist.phpfrom this package and setspaths, - handle
--restoreto restore configuration from the latest backup or from a specified directory.
Setup script options and examples
Options:
--dry-run
Show planned actions without modifying any files.--no-backup
Do not create backups before making changes.--paths="a,b,c"
Comma-separated list of project directories (default:src,tests).--restore[=DIR]
Restore configuration from the latest backup or from theDIRdirectory.
Examples:
Default configuration (with backup):
vendor/bin/code-style-setupConfiguration for custom paths:
vendor/bin/code-style-setup --paths="src,modules,tests"Dry run (preview only):
vendor/bin/code-style-setup --dry-runRestore from the latest backup:
vendor/bin/code-style-setup --restoreRestore from a specific backup directory:
vendor/bin/code-style-setup --restore=.code-style-backups/20250101-120000
Run
- Check code style:
composer cs:check - Automatically fix code style:
composer cs:fix - Run static analysis:
composer stan - Run Rector refactoring:
composer rector(orvendor/bin/rector) - Test coverage (when running PHPUnit):
- Text summary in the console
- Clover XML:
build/logs/clover.xml - HTML report:
build/coverage/
Notes
- This package provides reference configurations:
.php-cs-fixer.dist.phpphpstan.neon.distrector.dist.php
- In your projects you should always:
- import
.php-cs-fixer.dist.phpfromvendorand set your own Finder, - import
phpstan.neon.dist(directly or via extension-installer) and setpaths/parameters, - import
rector.dist.phpand setpaths.
- import
Versioning
- There is no
versionfield incomposer.json. Versioning is managed exclusively via Git tags.
License
- MIT (see LICENSE)