-
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
PSR12.Classes.AnonClassDeclaration incorrectly enforcing that closing brace be on a line by itself #2678
Comments
This might be related to issue #2621 or PR #2622. I have also found a layout that phpcs does not complain about: <?php
namespace Example;
class Example
{
public function __construct()
{
$this->foo(
new class
{
public function bar()
{
}
}
);
}
public function foo($input)
{
}
} I'm happy with this as a workaround, but others might consider this a breaking change. |
The reason for this is because the included PSR12 standard was only about 50% complete until 3.5.0, so you may have also noticed a lot more errors being generated. This issue is caused by the reuse of the PSR2 sniff to help aid in class checking. I think PSR-12 just needs to ignore this specific error code, but I'll make sure it is still enforcing the closing brace placement rules that the anon class formatting is inheriting for closures. |
…rcing that closing brace be on a line by itself
This is now fixed and both those code snippets pass without error. I made the change in the PSR2 sniff directly as PSR2 doesn't mention anon classes so it probably shouldn't be that strict about them there. That had the effect of fixing PSR12 as well. Thanks for reporting this. |
Issue squizlabs/PHP_CodeSniffer 2621 added the `T_COMMA` token to the `$ignoreTokens` (without adding a test) to prevent a false positive for a anonymous class declaration nested within a function call. That fix was later superseded by another fix for basically the same issue via squizlabs/PHP_CodeSniffer#2678, which excluded anonymous classes completely from the `CloseBraceSameLine` check. This commit adds the test case from squizlabs/PHP_CodeSniffer 2621 and removed the redundant `T_COMMA` token as the test now confirms it is no longer needed.
Issue squizlabs/PHP_CodeSniffer 2621 added the `T_COMMA` token to the `$ignoreTokens` (without adding a test) to prevent a false positive for a anonymous class declaration nested within a function call. That fix was later superseded by another fix for basically the same issue via squizlabs/PHP_CodeSniffer#2678, which excluded anonymous classes completely from the `CloseBraceSameLine` check. This commit adds the test case from squizlabs/PHP_CodeSniffer 2621 and removed the redundant `T_COMMA` token as the test now confirms it is no longer needed.
Issue squizlabs/PHP_CodeSniffer 2621 added the `T_COMMA` token to the `$ignoreTokens` (without adding a test) to prevent a false positive for a anonymous class declaration nested within a function call. That fix was later superseded by another fix for basically the same issue via squizlabs/PHP_CodeSniffer#2678, which excluded anonymous classes completely from the `CloseBraceSameLine` check. This commit adds the test case from squizlabs/PHP_CodeSniffer 2621 and removed the redundant `T_COMMA` token as the test now confirms it is no longer needed.
There appears to be no accepted way to format anonymous classes. This broke between 3.4.2 and 3.5.1.
I am using the following ruleset:
I would normally format anonymous classes like the following example:
There doesn't seem to be any way to solve this while still satisfying other rules. If I move the closing parenthesis onto the next line, I get error message "Expected 0 spaces before closing bracket; newline found".
The text was updated successfully, but these errors were encountered: