sllh/php-cs-fixer-styleci-bridge

This package is abandoned and no longer maintained. No replacement package was suggested.

Auto configure PHP-CS-Fixer from StyleCI config file

v2.1.1 2016-06-22 13:26 UTC

README

⚠️ This package is not maintained anymore. ⚠️

PHP-CS-Fixer v2 came with a brand new configuration structure but StyleCI decided to keep the old way.

Because of that, it's now very hard to maintain this bridge and it's still not compatible with PHP-CS-Fixer v2.

This is also why I decided to abandon this package and write FlintCI, my own Code Review service for multiple fixers and linters without any required configuration bridge.

You can try it here (flintci.io), or keep using StyleCI without this bridge.

Auto configure PHP-CS-Fixer from StyleCI config file.

This library permits to generate php-cs-fixer configuration directly from your .styleci.yml config file.

With that, you will avoid the pain of both config files maintenance.

Latest Stable Version Latest Unstable Version License Dependency Status Reference Status

Total Downloads Monthly Downloads Daily Downloads

Build Status Scrutinizer Code Quality Code Climate Coverage Status SensioLabsInsight

Who is using this?

You can see which projects are using this package on the dedicated Packagist page.

Installation

Include this library on your dev dependencies:

composer require --dev sllh/php-cs-fixer-styleci-bridge

Usage

You can use this bridge with several manners.

Basic usage

Put the following config on your .php_cs file:

<?php

// Needed to get styleci-bridge loaded
require_once __DIR__.'/vendor/sllh/php-cs-fixer-styleci-bridge/autoload.php';

use SLLH\StyleCIBridge\ConfigBridge;

return ConfigBridge::create();

With this configuration, the configuration bridge will just parse your .styleci.yml file.

Sample working file:

preset: symfony

enabled:
  - align_double_arrow
  - newline_after_open_tag
  - ordered_use
  - long_array_syntax

disabled:
  - unalign_double_arrow
  - unalign_equals

Directories options

You can change default repository of your .styleci.yml file and directories for the CS Finder directly on ConfigBridge::create method or constructor.

<?php

require_once __DIR__.'/vendor/sllh/php-cs-fixer-styleci-bridge/autoload.php';

use SLLH\StyleCIBridge\ConfigBridge;

return ConfigBridge::create(__DIR__.'/config', [__DIR__, __DIR__.'../lib']);

Customize the configuration class

ConfigBridge::create returns a Symfony\CS\Config\Config that you can customize as you want.

<?php

require_once __DIR__.'/vendor/sllh/php-cs-fixer-styleci-bridge/autoload.php';

use SLLH\StyleCIBridge\ConfigBridge;

return ConfigBridge::create()
    ->setUsingCache(true)       // Enable the cache
;

Using the bridge

You can also using bridge method, part by part.

<?php

require_once __DIR__.'/vendor/sllh/php-cs-fixer-styleci-bridge/autoload.php';

use SLLH\StyleCIBridge\ConfigBridge;
use Symfony\CS\Config\Config;

$bridge = new ConfigBridge();

return Config::create()
    ->finder($bridge->getFinder())
    ->fixers(['dummy', 'foo', '-bar'])
    ->setUsingCache(true)
;

Manually enable or disable fixers

To enable or disable some fixers manually on the .php_cs file, you will have to use merge system to keep fixers defined by the bridge:

require_once __DIR__.'/vendor/sllh/php-cs-fixer-styleci-bridge/autoload.php';

use SLLH\StyleCIBridge\ConfigBridge;

$config = ConfigBridge::create();

return $config
    ->setUsingCache(true)
    ->fixers(array_merge($config->getFixers(), ['-psr0', 'custom', 'foo', '-bar']))
;

Header comment

Unfortunately, header comment option is not available on StyleCI config file.

You will have to copy it from StyleCI web interface and set it manually.

The config bridge will automatically detect the fixer and add it on CS configuration.

PHP-CS-Fixer 1.x

<?php

require_once __DIR__.'/vendor/sllh/php-cs-fixer-styleci-bridge/autoload.php';

use SLLH\StyleCIBridge\ConfigBridge;
use Symfony\CS\Fixer\Contrib\HeaderCommentFixer;

$header = <<<EOF
This file is part of the dummy package.

(c) John Doe <john@doe.com>

This source file is subject to the MIT license that is bundled
with this source code in the file LICENSE.
EOF;

HeaderCommentFixer::setHeader($header);

return ConfigBridge::create();

PHP-CS-Fixer 2.x

<?php

require_once __DIR__.'/vendor/sllh/php-cs-fixer-styleci-bridge/autoload.php';

use SLLH\StyleCIBridge\ConfigBridge;

$header = <<<EOF
This file is part of the dummy package.

(c) John Doe <john@doe.com>

This source file is subject to the MIT license that is bundled
with this source code in the file LICENSE.
EOF;

$config = ConfigBridge::create();

return $config
    ->setRules(array_merge($config->getRules(), array(
        'header_comment' => array('header' => $header)
    )))
;

Both versions

You can handle both versions easily with some magic method_exists tricks:

<?php

require __DIR__.'/vendor/sllh/php-cs-fixer-styleci-bridge/autoload.php';

use SLLH\StyleCIBridge\ConfigBridge;
use Symfony\CS\Fixer\Contrib\HeaderCommentFixer;

$header = <<<EOF
This file is part of the dummy package.

(c) John Doe <john@doe.com>

This source file is subject to the MIT license that is bundled
with this source code in the file LICENSE.
EOF;

// PHP-CS-Fixer 1.x
if (method_exists('Symfony\CS\Fixer\Contrib\HeaderCommentFixer', 'getHeader')) {
    HeaderCommentFixer::setHeader($header);
}

$config = ConfigBridge::create();

// PHP-CS-Fixer 2.x
if (method_exists($config, 'setRules')) {
    $config->setRules(array_merge($config->getRules(), array(
        'header_comment' => array('header' => $header)
    )));
}

return $config;

Troubleshooting

Conflict with code or vendor library

In some edge cases, the bridge code may conflict with your code or your included vendor.

This kind of issue was discovered in puli/cli#21 (comment) and fixed since v1.3.3 in #47.

Before that, you had to require the vendor autoload like this:

require __DIR__.'/vendor/autoload.php';

This is not the secured way. Make sure to require our custom loader instead:

require __DIR__.'/vendor/sllh/php-cs-fixer-styleci-bridge/autoload.php';