shawnveltman/phpcsfixer-helper

Collection of my PHP CS Fixer rules to make it easy to share between projects

dev-master 2024-06-03 15:35 UTC

This package is auto-updated.

Last update: 2024-06-03 15:35:12 UTC


README

Latest Version on Packagist Total Downloads GitHub Actions

This allows our team to share the same code styling, to reduce the amount of time we need to discuss those things, as well as sorting through changes between commits that are only formating related.

Prerequisites

If you don't already have PhpCsFixer and Husky installed, then copy & paste below into terminal:

composer require friendsofphp/php-cs-fixer --dev
npm i -D husky lint-staged@^11.x.x
npm install --save-dev @shufo/prettier-plugin-blade prettier
npx husky init

Then, update the .husky/pre-commit file to only have this line:

npx lint-staged

Then, add this line to package.json (this assumes your Php CS Fixer file is called .php-cs-fixer.php, change as needed):

"lint-staged": {
    "*.php": "php ./vendor/bin/php-cs-fixer fix --config .php-cs-fixer.php",
    "*.blade.php": "node_modules/.bin/prettier --write resources/**/*.blade.php"
  },

Installation

You can install the package via composer:

composer require shawnveltman/phpcsfixer-helper --dev

Once you've installed it, create a new file called .php-cs-fixer.php in your base directory.

Update .gitignore

Add .php-cs-fixer.cache to your .gitignore file!

Usage

After installing the package, navigate to your PHP CS Fixer file (ie .php-cs-fixer.php), and replace the existing $rules array with:

use Shawnveltman\PhpcsfixerHelper\PhpcsfixerHelper;

$helper = new PhpcsfixerHelper();
$rules = $helper->get_style_rules();

Fixer File

If you don't have a fixer file, you can use the template below to your .php-cs-fixer.php file:

<?php

use PhpCsFixer\Finder;
use Shawnveltman\PhpcsfixerHelper\PhpcsfixerHelper;

$helper = new PhpcsfixerHelper();
$rules = $helper->get_style_rules();

$finder = Finder::create()
    ->notPath('bootstrap')
    ->notPath('storage')
    ->notPath('vendor')
    ->notPath('coverage-report')
    ->in(getcwd())
    ->name('*.php')
    ->notName('*.blade.php')
    ->notName('index.php')
    ->notName('server.php')
    ->ignoreDotFiles(true)
    ->ignoreVCS(true);

$config = new PhpCsFixer\Config();

return $config->setFinder($finder)
    ->setRules($rules)
    ->setRiskyAllowed(true)
    ->setUsingCache(true);
 

Testing

composer test

Common Issues / Troubleshooting

If you run the above steps and it is not automatically running on commit, there may be different npm versions at play in your team.

Step 1 is to delete your node_modules directory, and package-lock.json files, and then run npm install.

Step 2 is to ensure all team members are using the same version of npm.

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email shawn.veltman@gmail.com instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

Laravel Package Boilerplate

This package was generated using the Laravel Package Boilerplate.