algoritma/php-coding-standards

A usefull PHP Coding Standard for your projects, based on PHPStan, PHP CS Fixer and Rector.

Installs: 237

Dependents: 3

Suggesters: 0

Security: 0

Stars: 6

Watchers: 0

Forks: 0

Open Issues: 1

Type:composer-plugin

pkg:composer/algoritma/php-coding-standards


README

Version PHP version supported
3.0 >= 8.1

Installation

To install the coding standard, run the following command:

$ composer require --dev algoritma/php-coding-standards

When you install it, a plugin will ask you some questions to setup your project automatically.

The installer will add the .php-cs-fixer.dist.php, rector.php, and phpstan.neon files in your project root directory. You can then edit them manually if you need to make any changes.

The CS config will be configured to find your project files using composer autoload sources. Only psr-0, psr-4 and classmap autoloads are supported.

The installer will also add five scripts in your composer.json;

"scripts": {
    "cs-check": "php-cs-fixer fix --dry-run --diff --allow-risky yes",
    "cs-fix": "php-cs-fixer fix --diff --allow-risky yes",
    "rector-check": "rector process --dry-run",
    "rector-fix": "rector process",
    "phpstan": "phpstan analyze"
}

Usage

To start code style check:

$ composer cs-check

To automatically fix code style:

$ composer cs-fix

To automatically check for refactor:

$ composer rector-check

To automatically refactor fix:

$ composer rector-fix

To run static analisys check:

$ composer phpstan

Configuration Generation

If you need to regenerate the default configuration files, use the following commands:

To regenerate the default .php-cs-fixer.dist.php configuration:

$ composer algoritma-cs-create-config
$ composer algoritma-cs-create-config --help

Usage:
  algoritma-cs-create-config [options]

Options:
      --no-dev                   Do not include autoload-dev directories
      --no-risky                 Do not include risky rules

To regenerate the default rector.php configuration:

$ composer algoritma-rector-create-config
$ composer algoritma-rector-create-config --help

Usage:
  algoritma-rector-create-config [options]

Options:
      --no-dev                   Do not include autoload-dev directories

To regenerate the default phpstan.neon configuration:

$ composer algoritma-phpstan-create-config
$ composer algoritma-phpstan-create-config --help

Usage:
  algoritma-phpstan-create-config [options]

Options:
      --no-dev                   Do not include autoload-dev directories

Main Dependencies

This project relies on the following tools to enforce coding standards:

Configuration

For more information on how to configure the tools, please refer to their official documentation.

Risky rules (PHP-CS-Fixer)

Risky rules may be unstable, and cause unintended behavioral changes to your code. If you want to add these rules, you can create your own .php-cs-fixer.php configuration:

<?php

/** @var \PhpCsFixer\Config $config */
$config = include __DIR__ . '/.php-cs-fixer.dist.php';

$rulesProvider = new Algoritma\CodingStandards\Rules\CompositeRulesProvider([
    new Algoritma\CodingStandards\Rules\DefaultRulesProvider(),
    new Algoritma\CodingStandards\Rules\RiskyRulesProvider(),
    new Algoritma\CodingStandards\Rules\ArrayRulesProvider([
        // additional rules or rules to override
    ]),
]);

$config->setRules($rulesProvider->getRules());

return $config;