Skip to content

PEAR.WhiteSpace.ObjectOperatorIndent expects wrong indentation when there are multiple chained calls in the same statement #1154

@Daimona

Description

@Daimona

NOTE: Copied over from squizlabs/PHP_CodeSniffer#3718 with a reduced example and some more information.

Describe the bug

If you have multiple chained calls within the same statement (for example, an array where multiple elements consist of chained method calls), but using different indentation, then the PEAR.WhiteSpace.ObjectOperatorIndent sniff will expect an incorrect amount of indentation.

Code sample

<?php

namespace Example;

function toJson(): array {
    return [
        'brand' => [
            'title' => $item->product()->brand(),
        ],
        'charges' => $item->charges()
            ->toArray(),
    ];
}

Custom ruleset

<?xml version="1.0"?>
<ruleset name="My Custom Standard">
  <rule ref="PEAR.WhiteSpace.ObjectOperatorIndent" />
</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
 11 | ERROR | [x] Object operator not indented correctly; expected 16 spaces but found 12
    |       |     (PEAR.WhiteSpace.ObjectOperatorIndent.Incorrect)
PHPCBF CAN FIX THE 1 MARKED SNIFF VIOLATIONS AUTOMATICALLY

Expected behavior

No issue emitted for the above code.

Versions (please complete the following information)

Operating System Ubuntu 24.04
PHP version 8.3
PHP_CodeSniffer version master
Standard custom
Install type composer

Additional context

The sniff does:

$end = $phpcsFile->findEndOfStatement($stackPtr);
$next = $phpcsFile->findNext($this->targets, ($stackPtr + 1), $end);

But the whole array literal in the above example is just one statement. So, when analyzing the chained calls at line 8, $next will find the chained calls at line 10-11 and expect them to be indented like the ones at line 8.

Please confirm

  • I have searched the issue list and am not opening a duplicate issue.
  • I have read the Contribution Guidelines and this is not a support question.
  • I confirm that this bug is a bug in PHP_CodeSniffer and not in one of the external standards.
  • I have verified the issue still exists in the master branch of PHP_CodeSniffer.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions