Skip to content

PSR12 does not forbid blank lines at the start of the class body #3437

@InvisibleSmiley

Description

@InvisibleSmiley

Describe the bug
In certain situations, PSR12 does not forbid blank lines at the start of class bodies, e.g. with class constants at the top.
This is in contrast to:

  • the definition of PSR-12 (https://www.php-fig.org/psr/psr-12/) that states "Opening braces MUST be on their own line and MUST NOT be preceded or followed by a blank line."
  • the comment above <rule ref="PSR2.Classes.ClassDeclaration"/> in file PSR12/ruleset.xml which states: "Opening braces MUST be on their own line and MUST NOT be preceded or followed by a blank line."

In some other cases, blank lines at the start of class bodies are rejected by specific rules, e.g. those for trait imports (use), properties and methods (functions).

Code sample

<?php

namespace Foo;

class Bar
{


    public const X = 1;
}

Expected behavior
Blank lines at the start of class bodies should always result in some sort of error with PSR12.

Versions (please complete the following information):

  • OS: Windows 10
  • PHP: 7.4
  • PHPCS: 3.6.0
  • Standard: PSR12

Additional context
I think the ClassDeclaration sniff neeeds to be adjusted for PSR12 so that it does not matter which kind of element is at the start of the class body (constants, property, method, ...). Leaving this logic to specific sniffs might result in future breakages as PHP evolves.

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