Skip to content

DisallowMultipleAssignments false positive when assigning to variable property of an object in an array #598

@GaryJones

Description

@GaryJones

Describe the bug

False positive on a complex statement that suggests the assignment is not the first block of code.

Try assigning a value to a property of an object contained in an (indexed) array.

Code sample

<?php

$filtered_results[ $i ]->$field = $result;

Custom ruleset

Can be reproduced with the WordPress-Extra or Squiz rulesets, or any combination using the Squiz.PHP.DisallowMultipleAssignments rule.

To reproduce

Steps to reproduce the behavior:

  1. Create a file called test.php with the code sample above...
  2. Run phpcs test.php --standard=Squiz --sniffs=Squiz.PHP.DisallowMultipleAssignments -s or similar
  3. See error message displayed
3 | ERROR | Assignments must be the first block of code on a line (Squiz.PHP.DisallowMultipleAssignments.Found)

Expected behavior

No violation reported.

Versions (please complete the following information)

Operating System MacOS 14.6.1
PHP version 7.4
PHP_CodeSniffer version 3.9.2
Standard Squiz
Install type Composer

Additional context

None of the tests seem to have an assignment to a property of an object in an array.

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

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions