Php-cs-fixer: using git ls-files

Created on 8 Oct 2016  路  13Comments  路  Source: FriendsOfPHP/PHP-CS-Fixer

Typical projects store their configs in Git versioning system.

I was wondering, is there snippet around to use git ls-files as $finder configuration as that is usually most correct definition of files what need to be analyzed.

So, git ls-files can give files, but it still needs to be filtered for wanted files by their extensions.

kinquestion

Most helpful comment

@SpacePossum
for this check out second command:
https://github.com/FriendsOfPHP/PHP-CS-Fixer/tree/master#using-php-cs-fixer-on-ci

All 13 comments

if i'm about to implement my own finder, what methods it should implement?

Symfony\CS\FinderInterface declares only setDir() with no documentation.

So far i've come up with something like this:

$files = explode("\n", shell_exec('git ls-files'));
$finder = Symfony\CS\Finder\DefaultFinder::create()
    ->in(__DIR__)
    // this filter would accept only files that are present in Git
    ->filter(function(\SplFileInfo $file) use (&$files) {
        $key = array_search($file->getRelativePathname(), $files);
        if ($key) {
            error_log("ACCEPT: ".$file->getRelativePathname());
        } else {
            error_log("REJECT: ".$file->getRelativePathname());
        }
        return $key;
    })
;

If that's good, and can't be made even better, add the snippet to documentation?

Pretty sure the Fabbot does something like this, maybe @keradus knows how it does that?
(btw. would like this, an example of how to this would be nice to have in the docs :) )

Not sure how fabbot is connected here.

  1. Please describe your case. Why you cannot use __DIR__ (or __DIR__.'/src') ? (which, btw, is actually filtered like here: https://github.com/FriendsOfPHP/PHP-CS-Fixer/blob/master/src/Finder.php#L27-L35)
  2. You could try putting your $files array to finder method. If it won't work directly wrap it in array iterator. Then, you need to filter your files manually.

Not sure how fabbot is connected here.

I assume fabbot is only running on the file(s) modified in commit(s) of a PR? So I thought it might be related as to use git as input for the fixer.

@SpacePossum
for this check out second command:
https://github.com/FriendsOfPHP/PHP-CS-Fixer/tree/master#using-php-cs-fixer-on-ci

@keradus

1) i do not wish to fix files that are not part of the project. this use-case i described in issue body already. i wish to lint only files in project that match *.php (altho DefaultFinder specifies .xml files, can php-cs-fixer fix xml???). duplicating rules to php-cs config leads to errors, so that too few files are processed or too much. examples can be smarty cache dir or extracted snapshots of project older versions, etc. anything junk. also, for the sake of accurate history, never point to files using branch (master), use tag or commit hash.

2) where is finder() method, please make example. also note that git ls-files is not the files that need to be processed. it needs to be filtered for ->name('*.php') as well.

also what about my question?

@SpacePossum yes, that was my intention too, ask if there's already good solution and put it to documentation, so others can find it

the #using-php-cs-fixer-on-ci is not the same, i want to have config that defines all files in git repo (+the .php filter). always. that example there was for git pushes to process only modified files.

1)
Don't keep garbage files in your project folder. If you have them structured, like cache, simple exclude it in Finder class, eg:
https://github.com/FriendsOfPHP/PHP-CS-Fixer/blob/1.12/.php_cs#L29-L33

DefaultFinder (https://github.com/FriendsOfPHP/PHP-CS-Fixer/blob/1.12/Symfony/CS/Finder/DefaultFinder.php) is deprecated, use Finder (https://github.com/FriendsOfPHP/PHP-CS-Fixer/blob/1.12/Symfony/CS/Finder.php), it will not scan .xml files. Nowadays if you will point xml files they will be ignored during fixing, matching .xml/.yml files in DefaultFinder is a legacy that we dropped in new Finder, compatible with 2.x line.

2) finder() method is part of Config class: https://github.com/FriendsOfPHP/PHP-CS-Fixer/blob/1.12/Symfony/CS/Config.php#L89. If you provide any Traversable that's incompatibile with Symfony's Finder yhen you cannot use exclude from finder.
I do really think that in your case using solution with Finder from point 1 is enough, all repos works with it...

3) just don't, it's abandoned on 2.x line.

1) you're missing the whole point saying how i should dress or or eat ice cream.

seems nothing constructive can be get from the community members, i will use example i made up

you can close this ticket.

> How could I do it better ?
< Like this or that
> Dont tell me how to do things!

...

If you read the ticket i created, i wanted to define files from git + filter needed extensions. and you are suggesting on that "don't put junk in my project tree".

And for that we provided two ways, either have it nicely structure and then finder is fine, while you could exclude extra files (like mentioned cache), or just pass a traversable to finder method.
That's it ;)
Using Finder the way you provided is actually providing the files sets twice, and then combine them on the fly. It's extra work so we said it could be avoided. Thats all, don't feel bad with it

I feel this question is answered. The SF finder provided in the configuration has enough flexibility to use in complex situations. Having examples how to handles such situations in the docs would be nice, but these must be common enough. I don't think code changes are needed here. Please feel free to comment after closing if I really missed something.

Was this page helpful?
0 / 5 - 0 ratings