realodix/relax

Shared php-cs-fixer rules & finders preset

v1.7.0 2024-03-17 10:51 UTC

This package is auto-updated.

Last update: 2024-04-17 11:11:07 UTC


README

PHPVersion Packagist Version (custom server) Build Status

Realodix Relax is built on top of PHP-CS-Fixer and makes it simple to to sharing identical PHP CS Fixer rules across all of your projects without copy-and-pasting configuration files.

Installation

You can install this package by using composer:

composer require --dev realodix/relax

Running Relax

./vendor/bin/php-cs-fixer fix

For more details, see PHP-CS-Fixer: Usage documentation.

Configuring Relax

Run this command below:

./vendor/bin/relax init

or in your PHP CS Fixer configuration file, use the following contents:

<?php

use Realodix\Relax\Config;

return Config::create('@Realodix');

Rule Sets

Rule set defines a set of rules that can be used to fix code style issues in your code.

Preset Description
@Laravel The rule set by Laravel Pint
@Realodix Inherits @Laravel with some tweaks
@Spatie The rule set used by Spatie

💡 If you wish, you can also add the PHP-CS-Fixer rule sets.

Custom Fixers

💡 They're all registered, so you don't need to re-register via registerCustomFixers().

Finder Sets

By default, Relax will inspect all .php files in your project except those in the vendor directory.

Preset Description
Finder::base() The basic finder setup should be perfect for most PHP projects
Finder::laravel() Inherits Finder::base() with some specific tweaks to Laravel

💡 By default, if finder is not set Relax will use Finder::base().

Advanced Configuration

In case you only need some tweaks for specific projects, which won't deserve an own rule set - you may enable or disable specific rules.

<?php

use Realodix\Relax\Config;
use Realodix\Relax\Finder;

// You can add or override rule set
$localRules = [
    // Add rule
    'array_syntax' => true,

    // Add rule or override predefined rule
    'visibility_required' => true,

    // Override predefined rule
    'braces' => false,

    // Add custom fixers
    'CustomFixer/rule_1' => true,
    'CustomFixer/rule_2' => true,
];

$finder = Finder::laravel(__DIR__.'Foo')
    ->ignoreDotFiles(false)
    ->exclude(['Bar'])
    ->notName('*.foo.php')
    ->append(['.php-cs-fixer.dist.php']);

return Config::create('@PSR2', $localRules)
    ->setFinder($finder)
    ->setRiskyAllowed(false)
    ->registerCustomFixers(new \PhpCsFixerCustomFixers\CustomFixer());

Relax is built on top of PHP-CS-Fixer. Therefore, you may use any of its rules to fix code style issues in your project. For more details, see PHP-CS-Fixer: Config documentation and MLocati: PHP-CS-Fixer Configurator.

If you wish to completely define rules locally without using existing rule sets, you can do that:

<?php

use Realodix\Relax\Config;

$localRules = [
    '@PSR2'           => true,
    'array_syntax'    => ['syntax' => 'short'],
    'ordered_imports' => ['sort_algorithm' => 'alpha'],
];

return Config::create($localRules);

Custom Rule Set

You can easily create your own rule set by extending the AbstractRuleSet: class.

<?php

use Realodix\Relax\RuleSet\AbstractRuleSet;

final class MyRuleSet extends AbstractRuleSet
{
    /**
     * Optionally, set the rule set name.
     */
    // public string $name = 'Personal CS';

    protected function rules(): array
    {
        // ...
    }
}

And use it!

<?php

use Realodix\Relax\Config;
use Vendor\Package\MyRuleSet;

$localRules = [
    // ...
];

return Config::create(new MyRuleSet(), $localRules);

Troubleshooting

For general help and support join our GitHub Discussions.

Please report bugs to the GitHub Issue Tracker.

License

This package is licensed under the MIT License.