Skip to content

Commit 48f3644

Browse files
committed
Fix ThrowExpressionRule
1 parent 951971c commit 48f3644

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

conf/config.neon

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,11 @@ services:
334334
tags:
335335
- phpstan.parser.richParserNodeVisitor
336336

337+
-
338+
class: PHPStan\Parser\StandaloneThrowExprVisitor
339+
tags:
340+
- phpstan.parser.richParserNodeVisitor
341+
337342
-
338343
class: PHPStan\Parser\TryCatchTypeVisitor
339344
tags:
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Parser;
4+
5+
use PhpParser\Node;
6+
use PhpParser\NodeVisitorAbstract;
7+
8+
class StandaloneThrowExprVisitor extends NodeVisitorAbstract
9+
{
10+
11+
public const ATTRIBUTE_NAME = 'standaloneThrowExpr';
12+
13+
public function enterNode(Node $node)
14+
{
15+
if (!$node instanceof Node\Stmt\Expression) {
16+
return null;
17+
}
18+
19+
if (!$node->expr instanceof Node\Expr\Throw_) {
20+
return null;
21+
}
22+
23+
$node->expr->setAttribute(self::ATTRIBUTE_NAME, true);
24+
25+
return $node;
26+
}
27+
28+
}

src/Rules/Exceptions/ThrowExpressionRule.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use PhpParser\Node;
66
use PHPStan\Analyser\Scope;
7+
use PHPStan\Parser\StandaloneThrowExprVisitor;
78
use PHPStan\Php\PhpVersion;
89
use PHPStan\Rules\Rule;
910
use PHPStan\Rules\RuleErrorBuilder;
@@ -29,6 +30,10 @@ public function processNode(Node $node, Scope $scope): array
2930
return [];
3031
}
3132

33+
if ($node->getAttribute(StandaloneThrowExprVisitor::ATTRIBUTE_NAME) === true) {
34+
return [];
35+
}
36+
3237
return [
3338
RuleErrorBuilder::message('Throw expression is supported only on PHP 8.0 and later.')->nonIgnorable()
3439
->identifier('throw.notSupported')

0 commit comments

Comments
 (0)