Skip to content

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

@dylan-kerr

Description

@dylan-kerr

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions