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

Generic.Formatting.MultipleStatementAlignment false positive for empty anonymous classes and closures #3219

Closed
LastDragon-ru opened this issue Feb 13, 2021 · 2 comments
Milestone

Comments

@LastDragon-ru
Copy link

Code sample

<?php declare(strict_types = 1);

namespace Bug;

class A {
    public function bug(): void {
        $a        = 123;
        $model    = new class() {
            // empty
        };
        $resource = new class() {
            // empty
        };

        $b        = 123;
        $closure  = function (): void {
            // empty
        };
        $resource = 123;
    }
}

Custom ruleset

<?xml version="1.0"?>
<ruleset name="Bug"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="./vendor/squizlabs/php_codesniffer/phpcs.xsd">
    <file>./packages</file>
    <rule ref="Generic.Formatting.MultipleStatementAlignment"/>
</ruleset>

To reproduce
Steps to reproduce the behavior:

  1. Create a file called test.php with the code sample above...
  2. Run phpcs test.php ...
  3. See error message displayed
$ ./vendor/bin/phpcs ./packages/Bug.php

FILE: /project/packages/Bug.php
---------------------------------------------------------------------------------------------------------------
FOUND 0 ERRORS AND 4 WARNINGS AFFECTING 4 LINES
---------------------------------------------------------------------------------------------------------------
  7 | WARNING | [x] Equals sign not aligned with surrounding assignments; expected 5 spaces but found 8 spaces
  8 | WARNING | [x] Equals sign not aligned with surrounding assignments; expected 1 space but found 4 spaces
 15 | WARNING | [x] Equals sign not aligned with surrounding assignments; expected 7 spaces but found 8 spaces
 16 | WARNING | [x] Equals sign not aligned with surrounding assignments; expected 1 space but found 2 spaces
---------------------------------------------------------------------------------------------------------------
PHPCBF CAN FIX THE 4 MARKED SNIFF VIOLATIONS AUTOMATICALLY
---------------------------------------------------------------------------------------------------------------

Time: 119ms; Memory: 8MB

Expected behavior

No warnings.

Versions (please complete the following information):

  • OS: [e.g., Windows 10, MacOS 10.15]: Ubuntu Latest
  • PHP: [e.g., 7.2, 7.4]: 8.0
  • PHPCS: [e.g., 3.5.5, master]: master
  • Standard: [e.g., PSR2, PSR12, Squiz]: none
@gsherwood
Copy link
Member

I've done some debugging and this is happening because of those empty lines containing just comments. The sniff is seeing those as having no code on them, so it looks like a break in the assignment block.

@gsherwood gsherwood added this to the 3.6.0 milestone Feb 14, 2021
@gsherwood gsherwood changed the title False positive Generic.Formatting.MultipleStatementAlignment.IncorrectWarning: Equals sign not aligned correctly for anonymous classes and closures Generic.Formatting.MultipleStatementAlignment false positive for empty anonymous classes and closures Feb 14, 2021
gsherwood added a commit that referenced this issue Feb 14, 2021
… positive for empty anonymous classes and closures
gsherwood added a commit that referenced this issue Feb 14, 2021
… positive for empty anonymous classes and closures
@gsherwood
Copy link
Member

I've committed a fix for this bug, which will be released in 3.6.0. Thanks for the report.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants