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

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

Closed
4 tasks done
GaryJones opened this issue Aug 16, 2024 · 2 comments · Fixed by #610
Closed
4 tasks done

Comments

@GaryJones
Copy link
Contributor

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.
@jrfnl jrfnl changed the title DisallowedMultipleAssignments false positive when assigning to property of an object in an array DisallowMultipleAssignments false positive when assigning to variable property of an object in an array Aug 17, 2024
@jrfnl
Copy link
Member

jrfnl commented Aug 17, 2024

@GaryJones Thanks for reporting this. I can confirm the issue, which appears to be caused by the variable variable, i.e. the variable for the property name:

$filtered_results[] = $result;
$filtered_results[ $i ] = $result;

$filtered_results->field = $result;
$filtered_results->$field = $result;

$filtered_results[ $i ]->field = $result;
$filtered_results[ $i ]->$field = $result; // <- This is the bug, all other test cases in this code sample are handled correctly.

@jrfnl
Copy link
Member

jrfnl commented Sep 8, 2024

@GaryJones PR #610 should fix this. Testing appreciated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants