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

Unexpected spacing for anonymous classes with constructor arguments #3200

Closed
TimWolla opened this issue Jan 22, 2021 · 3 comments
Closed

Unexpected spacing for anonymous classes with constructor arguments #3200

TimWolla opened this issue Jan 22, 2021 · 3 comments

Comments

@TimWolla
Copy link

TimWolla commented Jan 22, 2021

Describe the bug

When creating an anonymous class while passing constructor arguments phpcs requests a space between class and the (. I did not expect this and it looks off.

Code sample

<?php

namespace foo;

class Foo
{
    protected function foo()
    {
        new class(1) extends Bar { // line 9
        };
    }
}

Custom ruleset

<?xml version="1.0"?>
<ruleset>
	<file>./test/</file>
	<arg name="extensions" value="php" />
	<arg value="p"/>
	<arg name="basepath" value="."/>

	<rule ref="PSR12"/>
</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
FILE: test/test.php
----------------------------------------------------------------------
FOUND 1 ERROR AFFECTING 1 LINE
----------------------------------------------------------------------
 9 | ERROR | [x] Expected 1 space after class keyword; 0 found
----------------------------------------------------------------------
PHPCBF CAN FIX THE 1 MARKED SNIFF VIOLATIONS AUTOMATICALLY
----------------------------------------------------------------------

Expected behavior

I expected that error not to be reported, because I believe there should not be a space between class and (.

Versions (please complete the following information):

  • OS: Docker container (Debian based) on Ubuntu host.
  • PHP: 7.4.12
  • PHPCS: 3.5.8 (stable)
  • Standard: PSR12

Additional context

PHP-CS-Fixer with the PSR2 ruleset expects the space to not be there.

@TimWolla
Copy link
Author

Looking at this: php-fig/fig-standards#1206 the consensus of the standard appears to be that the space should not be there, even if the current version of PSR-12 does not say anything regarding this topic.

@gsherwood
Copy link
Member

If the errata is accepted then I'd change the PSR-12 standard included with PHPCS. But right now the PSR-12 standard says:

Anonymous Classes MUST follow the same guidelines and principles as closures in the above section.

And in the closures section, all the examples clearly show a space between the function keyword and the opening parenthesis of the argument list. So that's all I have to go on until a change is made to clarify things.

@TimWolla
Copy link
Author

TimWolla commented Oct 5, 2021

For the record: PHP CS Fixer was changed to match PHP CodeSniffer: PHP-CS-Fixer/PHP-CS-Fixer#5924

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

No branches or pull requests

2 participants