adachsoft/php-code-style

Shared PHP code style (PHP-CS-Fixer) and PHPStan baseline for PHP 8.3+ projects

Maintainers

Package info

gitlab.com/a.adach/php-code-style

Issues

pkg:composer/adachsoft/php-code-style

Statistics

Installs: 65

Dependents: 29

Suggesters: 0

Stars: 0

v0.4.1 2026-03-06 05:58 UTC

This package is auto-updated.

Last update: 2026-03-06 05:02:55 UTC


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/ and tests/ (compatible with PHPStan 2.x, with sensible excludePaths for vendor and generated directories), including custom rules:
    • MinVariableLengthRule enforcing minimal variable name length (configurable via adachsoftMinVariableLength parameter).
    • SingleClassPerFileRule enforcing a single named class per PHP file.
    • CliBootstrapRule enforcing that CLI scripts in bin/ include bootstrap.php.
  • Rector 2.x base configuration:
    • PHP 8.3 set
    • code quality, type declarations, early return, coding style, naming, privatization
    • FinalizeTestCaseClassRector for 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.neon and rector.php in your project, importing this package's defaults.
    • vendor/bin/install-cli-bootstrap
      Installs a reusable bin/bootstrap.php in your project for robust CLI autoloading.

Requirements

  • PHP 8.3+
  • Your project should have dev tools installed: friendsofphp/php-cs-fixer, phpstan/phpstan and rector/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.php loads the configuration from vendor/adachsoft/php-code-style and points the Finder to your project directories.

2) phpstan.neon

This package ships a phpstan.neon.dist with:

  • level 10,
  • default paths src and tests,
  • ready-to-use excludePaths and bootstrapFiles,
  • registration of rules MinVariableLengthRule, SingleClassPerFileRule and CliBootstrapRule.

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.neon must include vendor/adachsoft/php-code-style/phpstan.neon.dist (manually or via extension-installer) and then add your own paths/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.php from 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.php imports configuration from vendor/adachsoft/php-code-style/rector.dist.php and 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:

  1. Add this package as a dev dependency:
composer require --dev adachsoft/php-code-style
  1. 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.php to ./bin/bootstrap.php,
  • not overwrite an existing bin/bootstrap.php (if you already have one).
  1. 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.php is responsible for locating and loading vendor/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) and rector(.php|.dist.php) files (unless you use --no-backup),
  • write .php-cs-fixer.php into your project which imports this package's rules and sets the Finder to your paths (by default: src, tests),
  • write phpstan.neon that includes phpstan.neon.dist from this package and sets paths,
  • write rector.php that imports rector.dist.php from this package and sets paths,
  • handle --restore to 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 the DIR directory.

Examples:

  • Default configuration (with backup):

    vendor/bin/code-style-setup
    
  • Configuration for custom paths:

    vendor/bin/code-style-setup --paths="src,modules,tests"
    
  • Dry run (preview only):

    vendor/bin/code-style-setup --dry-run
    
  • Restore from the latest backup:

    vendor/bin/code-style-setup --restore
    
  • Restore 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 (or vendor/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.php
    • phpstan.neon.dist
    • rector.dist.php
  • In your projects you should always:
    • import .php-cs-fixer.dist.php from vendor and set your own Finder,
    • import phpstan.neon.dist (directly or via extension-installer) and set paths/parameters,
    • import rector.dist.php and set paths.

Versioning

  • There is no version field in composer.json. Versioning is managed exclusively via Git tags.

License

  • MIT (see LICENSE)