Php-cs-fixer: Wrong indentation of arguments for multiline function calls

Created on 27 Jan 2019  Â·  4Comments  Â·  Source: FriendsOfPHP/PHP-CS-Fixer

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

=> PHP 7.2.12 (cli) (built: Nov 9 2018 11:00:45) ( NTS )

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

=> PHP CS Fixer 2.14.0 Sunrise by Fabien Potencier and Dariusz Ruminski

The command you use to run PHP CS Fixer:

=> php vendor/bin/php-cs-fixer fix ./src

The configuration file you are using, if any:

No custom configuration.

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

  • before running PHP CS Fixer (no changes):
<?php

class C{
  public function __construct(){
    sprintf(
      '%s',
      'A',
      'B'
    );
  }
}
  • with unexpected changes applied when running PHP CS Fixer:
<?php

class C
{
    public function __construct()
    {
        sprintf(
      '%s',
      'A',
      'B'
    ); // -> whoops?!
    }
}
  • with the changes you expected instead:
<?php

class C
{
    public function __construct()
    {
        sprintf(
            '%s',
            'A',
            'B'
        );
    }
}
kinfeature request

Most helpful comment

_I think_ this is a priority case where BracesFixer needs to run before MethodArgumentSpace, or MethodArgumentSpace needs to be(come) aware of indentation level and indentation configuration.

All 4 comments

_I think_ this is a priority case where BracesFixer needs to run before MethodArgumentSpace, or MethodArgumentSpace needs to be(come) aware of indentation level and indentation configuration.

I get a similar issue with just indention_type set. Reappling the fixer will keep decreasing the indent level until every line is indented at most once.

--- Original
+++ New
@@ @@
 <?php

 function foo() {
   if (mt_rand() % 2) {
-    return 1;
+  return 1;
   }

   return 100;
 }

Test file:

<?php

function foo() {
  if (mt_rand() % 2) {
    return 1;
  }

  return 100;
}

Test config:

<?php

return PhpCsFixer\Config::create()
  ->setIndent('  ')
  ->setRules(['indentation_type' => true]);

Same behaviour in arrays

$foo = [
  'bar' => [
    'baz',
    'qrz' => [
      1,
      2
    ]
  ]
];

After 1st fix:

$foo = [
  'bar' => [
  'baz',
  'qrz' => [
    1,
    2
  ]
  ]
];

After 2nd fix:

$foo = [
  'bar' => [
  'baz',
  'qrz' => [
  1,
  2
  ]
  ]
];

Configuration:

$finder = PhpCsFixer\Finder::create()
  ->exclude('vendor')
  ->notPath('*.phtml')
  ->in(__DIR__);

return PhpCsFixer\Config::create()
  ->setIndent('  ')
  ->setRules([
    'indentation_type' => true,
  ])
  ->setFinder($finder);

This only happens if indentation is user-defined (not 4 spaces).

Probably caused by this line:
https://github.com/FriendsOfPHP/PHP-CS-Fixer/blob/2.14/src/Fixer/Whitespace/IndentationTypeFixer.php#L136

I have the same issue as #4282

The original code :

    public function __construct(
        UserPasswordEncoderInterface $userPasswordEncoder,
        AccessTokenManager $accessTokenManager,
        RefreshTokenManager $refreshTokenManager,
        AuthorizationCodeManager $authorizationCodeManager
    )
    {
        $this->userPasswordEncoder = $userPasswordEncoder;
        $this->accessTokenManager = $accessTokenManager;
        $this->refreshTokenManager = $refreshTokenManager;
        $this->authorizationCodeManager = $authorizationCodeManager;
    }

And PHP-CS-Fixer fix :

    public function __construct(
        UserPasswordEncoderInterface $userPasswordEncoder,
        AccessTokenManager $accessTokenManager,
        RefreshTokenManager $refreshTokenManager,
        AuthorizationCodeManager $authorizationCodeManager
    ) {
        $this->userPasswordEncoder = $userPasswordEncoder;
        $this->accessTokenManager = $accessTokenManager;
        $this->refreshTokenManager = $refreshTokenManager;
        $this->authorizationCodeManager = $authorizationCodeManager;
    }

Do you have any workaround to bypass this ?
Our CI/CD is broken because of this :(

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kcloze picture kcloze  Â·  3Comments

OskarStark picture OskarStark  Â·  3Comments

teohhanhui picture teohhanhui  Â·  3Comments

ro0NL picture ro0NL  Â·  3Comments

aidantwoods picture aidantwoods  Â·  3Comments