PSR-12 - method chaining, line breaks, and indentation

1.1k views Asked by At

What is the correct indentation for the following code examples according to PSR-12?

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

and

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

As far as I can tell, they're correct... but it looks so wrong, like method2 is a child of method1. From what I've read, the closing parenthesis for method1 should have the same indentation as the line its opening parenthesis is on (no indentation), and that method2 should be indented.

I sure hope I'm misreading the guidelines, but the IDE formatting tools seem to agree on this as well.

1

There are 1 answers

1
dave On

I think by spec, either one is correct, but for what it's worth, I always do it look this, and I think this is also in accordance with the spec:

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

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