Skip to content

match() nested within if() statement breaks setting of "conditions" key #3750

@momala454

Description

@momala454

Describe the bug
The rule SlevomatCodingStandard.ControlStructures.EarlyExit will cause the following error at line 1 (but there is nothing on line 1), and there shouldn't be any error, the code is valid and doesn't contains any if without a curly brace

FOUND 1 ERROR AFFECTING 1 LINE
------------------------------------------------------------------------------------------------------------------------------------------------
 1 | ERROR | An error occurred during processing; checking has been aborted. The error message was: "if" without curly braces is not supported.
------------------------------------------------------------------------------------------------------------------------------------------------

Code sample

function testBugCurly(): void
{
    foreach ([1,2] as $step) {
        if (1 !== 1) {
            continue;
        }

        if (1 !== null) {
            if (!match (1) {
                default => 1,
            }) {
            }
        }
    }
}

Custom ruleset

<?xml version="1.0"?>
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="PSR12" xsi:noNamespaceSchemaLocation="../../../phpcs.xsd">
    <description>The PSR-12 coding standard.</description>
    <arg name="tab-width" value="4"/>

    <!-- Exclude the Composer Vendor directory. -->
	<exclude-pattern>/vendor/*</exclude-pattern>

    
    <!-- CUSTOM -->
    <config name="installed_paths" value="vendor/slevomat/coding-standard"/>

    <rule ref="SlevomatCodingStandard.ControlStructures.EarlyExit">
    </rule>

</ruleset>

Strangely, adding anything (even a comment) on the last line inside foreach avoid the error. Like this :

function testBugCurly(): void
{
    foreach ([1,2] as $step) {
        if (1 !== 1) {
            continue;
        }

        if (1 !== null) {
            if (!match (1) {
                default => 1,
            }) {
            }
        }
        // hello
    }
}

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 output here

Expected behavior
A clear and concise description of what you expected to happen.

Versions (please complete the following information):

  • OS:windows 10
  • PHP: 8.1
  • PHPCS: PHP_CodeSniffer version 3.7.1 (stable)
  • Standard: see xml above

Additional context
Add any other context about the problem here.

Initially reported there slevomat/coding-standard#1506 . They say :

         It looks like a bug in PHPCS. The `if (!match (1) {` is missing `scope_closer`.

Originally posted by @kukulich in slevomat/coding-standard#1506 (comment)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions