# Coding standard

> Centralized ECS and Rector configuration for PHP projects

Package entry: `php-forge/coding-standard`. Requires PHP `>=8.1`. Checked 2026-11-12. [View the entry in the package directory](</docs/packages/php-forge/coding-standard/index.md>)

**Centralized ECS and Rector configuration for PHP projects**

_Share one set of rules across multiple repositories via Composer._

## System requirements

- [`PHP`](https://www.php.net/downloads) 8.1 or higher.
- [`Composer`](https://getcomposer.org/download/) for dependency management.

## Installation

```bash
composer require php-forge/coding-standard:^0.3 --dev
```

Or add the dependency manually to `composer.json`:

```json
{
  "require-dev": {
    "php-forge/coding-standard": "^0.3"
  }
}
```

Then run `composer update`.

## Configuration files

This package ships shared ECS and Rector configurations under `vendor/php-forge/coding-standard/src/`:

| File              | Purpose                                            |
| ----------------- | -------------------------------------------------- |
| src/ecs.php       | Shared ECS base rules, no PHP migration set        |
| src/ecs-81.php    | Base + `@PHP81Migration`                           |
| src/ecs-82.php    | Base + `@PHP82Migration`                           |
| src/ecs-83.php    | Base + `@PHP82Migration` (no additional 8.3 rules) |
| src/ecs-84.php    | Base + `@PHP84Migration`                           |
| src/rector.php    | Shared Rector base rules, no PHP level set         |
| src/rector-81.php | Base + `LevelSetList::UP_TO_PHP_81`                |
| src/rector-82.php | Base + `LevelSetList::UP_TO_PHP_82`                |
| src/rector-83.php | Base + `LevelSetList::UP_TO_PHP_83`                |
| src/rector-84.php | Base + `LevelSetList::UP_TO_PHP_84`                |

Pick the version that matches the **minimum** PHP your project supports; Rector applies migrations up to that level and
ECS applies the corresponding non-risky migration rules. These tools do not prove minimum-version compatibility. The plain `ecs.php` / `rector.php` apply no PHP-version migrations.

### ECS wrapper (ecs.php)

Create `ecs.php` in your repository root, requiring the version that matches the minimum PHP your project supports
(`ecs-83.php` for PHP 8.3, etc.):

```php
<?php

declare(strict_types=1);

/** @var \Symplify\EasyCodingStandard\Configuration\ECSConfigBuilder $ecsConfigBuilder */
$ecsConfigBuilder = require __DIR__ . '/vendor/php-forge/coding-standard/src/ecs-83.php';

return $ecsConfigBuilder
    ->withPaths(
        [
            __DIR__ . '/src',
            __DIR__ . '/tests',
        ],
    )
    ->withSkip(
        [
            // project-specific skips here.
        ],
    );
```

### Rector wrapper (rector.php)

Create `rector.php` in your repository root, importing the version that matches your minimum PHP target:

```php
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;

return static function (RectorConfig $rectorConfig): void {
    $rectorConfig->import(__DIR__ . '/vendor/php-forge/coding-standard/src/rector-83.php');

    $rectorConfig->paths(
        [
            __DIR__ . '/src',
            __DIR__ . '/tests',
        ],
    );

    // project-specific overrides can be added after the import.
    // $rectorConfig->skip([...]);
};
```

### Yii2-specific rules

For framework-specific rules, keep them in a separate config file (or a separate package) and import it after the base
configuration. Do not mix Yii2 rules into the generic base:

```php
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;

return static function (RectorConfig $rectorConfig): void {
    $rectorConfig->import(__DIR__ . '/vendor/php-forge/coding-standard/src/rector.php');
    $rectorConfig->import(__DIR__ . '/rector-yii2.php');

    $rectorConfig->paths(
        [
            __DIR__ . '/src',
            __DIR__ . '/tests',
        ],
    );
};
```

## Scaffolded distribution

This package is a [`yii2-extensions/scaffold`](https://github.com/yii2-extensions/scaffold) provider for the
**root `ecs.php` and `rector.php` wrapper templates** (sourced from `src/config/` via `scaffold.json`). Consumers can
opt in by allowing the plugin and listing this package as an authorised provider:

```bash
composer require yii2-extensions/scaffold:^0.1 --dev
```

```json
{
  "config": {
    "allow-plugins": {
      "yii2-extensions/scaffold": true
    }
  },
  "extra": {
    "scaffold": {
      "auto": false,
      "allowed-packages": ["php-forge/coding-standard"]
    }
  }
}
```

With `auto: false`, the plugin does not run on `composer install`; sync the wrappers manually:

```bash
vendor/bin/scaffold reapply --provider=php-forge/coding-standard
```

Both wrappers ship in mode `preserve`; written once on first install, never overwritten so consumer edits to paths or
PHP target version survive subsequent runs.

## Related packages

For dev environment scaffolding, see [`php-forge/baseline`](https://github.com/php-forge/baseline). The two packages are
independent; adopt either, both, or neither.

## Composer scripts

Follow the same convention used across PHP Forge repositories:

```json
{
  "scripts": {
    "ecs": "./vendor/bin/ecs --fix",
    "rector": "./vendor/bin/rector process"
  }
}
```

## Documentation

- ⚙️ [Configuration files](#configuration-files)
- 🧪 [Composer scripts](#composer-scripts)

## AI assistance

AI agents: read the [documentation index](https://yii2-framework.github.io/docs/llms.txt) and fetch the Markdown version
of a page by appending `index.md` to its URL. The
[AI agents guide](https://github.com/yii2-framework/docs/blob/main/src/content/docs/project/ai-agents.md) covers skill
installation, client setup and verification.

- 🤖 [`php-forge-coding-standard` skill](https://github.com/php-forge/coding-standard/blob/main/docs/agents.md):
  ECS and Rector configuration.

See the [repository guides](#documentation), the [skill source](https://github.com/php-forge/coding-standard/blob/main/skills/php-forge-coding-standard/SKILL.md), the source
configuration and the [changelog](https://github.com/php-forge/coding-standard/blob/main/CHANGELOG.md).

## Our social networks

[Follow on X](https://x.com/Terabytesoftw)
[Follow on Facebook](https://www.facebook.com/wilmer.arambula.9)

## License

[License](https://github.com/php-forge/coding-standard/blob/main/LICENSE)

## Package

- Source code and issues: [php-forge/coding-standard](https://github.com/php-forge/coding-standard). Star the repository if the package is useful to you.
- Composer package: [php-forge/coding-standard](https://packagist.org/packages/php-forge/coding-standard).
- Changelog: [CHANGELOG.md](https://github.com/php-forge/coding-standard/blob/main/CHANGELOG.md).