Skip to content

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

Closed as not planned

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 confirm that this bug is a bug in PHP_CodeSniffer and not in one of the external standards.
  • [o] I have verified the issue still exists in the master branch of PHP_CodeSniffer.

Activity

  1. jrfnl commented on Aug 13, 2026

    @jrfnl
    Contributor

    Please don't open issues in this repo. This repo is abandoned and https://github.com/PHPCSStandards/PHP_CodeSniffer is its successor. See: #3932

  2. LLyaudet commented on Aug 14, 2026

    @LLyaudet
    Author

    Hello @jrfnl ,
    Sorry about this. I recreated my issue on the correct repository.
    Have a nice day, best regards,
    Laurent Lyaudet

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions