-
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
Incorrect indent for closures as method argument. #2078
Incorrect indent for closures as method argument. #2078
Comments
This is caused by a conflict between <?php
$repository->foo()
->bar(
function () {
return true;
}
); When running the fixer over it using the PSR2 standard, you get these first few loops:
The ScopeIndent sniff notices that a code block is not starting at a tab stop (assuming a 4-space indent) so it wants that function keyword (and closing brace) to be moved to a 16-space indent instead of a 15-space indent. But then the FunctionCallSignature sniff notices that the multi-line function call is now indented 5 spaces from the I think the solution here is to let the ScopeIndent sniff win so that code lines fall at the correct tab-stops. That's what a few other sniffs do as well, and it's the only way I can think of changing things that wont break any of the standards. Plus, it will allow the closure to have other code blocks like IF statements aligned to tab-stops as well without having to make further nested exceptions for them. This change would mean that your code would look like this: <?php
protected function getCategoryFeatureRepositoryMock(
array $map,
int $getExistingIdsCalls
): CategoryFeatureRepositoryInterface {
/** @var MockObject|CategoryFeatureRepository $repository */
$repository = $this->getMockBuilder(CategoryFeatureRepository::class)
->disableOriginalConstructor()
->setMethods(['getCategoryFeatureIds'])
->getMock();
$repository->expects(self::exactly($getExistingIdsCalls))
->method('getCategoryFeatureIds')
->willReturnCallback(
function (string $providerId, int $categoryId, array $idsList) use ($map) {
self::assertEquals($this->providerId, $providerId);
self::assertEquals($this->categoryId, $categoryId);
return \array_intersect($map, $idsList);
}
);
return $repository;
} I have to fix this conflict anyway, but I'm keen to see if you think that fix would work for you. |
The code for this is actually in the PEAR sniff, which already has some code to deal with functions that don't begin at tab-stops, so I'll need to look at that code and see if it can be improved. |
Ah yes. It has code to help fix this situation, but the PSR-2 standard specifically blocks it due to issue #1793. This is going to be pretty tricky to fix. |
Should have probably also commented (esp if I can't find a fix for this) that the other way I've seen people indent chained method calls works fine with PSR-2 and is probably easier to align given it uses tab stops: <?php
$repository
->expects(self::exactly($getExistingIdsCalls))
->method('getCategoryFeatureIds')
->willReturnCallback(
function (string $providerId, int $categoryId, array $idsList) use ($map) {
self::assertEquals($this->providerId, $providerId);
self::assertEquals($this->categoryId, $categoryId);
return \array_intersect($map, $idsList);
}
); I'm not suggesting this is the solution, but it might be worth considering something along those lines if you're already splitting up your chained calls to get PHPCS to stop reporting errors. |
@gsherwood , thanks for fast reply. I simply forgot about second way of formatting that you described. This formatting satisfy code sniffer. |
I don't think so, but I'll let you know if I need some help testing or someone to run ideas past. I'm still not really sure where to go with this, but I did get distracted with other work today. |
Today encountered problem with codesniffer on PSR1/PSR2 rules set.
In my class I have next part of code:
It is well formatted by PhpStorm but code sniffer throw next errors for this code part:
Firstly I decided that there is something with my IDE formatting settings and manually edited as required - I added 1 space for each marked line. But code sniffer again throw bad formatting error:
What should I do in this case?
P.S. As temporary bypass I edited my code part by splitting chained method calls:
The text was updated successfully, but these errors were encountered: