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 does not forbid blank lines at the start of the class body #3437

Closed
InvisibleSmiley opened this issue Sep 17, 2021 · 4 comments
Closed

Comments

@InvisibleSmiley
Copy link

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.

@scorgn
Copy link

scorgn commented Sep 29, 2021

I am seeing this as well, it seems that it is also not notifying of a blank line after a method opening brace.

  1. Control Structures
    [...]
    The body MUST be on the next line after the opening brace

I am on

  • OS: MacOS Catalina 10.15.7
  • PHP: 7.3.30
  • PHPCS: 3.6.0
  • Standard: PSR12

@gsherwood gsherwood added this to Idea Bank in PHPCS v3 Development via automation Sep 29, 2021
@gsherwood gsherwood added this to the 3.6.2 milestone Sep 29, 2021
@gsherwood
Copy link
Member

This looks to be a missing check from the start. PSR2 allows as many blank lines as you want after the opening brace, so using that sniff alone was not enough to enforce the PSR12 rule. I'll add a new sniff to specifically enforce no blank lines after the opening brace.

@gsherwood
Copy link
Member

it seems that it is also not notifying of a blank line after a method opening brace.

I think that's just for control structures, not functions and methods. I can't see a rule under methods that enforces no blank lines at the start of the method body

@gsherwood gsherwood changed the title PSR12 does not always forbid blank lines at the start of the class body PSR12 does not forbid blank lines at the start of the class body Nov 22, 2021
@gsherwood
Copy link
Member

I've added PSR12.Classes.OpeningBraceSpace to enforce this. The sample code provided now produces this error:

FILE: temp.php
----------------------------------------------------------------------
FOUND 1 ERROR AFFECTING 1 LINE
----------------------------------------------------------------------
 6 | ERROR | [x] Opening brace must not be followed by a blank line
----------------------------------------------------------------------
PHPCBF CAN FIX THE 1 MARKED SNIFF VIOLATIONS AUTOMATICALLY
----------------------------------------------------------------------

It is auto-fixed like this:

--- temp.php
+++ PHP_CodeSniffer
@@ -4,7 +4,5 @@

 class Bar
 {
-
-
     public const X = 1;
 }

Thanks for reporting this.

PHPCS v3 Development automation moved this from Idea Bank to Ready for Release Nov 28, 2021
mbmjertan pushed a commit to infinum/eightshift-libs that referenced this issue Dec 13, 2021
Fixes PSR12.Classes.OpeningBraceSpace.Found errors introduced in
PHPCS release 3.6.2 in the whole project: classes can't start with
a blank line

For more, see:

PHPCS release notes
https://github.com/squizlabs/PHP_CodeSniffer/releases/tag/3.6.2
PHPCS issue:
squizlabs/PHP_CodeSniffer#3437
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

3 participants