PSR-12 - 方法链、换行和缩进

问题描述

根据 PSR-12,以下代码示例的正确缩进是什么?

$object->method1(function () {
    # do things
})
    ->method2(function () {
        # do things
    });

$object->method1(
    $val1,$val2,... (long enough to require multiple lines)
)
    ->method2(
        $val1,... (long enough to require multiple lines)
    );

据我所知,他们是正确的……但它看起来很错误,就像 method2method1 的孩子一样。从我读到的内容来看,method1 的右括号应该与它的左括号所在行的缩进相同(没有缩进),并且 method2 应该缩进。

我当然希望我误读了指南,但 IDE 格式化工具似乎也同意这一点。

解决方法

我认为根据规范,任何一个都是正确的,但就其价值而言,我总是这样做,而且我认为这也符合规范:

$object
    ->method1(
        $val1,$val2,)
    ->method2(
        $val1,);

https://phptools.online/php-checker/result/a200cfcd-b7b9-42cf-88fd-2949000feb58

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...