Skip to content

Commit b3ab2a5

Browse files
Add BCHelper
1 parent 957854c commit b3ab2a5

File tree

4 files changed

+40
-5
lines changed

4 files changed

+40
-5
lines changed

Diff for: src/Psalm/Internal/Analyzer/FunctionLike/ReturnTypeCollector.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use PhpParser\NodeTraverser;
77
use Psalm\Codebase;
88
use Psalm\Internal\Analyzer\Statements\Block\ForeachAnalyzer;
9+
use Psalm\Internal\BCHelper;
910
use Psalm\Internal\PhpVisitor\YieldTypeCollector;
1011
use Psalm\Internal\Provider\NodeDataProvider;
1112
use Psalm\Type;
@@ -83,7 +84,8 @@ public static function getReturnTypes(
8384
}
8485

8586
if ($stmt instanceof PhpParser\Node\Stmt\Expression) {
86-
if ($stmt->expr instanceof PhpParser\Node\Expr\Exit_) {
87+
if ($stmt->expr instanceof PhpParser\Node\Expr\Exit_
88+
|| (!BCHelper::usePHPParserV4() && $stmt->expr instanceof PhpParser\Node\Expr\Throw_)) {
8789
$return_types[] = Type::getNever();
8890

8991
break;

Diff for: src/Psalm/Internal/Analyzer/ScopeAnalyzer.php

+4-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Psalm\Internal\Analyzer;
44

55
use PhpParser;
6+
use Psalm\Internal\BCHelper;
67
use Psalm\Internal\Provider\NodeDataProvider;
78
use Psalm\NodeTypeProvider;
89

@@ -48,7 +49,7 @@ public static function getControlActions(
4849

4950
foreach ($stmts as $stmt) {
5051
if ($stmt instanceof PhpParser\Node\Stmt\Return_ ||
51-
$stmt instanceof PhpParser\Node\Stmt\Throw_ ||
52+
BCHelper::isThrow($stmt) ||
5253
($stmt instanceof PhpParser\Node\Stmt\Expression && $stmt->expr instanceof PhpParser\Node\Expr\Exit_)
5354
) {
5455
if (!$return_is_exit && $stmt instanceof PhpParser\Node\Stmt\Return_) {
@@ -406,7 +407,7 @@ public static function onlyThrowsOrExits(NodeTypeProvider $type_provider, array
406407
for ($i = count($stmts) - 1; $i >= 0; --$i) {
407408
$stmt = $stmts[$i];
408409

409-
if ($stmt instanceof PhpParser\Node\Stmt\Throw_
410+
if (BCHelper::isThrow($stmt)
410411
|| ($stmt instanceof PhpParser\Node\Stmt\Expression
411412
&& $stmt->expr instanceof PhpParser\Node\Expr\Exit_)
412413
) {
@@ -436,7 +437,7 @@ public static function onlyThrows(array $stmts): bool
436437
}
437438

438439
foreach ($stmts as $stmt) {
439-
if ($stmt instanceof PhpParser\Node\Stmt\Throw_) {
440+
if (BCHelper::isThrow($stmt)) {
440441
return true;
441442
}
442443
}

Diff for: src/Psalm/Internal/Analyzer/Statements/ExpressionAnalyzer.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
use Psalm\Internal\Analyzer\Statements\Expression\YieldAnalyzer;
4343
use Psalm\Internal\Analyzer\Statements\Expression\YieldFromAnalyzer;
4444
use Psalm\Internal\Analyzer\StatementsAnalyzer;
45+
use Psalm\Internal\BCHelper;
4546
use Psalm\Internal\FileManipulation\FileManipulationBuffer;
4647
use Psalm\Internal\Type\TemplateResult;
4748
use Psalm\Issue\RiskyTruthyFalsyComparison;
@@ -456,7 +457,9 @@ private static function handleExpression(
456457
return MatchAnalyzer::analyze($statements_analyzer, $stmt, $context);
457458
}
458459

459-
if ($stmt instanceof PhpParser\Node\Expr\Throw_ && $analysis_php_version_id >= 8_00_00) {
460+
if ($stmt instanceof PhpParser\Node\Expr\Throw_
461+
&& ($analysis_php_version_id >= 8_00_00 || !BCHelper::usePHPParserV4())
462+
) {
460463
return ThrowAnalyzer::analyze($statements_analyzer, $stmt, $context);
461464
}
462465

Diff for: src/Psalm/Internal/BCHelper.php

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace Psalm\Internal;
4+
5+
use PhpParser;
6+
use PhpParser\Node;
7+
8+
use function class_exists;
9+
10+
/**
11+
* @internal
12+
*/
13+
final class BCHelper
14+
{
15+
public static function usePHPParserV4(): bool
16+
{
17+
return class_exists('\PhpParser\Node\Stmt\Throw');
18+
}
19+
20+
public static function isThrow(Node $stmt): bool
21+
{
22+
if (self::usePHPParserV4()) {
23+
return $stmt instanceof PhpParser\Node\Stmt\Throw_;
24+
}
25+
26+
return $stmt instanceof PhpParser\Node\Stmt\Expression
27+
&& $stmt->expr instanceof PhpParser\Node\Expr\Throw_;
28+
}
29+
}

0 commit comments

Comments
 (0)