PHPCSStandards / PHP_CodeSniffer

PHP_CodeSniffer tokenizes PHP files and detects violations of a defined set of coding standards.
BSD 3-Clause "New" or "Revised" License
857 stars 53 forks source link

FunctionCallSignature works incorrectly #452

Open semirke opened 4 months ago

semirke commented 4 months ago

Describe the bug

Seems PEAR.Functions.FunctionCallSignature.Indent sniff and fix works incorrectly with some nested structures. The problem appears when there is indentation issue together with another issue, like PEAR.WhiteSpace.ScopeIndent.Incorrect.

After running phpcbf wrong number of indentation character (space) is not reported anymore.

The problem does not appear when correctly formatted and correct code is processed by phpcbf, nor does it mess up the good code.

Sample code

public function store()
{
  $fails  = $this->req(array(
    'device_id'=> array (
      'default' =>$this->app->param('device_id'),
      'min' => 0,
      'optional' => true
    ),
    )
    );
}

Phpcbf formatting

public function store()
{
    $fails  = $this->req(
        array(
        'device_id'=> array (
        'default' =>$this->app->param('device_id'),
        'min' => 0,
        'optional' => true
        ),
        )
    );
}

Another Phpcs wrong indent not caught:

public function store()
{
    $fails  = $this->req(
        array(
        'device_id'=> array ( // missing indent
                'default' =>$this->app->param('device_id'), 
                'min' => 0,
                'optional' => true
            ), // this line only has 2 spaces
        )
    );
}

Sample from PEAR standard

From: https://pear.php.net/manual/en/standards.funcalls.php

$this->someObject->subObject->callThisFunctionWithALongName(
    $this->someOtherFunc(
        $this->someEvenOtherFunc(
            'Help me!',
            array(
                'foo'  => 'bar',
                'spam' => 'eggs',
            ),
            23
        ),
        $this->someEvenOtherFunc()
    ),
    $this->wowowowowow(12)
);

Custom ruleset

No custom 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
  4. Run phpcbf test.php
  5. Check indentation
  6. phpcs test.php no longer reports indentation error.

PHPCS output here
-------------------------------------------------------------------------------------------------------------------------------------------------------
FOUND 9 ERRORS AFFECTING 6 LINES
-------------------------------------------------------------------------------------------------------------------------------------------------------
  2 | ERROR | [ ] Missing file doc comment (PEAR.Commenting.FileComment.Missing)
  2 | ERROR | [ ] Missing doc comment for function store() (PEAR.Commenting.FunctionComment.Missing)
  4 | ERROR | [x] Line indented incorrectly; expected at least 4 spaces, found 2 (PEAR.WhiteSpace.ScopeIndent.Incorrect)
  4 | ERROR | [x] Opening statement of multi-line function call not indented correctly; expected 0 spaces but found 2
    |       |     (PEAR.Functions.FunctionCallSignature.OpeningIndent)
  4 | ERROR | [x] Opening parenthesis of a multi-line function call must be the last content on the line
    |       |     (PEAR.Functions.FunctionCallSignature.ContentAfterOpenBracket)
  5 | ERROR | [x] Multi-line function call not indented correctly; expected 6 spaces but found 4 (PEAR.Functions.FunctionCallSignature.Indent)
  9 | ERROR | [x] Multi-line function call not indented correctly; expected 6 spaces but found 4 (PEAR.Functions.FunctionCallSignature.Indent)
 10 | ERROR | [x] Multi-line function call not indented correctly; expected 6 spaces but found 4 (PEAR.Functions.FunctionCallSignature.Indent)
 11 | ERROR | [x] Multi-line function call not indented correctly; expected 2 spaces but found 4 (PEAR.Functions.FunctionCallSignature.Indent)
-------------------------------------------------------------------------------------------------------------------------------------------------------
PHPCBF CAN FIX THE 7 MARKED SNIFF VIOLATIONS AUTOMATICALLY
-------------------------------------------------------------------------------------------------------------------------------------------------------

Time: 91ms; Memory: 6MB

Expected behavior

Phpcbf: follow standard while fixing. Phpcs: catch wrong indentation.

Versions (please complete the following information)

Operating System Debian 10
PHP version 7.4
PHP_CodeSniffer version PHP_CodeSniffer version 3.8.0 (stable) by Squiz (https://www.squiz.net)
Standard PEAR
Install type git clone

Please confirm

Thank you!

semirke commented 4 months ago

Hi, I edited the issue and added more details with better sample code.

semirke commented 4 months ago

My guess is sniffer would need recursive processing of multi line function calls. Or from inside to outside. I tried to look into it, but it was too much...