Skip to content

False positive for Squiz.PHP.DisallowComparisonAssignment.AssignedComparison #1481

Description

@LLyaudet

Describe the bug

Squiz.PHP.DisallowComparisonAssignment.AssignedComparison
yields a false positive on the following code:

$this->s_dirname = (
  isset($path_parts['dirname']) ? $path_parts['dirname'] : ''
);
-----------------------------------------------------------------------------------------------------------------------------------------
FOUND 1 ERROR AFFECTING 1 LINE
-----------------------------------------------------------------------------------------------------------------------------------------
LINE 183: ERROR The value of a comparison must not be assigned to a variable (Squiz.PHP.DisallowComparisonAssignment.AssignedComparison)
-----------------------------------------------------------------------------------------------------------------------------------------
   181:  ······$this->s_filepath·=·$s_filepath;
   182:  ······$path_parts·=·pathinfo($this->s_filepath);
>> 183:  ······$this->s_dirname·=·(
   184:  ········isset($path_parts['dirname'])·?·$path_parts['dirname']·:·''
   185:  ······);
-----------------------------------------------------------------------------------------------------------------------------------------

Code sample

<?php

$arr = [];
$s = isset($arr['a']) ? $arr['a'] : 'b';

Custom ruleset

Not needed

To reproduce

Steps to reproduce the behavior:

  1. Create a file called test.php with the code sample above...
  2. Run phpcs --report=code --standard=SQUIZ test.php`
  3. See error message displayed
FILE: /home/laurent/test.php
------------------------------------------------------------------------------------------------------------
FOUND 5 ERRORS AFFECTING 2 LINES
------------------------------------------------------------------------------------------------------------
LINE 1: ERROR [ ] Missing file doc comment
------------------------------------------------------------------------------------------------------------
>> 1:  <?php
   2:  
   3:  $arr·=·[];
------------------------------------------------------------------------------------------------------------
LINE 4: ERROR [x] Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space
LINE 4: ERROR [ ] The value of a comparison must not be assigned to a variable
LINE 4: ERROR [ ] Inline IF statements are not allowed
LINE 4: ERROR [ ] Implicit true comparisons prohibited; use === TRUE instead
------------------------------------------------------------------------------------------------------------
   2:  
   3:  $arr·=·[];
>> 4:  $s·=·isset($arr['a'])·?·$arr['a']·:·'b';
------------------------------------------------------------------------------------------------------------
PHPCBF CAN FIX THE 1 MARKED SNIFF VIOLATIONS AUTOMATICALLY
------------------------------------------------------------------------------------------------------------
Time: 37ms; Memory: 8MB

Expected behavior

No error "The value of a comparison must not be assigned to a variable".

Versions (please complete the following information)

Operating System Ubuntu 26.04
PHP version 8.5.4
PHP_CodeSniffer version 4.0.4
Standard Squiz
Install type Composer global

Additional context

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 4.x branch of PHP_CodeSniffer.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions