Skip to content

PSR2.ControlStructures.SwitchDeclaration.TerminatingComment false positive with never returning function call #657

@anthonyryan1

Description

@anthonyryan1

Describe the bug

I'm not certain if this is in scope, but I'm curious if phpcs could treat never functions as comparable to exit;

Code sample

<?php

declare(strict_types=1);

function error(): never
{
    // Do logging
    exit;
}


switch ($Var) {
    case 'one':
        // Do stuff
        break;
    case 'two':
        error();
    case 'three':
        // Do stuff
        break;
}

Custom ruleset

<?xml version="1.0"?>
<ruleset name="PSR12">
    <config name="testVersion" value="8.3-"/>
    <arg name="extensions" value="php"/>
    <arg name="ignore" value="vendor/"/>
    <arg name="colors" />
    <arg value="sp"/>
    <rule ref="PSR12">
        <exclude name="Generic.Files.LineLength.TooLong" />
    </rule>
    <rule ref="Generic.PHP.RequireStrictTypes" />
    <rule ref="Generic.PHP.DisallowShortOpenTag" />
</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
---------------------------------------------------------------------------------------------------------------
FOUND 1 ERROR AND 1 WARNING AFFECTING 2 LINES
---------------------------------------------------------------------------------------------------------------
  1 | WARNING | A file should declare new symbols (classes, functions, constants, etc.) and cause no other
    |         | side effects, or it should execute logic with side effects, but should not do both. The first
    |         | symbol is defined on line 5 and the first side effect is on line 12.
    |         | (PSR1.Files.SideEffects.FoundWithSymbols)
 16 | ERROR   | There must be a comment when fall-through is intentional in a non-empty case body
    |         | (PSR2.ControlStructures.SwitchDeclaration.TerminatingComment)
---------------------------------------------------------------------------------------------------------------


Expected behavior

There should be no error on line 16, because it's unreachable.

The obvious workarounds of doing:

error();
break;

regrettably causes the 3rd party project phpstan to rightly throw an error than the break statement is unreachable. So it fixes the phpcs error, and creates a phpstan one. Making it a bit inconvenient.

Versions (please complete the following information)

Operating System Gentoo Linux
PHP version 8.3.13
PHP_CodeSniffer version 3.10.3
Standard PSR12
Install type composer

Additional context

Add any other context about the problem here.

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.
  • I have verified the issue still exists in the master 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