adachsoft/php-code-style

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

Installs: 51

Dependents: 26

Suggesters: 0

Security: 0

Stars: 0

Forks: 0

pkg:composer/adachsoft/php-code-style

v0.3.1 2026-02-16 10:52 UTC

This package is not auto-updated.

Last update: 2026-02-26 07:15:26 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 AdachSoft... vendor prefix.

What this package provides

  • PHP-CS-Fixer ruleset aligned with PSR-12 plus project conventions:
    • php_unit_method_casing: camelCase
    • declare_strict_types
    • fully_qualified_strict_types: import_symbols=false
    • ordered_imports
    • trailing_comma_in_multiline for arguments, arrays, parameters
    • single_quote
    • blank_line_before_statement for return
  • PHPStan baseline config (level 10) ready to analyse src/ and tests/ (compatible with PHPStan 2.x, with sensible excludePaths for vendor and generated directories).
  • Rector 2.x base configuration:
    • PHP 8.3 set
    • code quality, type declarations, early return, coding style, naming, privatization
    • FinalizeTestCaseClassRector for PHPUnit test classes
    • sane defaults for skips (vendor, var, node_modules, build, .code-style-backups, storage, cache, bootstrap/cache, tests/_output, FlipTypeControlToUseExclusiveTypeRector), cache and parallel processing.

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

  • Create (or update) a .php-cs-fixer.php file in your project root that imports this package's rules and applies them to your project tree. Example:
    • Load the config from this package
    • Replace the Finder to point at your project (so it scans your code, not the vendor directory)
    • Optionally extend/override rules to fit your needs

2) PHPStan

  • EITHER let phpstan/extension-installer include our config automatically via composer extra:
    • This package declares extra.phpstan.includes in its composer.json
    • If your project uses phpstan/extension-installer, our phpstan.neon.dist will be auto-included
  • OR include manually in your project's phpstan.neon: includes:
    • vendor/adachsoft/php-code-style/phpstan.neon.dist parameters: paths:
      • src

        Optional: configure minimum variable length rule

        adachsoftMinVariableLength: 3

3) Rector

  • Manual usage:

    • Create a rector.php file in your project root and import this package's base config: <?php

      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',
      ]);
      
    • You can further customize the returned RectorConfigBuilder in your project (e.g. add more sets, rules, skips).
  • Automatic usage via setup script:
    • Run vendor/bin/code-style-setup in your project
    • The script will generate rector.php that imports vendor/adachsoft/php-code-style/rector.dist.php and sets paths based on --paths (default: src,tests)

Composer scripts (recommended in your project)

  • In your project's composer.json add: 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

  • This package ships with a helper script to configure your project quickly: vendor/bin/code-style-setup
  • It will:
    • backup existing .php-cs-fixer(.dist).php, phpstan(.neon|.neon.dist) and rector(.php|.dist.php)
    • write project .php-cs-fixer.php importing rules from this package and setting the Finder to your paths (default: src, tests)
    • write phpstan.neon including this package's phpstan.neon.dist and set paths
    • write rector.php importing this package's rector.dist.php and set paths
    • support --restore to revert from the latest backup

Setup script options and examples

  • Options:

    • --dry-run Show planned actions without modifying files
    • --no-backup Do not create backups before changing files
    • --paths="a,b,c" Comma-separated list of project paths to analyse (defaults: src,tests)
    • --restore[=DIR] Restore configs from the latest backup or from DIR if provided
  • Examples:

    • Default setup (with backup): vendor/bin/code-style-setup
    • Setup for custom paths: vendor/bin/code-style-setup --paths="src,modules,tests"
    • Dry run preview: vendor/bin/code-style-setup --dry-run
    • Restore from 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 coding style: composer cs:check
  • Auto-fix coding style: composer cs:fix
  • Static analysis: composer stan
  • Rector refactoring: composer rector (or vendor/bin/rector)
  • Coverage (when running PHPUnit):
    • Text summary in console
    • Clover XML: build/logs/clover.xml
    • HTML report: build/coverage/

Notes

  • This package ships with reference configs:
    • .php-cs-fixer.dist.php — the default ruleset
    • phpstan.neon.dist — the default PHPStan config (level 10) for src and tests, with sensible excludePaths (vendor/, storage/, var/, cache/, node_modules/, bootstrap/cache/, tests/_output/*) and the AdachSoft custom rule
    • rector.dist.php — the default Rector base config (PHP 8.3, quality/style/type/naming/privatization sets, FinalizeTestCaseClassRector for tests, sensible skips for vendor and build artefacts, with cache and parallel enabled)
  • When importing .php-cs-fixer.dist.php from vendor, always override the Finder to point to your project's root.
  • When importing rector.dist.php from vendor, always set paths in your project (either manually or via the setup script).

Versioning

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

License

  • MIT (see LICENSE)