在 Symfony Finder/PHP-CS-Fixer 中获取非 PHP 文件扩展名 [英] Getting Non PHP File Extensions in Symfony Finder / PHP-CS-Fixer

查看:20
本文介绍了在 Symfony Finder/PHP-CS-Fixer 中获取非 PHP 文件扩展名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为我的查找器添加非 php 扩展而苦苦挣扎.做了一个很长的谷歌搜索,但结果是空白的.找到这个但不太明白:如何在php-cs-fixer中使用其他文件扩展名,例如.ctp?

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?

这就是我所拥有的:

<?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);

我希望它处理 *.inc 和 *.class 文件,但它似乎只选择 *.php 文件.

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?

附注

我忘了补充一点,尝试单个 ->name('/(\.php|\.inc|\.class)$/'); 没有任何区别.它仍然只选择 *.php 文件.

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.

推荐答案

您的问题描述不完整,无法重现.

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 文件已修复

在项目 gitter 页面上得到一些大力支持后:https://gitter.im/PHP-CS-Fixer/Lobby,事实证明问题是我在命令行上调用事物的方式覆盖了我的配置文件中的路径信息.

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.

一个线索是从配置文件读取路径已被作为命令参数提供的路径覆盖的 CLI 消息.

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

我原来的命令是...

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

应该使用的两个选项:

  1. 跳过/path/to/project/folder正确的命令 = php php-cs-fixer fix --config/path/to/config/file/.php_cs.dist.根据开发人员的说法,这可能有一个缺点,即可能无法使用根项目的子路径运行该工具.

  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.

在 CLI 语句中添加一个 -path-mode=intersection 标志,以使事情彼此协调.正确命令 = php php-cs-fixer fix/path/to/project/folder --config/path/to/config/file/.php_cs.dist --path-mode=intersection

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

这篇关于在 Symfony Finder/PHP-CS-Fixer 中获取非 PHP 文件扩展名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆