Skip to content

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

@ZebulanStanphill

Description

@ZebulanStanphill

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):

  • OS: KDE Neon Linux
  • PHP: 8.1
  • PHPCS: 9.5.20
  • Standard: Squiz

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