squizlabs / PHP_CodeSniffer

PHP_CodeSniffer tokenizes PHP files and detects violations of a defined set of coding standards.
BSD 3-Clause "New" or "Revised" License
10.68k stars 1.48k forks source link

PHP 8 throw expressions not supported by `Squiz.PHP.NonExecutableCode` rule #3592

Closed ZebulanStanphill closed 1 year ago

ZebulanStanphill commented 2 years ago

Describe the bug The throw expression added in PHP 8 is not supported by the Squiz.PHP.NonExecutableCode rule.

Code sample

<?php declare(strict_types=1);

function doStuff(?string $thing): string {
    $foo = $thing ?? throw new \Exception();

    return $foo;
}

Custom ruleset

<?xml version="1.0"?>
<ruleset name="My Custom Standard">
  <rule ref="Squiz.PHP.NonExecutableCode" />
</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
    6 | WARNING | Code after the THROW statement on line 4 cannot be executed

Expected behavior Throw expressions should be recognized as expressions, not statements.

Versions (please complete the following information):

derrabus commented 2 years ago

related: #2857

jrfnl commented 12 months ago

FYI: the fix for this issue is included in today's PHP_CodeSniffer 3.8.0 release.

As per #3932, development on PHP_CodeSniffer will continue in the PHPCSStandards/PHP_CodeSniffer repository. If you want to stay informed, you may want to start "watching" that repo (or watching releases from that repo).