Skip to content

Squiz.PHP.DisallowComparisonAssignment false positive for PHP 8 match expression #3616

Closed
@frmnch

Description

@frmnch

Describe the bug
When using PHP 8 match() expression, the AssignedComparison validation shows a message, telling that this is not valid.

I don't think this is on purpose as this is the way how match should be used accoding to the PHP docs.
https://www.php.net/manual/de/control-structures.match.php

Code sample

<?php
test();

/**
 * @return void
 */
function test() {
    $food = 'cake';

    $returnValue = match (true) {
        $food === 'apple' => 'This food is an apple',
        $food === 'bar' => 'This food is a bar',
        $food === 'cake' => 'This food is a cake',
    };
}

Custom ruleset

<?xml version="1.0"?>
<ruleset name="My Custom Standard">
    <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. Run phpcs test.php ...
  3. See error message displayed
phpcs: Squiz.PHP.DisallowComparisonAssignment.AssignedComparison: The value of a comparison must not be assigned to a variable

Expected behavior
No warning. Instead it should be a valid usage of the match expression.

Versions (please complete the following information):

  • OS: Windows 10
  • PHP: 8.1
  • PHPCS: 3.7.0
  • Standard: Custom

Additional context
none

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions