Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Idea: Check multi-line method call syntax #351

Closed
alcaeus opened this issue May 23, 2018 · 5 comments
Closed

Idea: Check multi-line method call syntax #351

alcaeus opened this issue May 23, 2018 · 5 comments

Comments

@alcaeus
Copy link

alcaeus commented May 23, 2018

This is relevant for code building on fluent interfaces, e.g. when using the query builders provided with Doctrine ORM or MongoDB ODM. The idea is to force the following syntax for multi-line function calls:

// Single-line syntax stays allowed
$this->getChild()->doSomething();

// Multi-line syntax
$this
    ->doSomething()
    ->beginBlock()
        ->doSomethingElse()
        ->beginBlock()
            ->doYetSomethingElse(
                $firstArg,
                $secondArg,
                [
                    'foo' => 'bar',
                    'baz' => 'foo',
                ]
            )
    ->doLastThing()
;

I was able to come up with the following rules

  • Similar to multi-line function declarations, once there's a newline in a series of method calls, there MUST NOT be more than one call per line
  • The first call MUST be on a new line, indented one step further than the previous line
  • Each line MUST NOT increase indentation level more than once compared to the previous line
  • Each line MAY decrease indentation level multiple times compared to the previous line, but MUST NOT be indented less than the first call
  • If a single call is split up over multiple lines, its arguments MUST be presented on their own line
  • The trailing semicolon must be on its own line indented the same as the original statement (in the example above, $this.

Is this something you would consider implementing? I could give it a shot, but don't really know where to start.

@alcaeus alcaeus changed the title Idea: Force mult-line method call syntax Idea: Check mult-line method call syntax May 23, 2018
@alcaeus alcaeus changed the title Idea: Check mult-line method call syntax Idea: Check multi-line method call syntax May 23, 2018
@kukulich
Copy link
Contributor

I'll think about it. It's not easy but it's possible :)

Maybe there could also be an optional settings for method list that should always increase indentation level.

@alcaeus
Copy link
Author

alcaeus commented May 23, 2018

Maybe there could also be an optional settings for method list that should always increase indentation level.

Sniff-level option is difficult, since builders may require moving back a few indentation levels. Take MongoDB ODM's aggregation builder for example:

$builder
    ->match()
        ->field('foo')
          ->gt(5)
          ->lt(2)
    ->group()
        ->field('_id')
          ->expression('...')
    ->execute()
;

@marcospassos
Copy link

@alcaeus I pushed a PR to CodeSniffer a few days ago very similar to what you described:
squizlabs/PHP_CodeSniffer#2224

@kukulich
Copy link
Contributor

Should be already in PHPCS 3.5.0.

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 22, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants