gtjamesa/php-standards

James' shared PHP style rules for PHP-CS-Fixer

v2.0.1 2021-09-17 09:51 UTC

This package is auto-updated.

Last update: 2024-10-17 17:09:45 UTC


README

This package defines my FriendsOfPHP/PHP-CS-Fixer coding standards to be used across multiple projects. For details regarding the rules used, you may use php-cs-fixer-configurator.

Usage

Install the package by requiring php-cs-fixer and php-standards:

composer require --dev friendsofphp/php-cs-fixer gtjamesa/php-standards

Next, create a .php-cs-fixer.dist.php file in the root of your project with the following content:

<?php

use PhpCsFixer\Finder;

$project_path = getcwd();
$finder = Finder::create()
    ->notPath('bootstrap/*')
    ->notPath('storage/*')
    ->notPath('resources/view/mail/*')
    ->in([
        $project_path . '/app',
        $project_path . '/config',
        $project_path . '/database',
        $project_path . '/resources',
        $project_path . '/routes',
        $project_path . '/tests',
    ])
    ->name('*.php')
    ->notName('*.blade.php')
    ->ignoreDotFiles(true)
    ->ignoreVCS(true);

return JamesAusten\styles($finder);

The file above assumes a Laravel project, but the configuration can be edited for any PHP 7.0+ project.

Add the following entries to your .gitignore file:

.php_cs.cache
.php-cs-fixer.cache

Configuration

The php-cs-fixer rules can be found in ./src/rules.php. These were taken from laravel-shift/.php_cs.laravel.php and modified to suit my coding style. The following configuration parameters can be specified to the styles function. The function returns a \PhpCsFixer\Config instance, so you may also chain additional configuration parameters after the function call.

// Define rules to override the defaults
$rules = [
    'method_argument_space' => true,
    'phpdoc_summary' => false,
];

// Specify whether to enable caching (default: `true`)
// https://github.com/FriendsOfPHP/PHP-CS-Fixer#caching
$useCache = false;

return JamesAusten\styles($finder, $rules, $useCache)
    ->setCacheFile(__DIR__ . '.php_cs.cache'); // Set the cache file

Run the fixer

To run the fixer, run the following command:

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