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

Squiz.PHP.DisallowComparisonAssignment false positive when comparison inside closure #3165

Closed
stealeks opened this issue Nov 17, 2020 · 1 comment
Milestone

Comments

@stealeks
Copy link

Describe the bug
DisallowComparisonAssignment triggered on callbacks with comparison inside, assigned to variables

Code sample

<?php

$array = [];

$first = array_filter($array, function ($value) {
    if ($value > 10) {
        return false;
    }

    return true;
});

$callback = function ($value) {
    if ($value > 10) {
        return false;
    }

    return true;
};

$second = array_filter($array, $callback);

Custom ruleset

<?xml version="1.0"?>
<ruleset name="test" xsd="vendor/squizlabs/php_codesniffer/phpcs.xsd">
    <rule ref="Squiz.PHP.DisallowComparisonAssignment"/>
</ruleset>

To reproduce
Steps to reproduce the behavior:

  1. Create a file called test.php with the code sample above
  2. Create a file called ruleset.xml with the code ruleset above
  3. Run phpcs --standard="ruleset.xml" test.php
  4. See error message displayed
FILE: test.php
---------------------------------------------------------------------------
FOUND 1 ERROR AFFECTING 1 LINE
---------------------------------------------------------------------------
 13 | ERROR | The value of a comparison must not be assigned to a variable
---------------------------------------------------------------------------

Time: 21ms; Memory: 4MB

Expected behavior
No output (no errors)

Versions:

  • OS: Linux (Ubuntu 20.04.1 LTS)
  • PHP: 7.4.3
  • PHPCS: 3.5.8
  • Standard: Custom

Additional context
Similar bug was already fixed in 3.5.7:
Fixed Squiz.PHP.DisallowComparisonAssignment false positive for methods called on an object

@gsherwood gsherwood changed the title Squiz.PHP.DisallowComparisonAssignment false positive for callbacks with comparison inside, assigned to variables Squiz.PHP.DisallowComparisonAssignment false positive when comparison inside closure Nov 17, 2020
@gsherwood gsherwood added this to the 3.6.0 milestone Nov 17, 2020
gsherwood added a commit that referenced this issue Nov 17, 2020
gsherwood added a commit that referenced this issue Nov 17, 2020
@gsherwood
Copy link
Member

Thanks for the bug report. I've committed a fix, which will be in the 3.6.0 release.

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

No branches or pull requests

2 participants