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

Anonymous classes' constructor params are forced on one line #4628

Closed
SvenRtbg opened this issue Nov 6, 2019 · 3 comments
Closed

Anonymous classes' constructor params are forced on one line #4628

SvenRtbg opened this issue Nov 6, 2019 · 3 comments

Comments

@SvenRtbg
Copy link

SvenRtbg commented Nov 6, 2019

When reporting an issue (bug) please provide the following information:

The PHP version you are using ($ php -v):

PHP 7.2.21-1+ubuntu16.04.1+deb.sury.org+1 (cli) (built: Aug 7 2019 09:53:30) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
with Zend OPcache v7.2.21-1+ubuntu16.04.1+deb.sury.org+1, Copyright (c) 1999-2018, by Zend Technologies

PHP CS Fixer version you are using ($ php-cs-fixer -V):

PHP CS Fixer 2.16.0 Yellow Bird by Fabien Potencier and Dariusz Ruminski

The command you use to run PHP CS Fixer:

php-cs-fixer fix --diff --dry-run --rules=class_definition .

The configuration file you are using, if any:

none

If applicable, please provide minimum samples of PHP code (as plain text, not screenshots):

  • before running PHP CS Fixer (no changes):
$class = new class(
    $foo,
    $bar,
    $baz
) extends \Psr\Log\AbstractLogger implements \Psr\Log\LoggerInterface {
    public function __construct($foo, $bar, $baz)
    {
    }

    public function log($level, $message, array $context = array())
    {
        echo $message;
    }
};
  • with unexpected changes applied when running PHP CS Fixer:
$class = new class($foo, $bar, $baz) extends \Psr\Log\AbstractLogger implements \Psr\Log\LoggerInterface {

The multiline constructor parameters are forced onto one line. This example uses short var names, using longer var names and outer class properties (like injecting mock objects in tests) makes this more obvious that creating a very long single line is not a good idea.

  • with the changes you expected instead:
    PSR12 states in section 8: Anonymous Classes MUST follow the same guidelines and principles as closures in the above section.

Section 7 has this example

$longArgs_longVars = function (
    $longArgument,
    $longerArgument,
    $muchLongerArgument
) use (
    $longVar1,
    $longerVar2,
    $muchLongerVar3
) {
   // body
};

I'd expect that using multiline argument lists like in the example code is completely correct, no changes should take place at all.

@SvenRtbg
Copy link
Author

SvenRtbg commented Nov 6, 2019

Looks like an open issue for PSR12 support #4502

@SpacePossum
Copy link
Contributor

Hi and thanks for reporting,

The fixer does this as part of PSR2:

4.1 The extends and implements keywords MUST be declared on the same line as the class name.

However anonymous classes have no names, so one could reason that the fixer is to strict here.

Your case can be indeed be fixed through PSR12 as well though 👍

@SpacePossum
Copy link
Contributor

closing as duplicate of #4289

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