-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Add support for multiline indenting to ObjectOperatorIndentSniff #2224
Add support for multiline indenting to ObjectOperatorIndentSniff #2224
Conversation
e3b2007
to
8f843c3
Compare
$foundIndent, | ||
]; | ||
|
||
$fix = $phpcsFile->addFixableError($error, $next, 'Incorrect', $data); | ||
if ($fix === true) { | ||
$spaces = str_repeat(' ', $requiredIndent); | ||
$spaces = str_repeat(' ', $expectedIndent); | ||
if ($foundIndent === 0) { | ||
$phpcsFile->fixer->addContentBefore($next, $spaces); | ||
} else { | ||
$phpcsFile->fixer->replaceToken(($next - 1), $spaces); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just wondering: should $previousIndent
be (re)set in this leaf of the condition as well ? Maybe to $expectedIndent
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is one of the possible heuristics.
Notice that it affects error reporting only:
// Keeping the last valid
$builder
->start()
->do() // Excpected 8, found 12
->end(); // Expected 8, found 16
// Keeping the last adjusted
$builder
->start()
->do() // Excpected 8, found 12
->end(); // Expected 12, found 16
Let's go with your suggestion as it may lead to more correct results most of the time, assuming that if there are extraneous spaces, it is more likely that there should be indentation than nothing.
…nsider the previous chained call
@jrfnl I've adjusted the logic based on your suggestion. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I haven't tested the PR, but based on the code itself, this looks to be a good change.
@jrfnl any chances of merging it anytime soon, once the PR includes tests? |
@marcospassos That's not up to me. Depends on when @gsherwood has time to have a look at this as well.
What do you mean by this ? AFAICS, the PR does include some unit tests. Other than that, you may want to reset the custom property to the default after the tests covering the property, to prevent confusion if more unit tests are added later, but that's minor. |
Sorry, I didn't realize you don't have such privilege.
Nice catch. |
Done! |
@gsherwood, did you have time to take a look into this? |
If I had to document this (which I will need to), would I say that the multilevel option allows for a line in a chained call to either be indented 1 level more or less than the previous line, with the only exception being that lines must be indented at least 1 level from the first line? So this valid: <?php
$rootNode
->one()
->two()
->three()
->four()
->five(); This is not: <?php
$rootNode
->one()
->two()
->three()
->four()
->five(); This is not: <?php
$rootNode
->one()
->two()
->three()
->four()
->five(); But is this valid, or does the chain need to close properly? <?php
$rootNode
->one()
->two()
->three()
->four()
->five(); |
@gsherwood thanks for reviewing it.
Exactly, this precisely describes the current implementation. I believe the current implementation covers the most common use cases. The widespread use case is balanced method calls, such as $writer
->writeln("First line")
->indent()
->writeln("Indent level 1")
->indent()
->writeln("Indent level 2") As a side note, I've been using it for two months now in different projects with no issues so far. |
@gsherwood are there any issues still pending in this PR? |
No, sorry. Just haven't had time to look at this yet. |
Sorry this took so long to merge, but it is in now and will be released in 3.5.0. Thanks a lot for this PR. |
This PR adds supports for multilevel indenting to the amazing sniff
ObjectOperatorIndentSniff
created by @gsherwood.Multilevel indenting contributes to improve code readability in long chained method calls. This convention is widely used by Symfony in configuration definitions:
The option
multilevel
allows enabling/disabled the new behavior, which is disabled by default to be backward compatible with previous versions.The sniff can automatically fix violations by adjusting the number of spaces based on the value of property
indent
and the previous indentation:Tests are included to confirm the correctness.