Skip to content

nelson6e65/php-code-sniffer-helpers

Repository files navigation

PHP Code Sniffer Helpers

Commitizen friendly semantic-release Packagist PHP Version Support

GitHub release Latest Version Latest unstable Version

License time tracker

Helpers for PHP Code Sniffer.

Installation

composer require --dev nelson6e65/code-sniffer-helpers

Features

Composer scripts

phpcbf for lint-staged

A wrapper to fix your staged code (or argumented files/folders) using the PHP Code Sniffer auto-fixer.

There is a bug that does not allows you to use it directly as autofixer (squizlabs/PHP_CodeSniffer#1818). There is a workarround for using it as a composer script, but does not works for using it in a lint-staged pre-commit hook.

This helper is designed to be run with lint-staged, but you can also use it directly in your composer script.

Setup with lint-staged

Add the script to your composer.json:

{
  "scripts": {
    "cs:fix-filtered": ["NelsonMartell\\PhpCodeSniffer\\ComposerScripts::phpcbf"]
  }
}

I used "cs:fix-filtered" name, but you can use any script name you like.

Configure your Husky + lint-staged in your package.json

{
  "husky": {
    "hooks": {
      "pre-commit": "lint-staged"
    }
  },
  "lint-staged": {
    "*.php": "composer cs:fix-filtered"
  }
}

Example for Husky 4. Adapt it if you use Husky 5.

Usage

You can also run it directly with composer by using composer cs:fix-filtered {PATHS}. Example:

composer cs:fix-filtered src/ tests/ config/my-config-file.php

Note: Non exixtent files/directories are ignored.

Output

The output is inspired on pretty-quick output:

composer cs:fix-filtered config/ src/Example.php src/non-existent-file.php

output1

License

License

Copyright (c) 2021 Nelson Martell

Read the LICENSE file for details.