Skip to content

Commit

Permalink
feature PHP-CS-Fixer#262 Allow to specify path to .php_cs file using …
Browse files Browse the repository at this point in the history
…--config-file option (localheinz)

This PR was squashed before being merged into the 0.4.x-dev branch (closes PHP-CS-Fixer#262).

Discussion
----------

Allow to specify path to .php_cs file using --config-file option

This PR adds the possibility to specify the path to  the `.php_cs` file using a newly added `--config-file` option.

This is very useful if you have a Git `pre-commit` hook similar to the one suggested by [ZF2](https://github.com/zendframework/zf2/blob/master/README-GIT.md#pre-commit-hook-optional), but want to keep the configuration separate from the hook:

```php
#!/usr/bin/env php
<?php

$changes = [];

exec(
    'git diff --cached --name-status --diff-filter=ACM',
    $changes
);

$exit = 0;

array_walk($changes, function ($change) use (&$exit) {

    if ('D' === substr($change, 0, 1)) {
        return;
    }

    $name = trim(substr(
        $change,
        1
    ));

    $extension = pathinfo(
        $name,
        PATHINFO_EXTENSION
    );

    if (!preg_match('/^ph(p|tml)$/', $extension)) {
        return;
    }

    $output = [];
    $return = 0;

    exec(
        sprintf(
            'php -l %s',
            escapeshellarg($name)
        ),
        $output,
        $return
    );

    if (0 != $return) {

        echo sprintf(
            'Failed parsing: %s: ' . PHP_EOL,
            $name
        );

        echo implode(PHP_EOL, $output) . PHP_EOL;

        $exit = 1;
        return;
    }

    $output = [];
    $return = 0;

    exec(
        sprintf(
            'vendor/bin/php-cs-fixer fix --verbose --config-file=.php_cs %s',
            escapeshellarg($name)
        ),
        $output,
        $return
    );

    if (0 != $return) {

        echo sprintf(
            'Fixed coding style issues in : %s' . PHP_EOL,
            $name
        );

        echo implode(PHP_EOL, $output) . PHP_EOL;

        $exit = 1;
        return;
    }
});

exit($exit);
```

Fixes PHP-CS-Fixer#251.

Commits
-------

e4ceb86 Allow to specify path to .php_cs file using --config-file option
  • Loading branch information
fabpot committed Mar 13, 2014
2 parents fc0d890 + e4ceb86 commit 99051c3
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
composer.lock
vendor
phpunit.xml

7 changes: 7 additions & 0 deletions Symfony/CS/Console/Command/FixCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ protected function configure()
->setDefinition(array(
new InputArgument('path', InputArgument::REQUIRED, 'The path'),
new InputOption('config', '', InputOption::VALUE_REQUIRED, 'The configuration name', null),
new InputOption('config-file', '', InputOption::VALUE_OPTIONAL, 'The path to a .php_cs file ', null),
new InputOption('dry-run', '', InputOption::VALUE_NONE, 'Only shows which files would have been modified'),
new InputOption('level', '', InputOption::VALUE_REQUIRED, 'The level of fixes (can be psr0, psr1, psr2, or all)', null),
new InputOption('fixers', '', InputOption::VALUE_REQUIRED, 'A list of fixers to run'),
Expand Down Expand Up @@ -148,6 +149,9 @@ protected function configure()
->fixers(array('-Psr0Fixer'))
->finder(\$finder)
;
With the <comment>--config-file</comment> option you can specify the path to the
<comment>.php_cs</comment> file.
EOF
);
}
Expand Down Expand Up @@ -187,6 +191,9 @@ protected function execute(InputInterface $input, OutputInterface $output)
if (null === $config) {
throw new \InvalidArgumentException(sprintf('The configuration "%s" is not defined', $input->getOption('config')));
}
} elseif ($input->getOption('config-file')) {
$file = $input->getOption('config-file');
$config = include $file;
} elseif (file_exists($file = $path.'/.php_cs')) {
$config = include $file;
$addSuppliedPathFromCli = false;
Expand Down

0 comments on commit 99051c3

Please sign in to comment.