From b3ab2a5d0a7266c4685b0eec6b77172d34f9ed21 Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Thu, 25 Jul 2024 08:49:18 +0200 Subject: [PATCH] Add BCHelper --- .../FunctionLike/ReturnTypeCollector.php | 4 ++- src/Psalm/Internal/Analyzer/ScopeAnalyzer.php | 7 +++-- .../Statements/ExpressionAnalyzer.php | 5 +++- src/Psalm/Internal/BCHelper.php | 29 +++++++++++++++++++ 4 files changed, 40 insertions(+), 5 deletions(-) create mode 100644 src/Psalm/Internal/BCHelper.php diff --git a/src/Psalm/Internal/Analyzer/FunctionLike/ReturnTypeCollector.php b/src/Psalm/Internal/Analyzer/FunctionLike/ReturnTypeCollector.php index 2d99b3435af..02dd19ad8b5 100644 --- a/src/Psalm/Internal/Analyzer/FunctionLike/ReturnTypeCollector.php +++ b/src/Psalm/Internal/Analyzer/FunctionLike/ReturnTypeCollector.php @@ -6,6 +6,7 @@ use PhpParser\NodeTraverser; use Psalm\Codebase; use Psalm\Internal\Analyzer\Statements\Block\ForeachAnalyzer; +use Psalm\Internal\BCHelper; use Psalm\Internal\PhpVisitor\YieldTypeCollector; use Psalm\Internal\Provider\NodeDataProvider; use Psalm\Type; @@ -83,7 +84,8 @@ public static function getReturnTypes( } if ($stmt instanceof PhpParser\Node\Stmt\Expression) { - if ($stmt->expr instanceof PhpParser\Node\Expr\Exit_) { + if ($stmt->expr instanceof PhpParser\Node\Expr\Exit_ + || (!BCHelper::usePHPParserV4() && $stmt->expr instanceof PhpParser\Node\Expr\Throw_)) { $return_types[] = Type::getNever(); break; diff --git a/src/Psalm/Internal/Analyzer/ScopeAnalyzer.php b/src/Psalm/Internal/Analyzer/ScopeAnalyzer.php index 808010d09bc..54bdd54c540 100644 --- a/src/Psalm/Internal/Analyzer/ScopeAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/ScopeAnalyzer.php @@ -3,6 +3,7 @@ namespace Psalm\Internal\Analyzer; use PhpParser; +use Psalm\Internal\BCHelper; use Psalm\Internal\Provider\NodeDataProvider; use Psalm\NodeTypeProvider; @@ -48,7 +49,7 @@ public static function getControlActions( foreach ($stmts as $stmt) { if ($stmt instanceof PhpParser\Node\Stmt\Return_ || - $stmt instanceof PhpParser\Node\Stmt\Throw_ || + BCHelper::isThrow($stmt) || ($stmt instanceof PhpParser\Node\Stmt\Expression && $stmt->expr instanceof PhpParser\Node\Expr\Exit_) ) { if (!$return_is_exit && $stmt instanceof PhpParser\Node\Stmt\Return_) { @@ -406,7 +407,7 @@ public static function onlyThrowsOrExits(NodeTypeProvider $type_provider, array for ($i = count($stmts) - 1; $i >= 0; --$i) { $stmt = $stmts[$i]; - if ($stmt instanceof PhpParser\Node\Stmt\Throw_ + if (BCHelper::isThrow($stmt) || ($stmt instanceof PhpParser\Node\Stmt\Expression && $stmt->expr instanceof PhpParser\Node\Expr\Exit_) ) { @@ -436,7 +437,7 @@ public static function onlyThrows(array $stmts): bool } foreach ($stmts as $stmt) { - if ($stmt instanceof PhpParser\Node\Stmt\Throw_) { + if (BCHelper::isThrow($stmt)) { return true; } } diff --git a/src/Psalm/Internal/Analyzer/Statements/ExpressionAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/ExpressionAnalyzer.php index c19c8df51a6..5474cdb03a3 100644 --- a/src/Psalm/Internal/Analyzer/Statements/ExpressionAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/ExpressionAnalyzer.php @@ -42,6 +42,7 @@ use Psalm\Internal\Analyzer\Statements\Expression\YieldAnalyzer; use Psalm\Internal\Analyzer\Statements\Expression\YieldFromAnalyzer; use Psalm\Internal\Analyzer\StatementsAnalyzer; +use Psalm\Internal\BCHelper; use Psalm\Internal\FileManipulation\FileManipulationBuffer; use Psalm\Internal\Type\TemplateResult; use Psalm\Issue\RiskyTruthyFalsyComparison; @@ -456,7 +457,9 @@ private static function handleExpression( return MatchAnalyzer::analyze($statements_analyzer, $stmt, $context); } - if ($stmt instanceof PhpParser\Node\Expr\Throw_ && $analysis_php_version_id >= 8_00_00) { + if ($stmt instanceof PhpParser\Node\Expr\Throw_ + && ($analysis_php_version_id >= 8_00_00 || !BCHelper::usePHPParserV4()) + ) { return ThrowAnalyzer::analyze($statements_analyzer, $stmt, $context); } diff --git a/src/Psalm/Internal/BCHelper.php b/src/Psalm/Internal/BCHelper.php new file mode 100644 index 00000000000..9cdd9df1673 --- /dev/null +++ b/src/Psalm/Internal/BCHelper.php @@ -0,0 +1,29 @@ +expr instanceof PhpParser\Node\Expr\Throw_; + } +}