简体   繁体   English

使用 phpcs 或 php-cs-fixer 将 function 多行 arguments 固定到某个结构

[英]Fixing function multiline arguments to a certain structure using phpcs or php-cs-fixer

I'm working on a project which developed many developers for many years, and as it often happens there is no coding standard.我正在从事一个多年来开发了许多开发人员的项目,并且经常发生没有编码标准。

There are many code blocks which in general is similar but written in a different way.有许多代码块通常相似但以不同的方式编写。 For example:例如:

return $this->render('LibraryItem:list.html.twig',
    [
        'library_items' => $libraryItems,
    ]
);

or或者

return $this->render(
    'LibraryItem:list.html.twig',
    [
        'library_items' => $libraryItems,
    ]
);

or或者

return $this->render('LibraryItem:list.html.twig', array(
        'library_items' => $libraryItems,
    )
);

Now, I'm upgrading the project and introducing PSR-12 Coding Style.现在,我正在升级项目并引入PSR-12编码风格。 For this case, I'm playing with two packages PHPCodeSniffer and PHP-CS-Fixer .对于这种情况,我正在使用两个包PHPCodeSnifferPHP-CS-Fixer

I want to achieve that one of those packages forcing the code blocks described above to the next format:我想实现将上述代码块强制转换为下一种格式的那些包之一:

return $this->render('LibraryItem:list.html.twig', [
    'library_items' => $libraryItems,
]);

This is the short, simple and readable format, which is well described in The PSR-2 Meta Document .这是简短、简单且易读的格式,在PSR-2 元文档中有很好的描述。

Using one or more multi-line arguments (ie: arrays or anonymous functions) does not constitute splitting the argument list itself, therefore Section 4.6 is not automatically enforced.使用一个或多个多行 arguments(即: arrays或匿名函数)并不构成拆分参数列表本身,因此不会自动执行第 4.6 节。 Arrays and anonymous functions are able to span multiple lines. Arrays 和匿名函数能够跨越多行。

The problem is I cannot find appropriate rules that allow me to fix all such blocks automatically with PHPCodeSniffer or PHP-CS-Fixer.问题是我找不到合适的规则来允许我使用 PHPCodeSniffer 或 PHP-CS-Fixer 自动修复所有此类块。

PHP CS Fixer has a fixer method_argument_space with option ensure_single_line for on_multiline . PHP CS Fixer 有一个method_argument_space带有选项ensure_single_line for on_multiline

The config looks like this:配置如下所示:

        'method_argument_space' => [
            'on_multiline' => 'ensure_single_line'
        ],

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM