Skip to content

Shareable pre-configured base style rules for the PHP Code Standards Fixer tool.

License

Notifications You must be signed in to change notification settings

weerd/php-style

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

33 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

✨ PHP Styles ✨

This package provides a simple way to apply pre-configured base style rules for the PHP Code Standards Fixer tool, that can be easily shared across projects and packages using PHP 7+.

It can be configured using the default rules or you can specify a different supported base ruleset, along with any additional rules or overrides you would like to include.

Installation

$ composer require weerd/php-style

Usage

Add a .php_cs.dist in the root directory of your project or pacakge.

Within that file create a new instance of PhpCsFixer\Finder and configure what files and directories the PHP CS Fixer tool should be looking to fix code style.

Next, call the Weerd\PhpStyle\configure() function and pass the instance of the finder as the first argument.

Below are some examples along with variations utilizing the $options array:

Example using default ruleset

The following example returns a configuration with the default rules for PHP styles.

<?php

use PhpCsFixer\Finder;
use function Weerd\PhpStyle\configure;

$finder = Finder::create()
    ->in([
        __DIR__.'/src',
        __DIR__.'/tests',
    ]);

return configure($finder);

Example using default ruleset and additional rules

The following example returns a configuration with the default rules, along with two additional rules included in the final set of rules for PHP styles.

<?php

use PhpCsFixer\Finder;
use function Weerd\PhpStyle\configure;

$finder = Finder::create()
    ->in([
        __DIR__.'/src',
        __DIR__.'/tests',
    ]);

$options = [
  'rules' => [
    'single_quote' => true,
    'trim_array_spaces' => true,
  ],
];

return configure($finder, $options);

Example using laravel ruleset

The following example returns a configuration with the laravel rules for PHP styles.

<?php

use PhpCsFixer\Finder;
use function Weerd\PhpStyle\configure;

$finder = Finder::create()
    ->in([
        __DIR__.'/app',
        __DIR__.'/config',
        __DIR__.'/database',
        __DIR__.'/routes',
        __DIR__.'/tests',
    ])
    ->notName('*.blade.php');

return configure($finder, ['base' => 'laravel']);

Once you have the .php_cs.dist file setup, you can run the PHP CS Fixer tool by running:

$ vendor/bin/php-cs-fixer fix --allow-risky=yes

Or to make things easier, add that command to the composer.json file as a script:

// composer.json

{
    "scripts": {
        "format": "vendor/bin/php-cs-fixer fix --allow-risky=yes"
    }
}

Now you can run the PHP CS Fixer tool with the following composer command:

$ composer format

Credits

🙌 Thanks to @Spatie and their Laravel Package Training course for introducing me to the PHP Code Standards Fixer tool and how to use it!

🙌 This package is heavily inspired by @timacdonald's great article on how to share PHP CS Fixer rules!

🙌 The complete set of Laravel specific rules are thanks to @laravel-shift and the compiled rules shared in a gist and used on Laravel Shift!

About

Shareable pre-configured base style rules for the PHP Code Standards Fixer tool.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages