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

PSR12.Classes.AnonClassDeclaration incorrectly enforcing that closing brace be on a line by itself #2678

Closed
dylan-kerr opened this issue Nov 1, 2019 · 3 comments

Comments

@dylan-kerr
Copy link

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:

    <rule ref="PSR12"/>
    <rule ref="Generic.Arrays.DisallowLongArraySyntax"/>
    <rule ref="Generic.Formatting.SpaceAfterNot">
        <properties>
            <property name="spacing" value="0"/>
        </properties>
    </rule>

I would normally format anonymous classes like the following example:

<?php

namespace Example;

class Example
{
    public function __construct()
    {
        $this->foo(new class {
            public function bar()
            {
            }
        }); // <-- New error message: "Closing class brace must be on a line by itself"
    }

    public function foo($input)
    {
    }
}

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".

@dylan-kerr
Copy link
Author

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.

@gsherwood gsherwood changed the title Rule conflict for anonymous classes PSR12.Classes.AnonClassDeclaration incorrectly enforcing that closing brace be on a line by itself Nov 3, 2019
@gsherwood gsherwood added this to Idea Bank in PHPCS v3 Development via automation Nov 3, 2019
@gsherwood
Copy link
Member

This broke between 3.4.2 and 3.5.1

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.

gsherwood added a commit that referenced this issue Nov 3, 2019
…rcing that closing brace be on a line by itself
@gsherwood
Copy link
Member

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.

PHPCS v3 Development automation moved this from Idea Bank to Ready for Release Nov 3, 2019
@gsherwood gsherwood added this to the 3.5.3 milestone Nov 3, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
PHPCS v3 Development
Ready for Release
Development

No branches or pull requests

2 participants