6

I am trying to run PHP CS Fixer, which I believe is based on Symfony (which I am not familiar with), and having a problem with excluding some paths.

My setup is below:

$finder = PhpCsFixer\Finder::create()
    ->in(__DIR__)
    ->exclude('lib/adodb')
    ->exclude('lib/bbcode')
    ->exclude('lib/joomla')
    ->exclude('lib/JSON')
    ->exclude('lib/pear')
    ->exclude('lib/phpass')
    ->exclude('lib/smarty')
    ->exclude('lib/smtp')
    ->exclude('modules/*/lib')  
    ->name('*.class')
    ->name('*.inc')
    ->name('*.php')
;

Basically, I will like to exclude:

modules/ANYNAME/lib/ANYFILE
modules/ANYNAME/lib/ANYSUBDIR/ANYFILE

But I find that the ->exclude('modules/*/lib') line is not catching these. For instance, modules/somemodule/lib/somefile.inc is still processed.

I had thought that this was because I had ->name('*.inc') but it seems to happen with or without that line.

The other excludes work fine except the ->exclude('modules/*/lib') one.

Any pointers?

** Correction/Update **

It does seem that the issue is with the name selector. Seems it is not allowed to select *.inc using name for instance and then try to exclude those found in modules/xyz/lib.

Overcoming this would solve my issue

Dayo
  • 12,413
  • 5
  • 52
  • 67
  • 1
    There is `notPath()` method http://api.symfony.com/3.2/Symfony/Component/Finder/Finder.html#method_notPath Would it work ? – Alexey Chuhrov May 05 '17 at 07:31
  • Thanks. Doing some tests, it does seem that the issue is with the `name` selector. Seems it is not allowed to select `*.inc` using `name` for instance and then try to exclude those found in `modules/xyz/lib` – Dayo May 05 '17 at 08:02
  • @Dayo I'm little lost so is my understanding right? You want to exclude everything under `modules`? – BentCoder May 05 '17 at 08:36
  • @BentCoder. I have several folders under modules and some of them have a lib folder. I want to exclude anything under such lib folders. – Dayo May 05 '17 at 16:28

1 Answers1

4

PHP CS Fixer could accept any iterable as finder. Indeed, default one is just a symfony/finder (https://github.com/symfony/finder/blob/master/Finder.php).

As you can see, exclude is not accepting a glob. You could use, eg, notPath:

$finder = PhpCsFixer\Finder::create()
    ->in(__DIR__)
    ->notPath('#modules/.*/lib#')
    ->name('*.inc');

Let say you have following structure: $ ls -lR .: total 8 drwxr-xr-x 2 keradus keradus 4096 Mai 5 20:32 a drwxr-xr-x 3 keradus keradus 4096 Mai 5 20:31 modules

./a:
total 4
-rw-r--r-- 1 keradus keradus 24 Mai  5 20:35 a.inc

./modules:
total 4
drwxr-xr-x 3 keradus keradus 4096 Mai  5 20:31 ANYNAME

./modules/ANYNAME:
total 4
drwxr-xr-x 3 keradus keradus 4096 Mai  5 20:31 lib

./modules/ANYNAME/lib:
total 8
-rw-r--r-- 1 keradus keradus   24 Mai  5 20:35 b.inc
drwxr-xr-x 2 keradus keradus 4096 Mai  5 20:32 sub

./modules/ANYNAME/lib/sub:
total 4
-rw-r--r-- 1 keradus keradus 24 Mai  5 20:35 c.inc

Even when all of that 3 files violates coding standards, only one (not excluded by finder) would be fixed:

$ php-cs-fixer fix --dry-run -vvv
Loaded config default from "/home/keradus/tmp/.php_cs.dist".
F
Legend: ?-unknown, I-invalid file syntax, file ignored, S-Skipped, .-no changes, F-fixed, E-error
   1) a/a.inc (braces)
keradus
  • 534
  • 2
  • 5
  • Accepting as tests against this minimal setup works as explained. Need to figure out why I still have issues with my actual setup. – Dayo May 05 '17 at 21:20
  • If you need to exclude ONLY the directory in the root, then between the characters `#` you need to use a **regular expression**, for example: `notPath('#^lib#')`. Or if you need to exclude only the most nested directories, then: `notPath('#lib$#')` – Maxim Mandrik Mar 28 '19 at 20:52