How to use other file extensions in php-cs-fixer,

2019-08-27 03:47发布

问题:

How to use other file extensions in php-cs-fixer, for example cakephp template .ctp files ?

I have trying this code:

<?php
$finder = PhpCsFixer\Finder::create()
    ->notPath('bootstrap/cache')
    ->notPath('storage')
    ->notPath('vendor')
    ->in(__DIR__)
    ->name('*.php') // <===== *.ctp
    ->notName('*.blade.php')
    ->ignoreDotFiles(true)
    ->ignoreVCS(true)
;

return PhpCsFixer\Config::create()
    ->setRules(array(
        '@Symfony' => true,
        'binary_operator_spaces' => ['align_double_arrow' => false],
        'array_syntax' => ['syntax' => 'short'],
        'linebreak_after_opening_tag' => true,
        'not_operator_with_successor_space' => true,
        'ordered_imports' => true,
        'phpdoc_order' => true,
    ))
    ->setFinder($finder)
;

回答1:

Thing is, that with original way of running the PHP CS Fixer, you have path finder configured once in config file, and then you pass the path also as CLI argument. As a result - you have fully defined finder in config, which got later overwritten by CLI argument, also you got a message:

Paths from configuration file have been overridden by paths provided as command arguments.

Solution is to: - provide a path only in config file - provide a path only as CLI argument - provide a path in both places (ex, in config plugins, while as CLI argument plugins/subdir (as passing exactly same value doesn't really make sense...), but then also provide --path-mode=intersection parameter, to not override the path, but take common part of paths (one in config, one as CLI arg)



回答2:

I try with this config file:

 <?php
$finder = PhpCsFixer\Finder::create()
    ->notPath('bootstrap/cache')
    ->notPath('storage')
    ->notPath('vendor')
    ->in(__DIR__)
    ->name('*.ctp')
    ->notName('*.blade.php')
    ->ignoreDotFiles(true)
    ->ignoreVCS(true)
;

return PhpCsFixer\Config::create()
    ->setRules(array(
        '@Symfony' => true,
        'binary_operator_spaces' => ['align_double_arrow' => false],
        'array_syntax' => ['syntax' => 'short'],
        'linebreak_after_opening_tag' => true,
        'not_operator_with_successor_space' => true,
        'ordered_imports' => true,
        'phpdoc_order' => true,
    ))
    ->setFinder($finder)
;

and command

php php-cs-fixer-v2.phar fix plugins

without success.

But when I change

->in(__DIR__)

to

->in('plugins')

and run

php php-cs-fixer-v2.phar fix

fixer is executed and fix (arrays) in old cakephp 2 .ctp files for using in new cakephp 3 app.