mklbravo/php-cs-fixer-config

Provides rule sets for friendsofphp/php-cs-fixer

0.2.0 2020-12-04 18:39 UTC

This package is auto-updated.

Last update: 2024-03-28 08:25:42 UTC


README

Inspired by ergebnis/php-cs-fixer-config, this repository provides a configuration factory and a rule set for friendsofphp/php-cs-fixer that works on PHP 7.2.

Installation

Since the repository is not in Packagist, add it to composer.json:

{
  "repositories": [
    {
      "type": "vcs",
      "url":  "git@github.com:mklbravo/php-cs-fixer-config.git"
    }
  ]
}

Run

$ composer require --dev mklbravo/php-cs-fixer-config

Usage

Configuration

Create a configuration file .php-cs-fixr/.php_cs in the root of your project:

<?php

use mklbravo\PhpCsFixer\Config;

$ruleSet = new Config\RuleSet\PHP71();
$config = Config\Factory::fromRuleSet($ruleSet);

$config->getFinder()->in(__DIR__ . '/../src');

return $config;

Configuration for libraries with header

💡 Optionally specify a header:

<?php

use mklbravo\PhpCsFixer\Config;

$year = \date('Y');

$header = <<<EOF
Copyright (c) 2020 mklbravo

@see https://github.com/mklbravo/php-cs-fixer-config.git
EOF;

$ruleSet = new Config\RuleSet\PHP71($header);
$config = Config\Factory::fromRuleSet($ruleSet);

$config->getFinder()->in(__DIR__ . '/../src');

return $config;

This will enable and configure the HeaderCommentFixer, so that file headers will be added to PHP files, for example:

<?php

/**
 * Copyright (c) 2020 mklbravo
 *
 * @see https://github.com/mklbravo/php-cs-fixer-config.git
 */

Configuration with override rules

💡 Optionally override rules from a rule set by passing in an array of rules to be merged in:

<?php

use mklbravo\PhpCsFixer\Config;


$ruleSet = new Config\RuleSet\PHP71();
$config = Config\Factory::fromRuleSet($ruleSet, [
    'mb_str_functions' => false,
    'strict_comparison' => false,
]);

$config->getFinder()->in(__DIR__ . '/../src');

return $config;

Git

Add ./php-cs-fixer/.php_cs.cache to .gitignore:

./php-cs-fixer/.php_cs.cache
vendor/

Makefile

Create a Makefile with a cs target:

.PHONY: composer cs

vendor: composer.json composer.lock
	composer validate
	composer install

cs: composer
	vendor/bin/php-cs-fixer fix --config=.php-cs-fixer/.php_cs --diff --verbose

Then run

$ make cs