Getting Non PHP File Extensions in Symfony Finder

2019-09-21 16:29发布

问题:

Struggling with adding non php extensions to my finder. Done a long google search but came up blank. Found this but couldn't quite understand: How to use other file extensions in php-cs-fixer, for example .ctp?

This is what I have:

<?php
$finder = PhpCsFixer\Finder::create()
    ->notPath('path/to/some/file.inc')
    ->notPath('path/to/some/file.class')
    ->in(__DIR__)
    ->name('*.php')
    ->name('*.inc')
    ->name('*.class');

    return PhpCsFixer\Config::create()
        ->setRules(
            array(
                'Rule 1' => true,
                ...
                'Rule n' => true,
            )
        )
    ->setFinder($finder);

I will like it to work on the *.inc and *.class files but it only seems to be picking the *.php files.

Any clues to what I may have missed?

PS

I forgot to add that trying a single ->name('/(\.php|\.inc|\.class)$/'); makes no difference. It still only picks *.php files.

Also, instead of voting me down without explanation, please give me a definitive answer ... is there something I am doing wrongly? If so, point me to this.

回答1:

Your issue is not fully described, can't reproduce.

ker@dus:~/github/PHP-CS-Fixer λ cat .php_cs.dist 
<?php

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

return PhpCsFixer\Config::create()
    ->setRules([
        '@Symfony' => true,
    ])
    ->setFinder($finder)
;

ker@dus:~/github/PHP-CS-Fixer λ ls -al example/
total 16
drwxr-xr-x  2 keradus keradus 4096 cze 18 13:20 .
drwxr-xr-x 11 keradus keradus 4096 cze 18 10:20 ..
-rw-rw-r--  1 keradus keradus 1550 cze 17 12:00 FileReader.php
-rw-rw-r--  1 keradus keradus 1507 cze 18 13:20 FileRemoval.inc

ker@dus:~/github/PHP-CS-Fixer λ php php-cs-fixer fix -vv --dry-run --diff --diff-format=udiff
Loaded config default from "/home/keradus/github/PHP-CS-Fixer/.php_cs.dist".
.F
Legend: ?-unknown, I-invalid file syntax, file ignored, S-Skipped, .-no changes, F-fixed, E-error
   1) example/FileRemoval.inc (braces)
      ---------- begin diff ----------
--- Original
+++ New
@@ -29,7 +29,8 @@
      */
     private $files = [];

-    public function __construct() {
+    public function __construct()
+    {
         register_shutdown_function([$this, 'clean']);
     }


      ----------- end diff -----------


Checked all files in 0.027 seconds, 12.000 MB memory used

*.inc file was fixed

** EDIT BY OP **

After some great support on the project gitter page: https://gitter.im/PHP-CS-Fixer/Lobby, it turns out the issue was that the way I was calling things on the command line was overwriting the path information in my config file.

A clue was the CLI message that read Paths from configuration file have been overridden by paths provided as command arguments.

My original command was...

php php-cs-fixer fix /path/to/project/folder --config /path/to/config/file/.php_cs.dist

Two options that should have been used:

  1. Skip the /path/to/project/folder Correct command = php php-cs-fixer fix --config /path/to/config/file/.php_cs.dist. According to the devs, this could have a drawback in that one may not be able to run the tool with sub paths of the root project.

  2. Add a -path-mode=intersectionflag to the CLI statement to make things play nice with each other. Correct command = php php-cs-fixer fix /path/to/project/folder --config /path/to/config/file/.php_cs.dist --path-mode=intersection