Skip to content

Commit

Permalink
Add BCHelper
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentLanglet committed Jul 25, 2024
1 parent 957854c commit b3ab2a5
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
7 changes: 4 additions & 3 deletions src/Psalm/Internal/Analyzer/ScopeAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Psalm\Internal\Analyzer;

use PhpParser;
use Psalm\Internal\BCHelper;
use Psalm\Internal\Provider\NodeDataProvider;
use Psalm\NodeTypeProvider;

Expand Down Expand Up @@ -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_) {
Expand Down Expand Up @@ -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_)
) {
Expand Down Expand Up @@ -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;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

Expand Down
29 changes: 29 additions & 0 deletions src/Psalm/Internal/BCHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Psalm\Internal;

use PhpParser;
use PhpParser\Node;

use function class_exists;

/**
* @internal
*/
final class BCHelper
{
public static function usePHPParserV4(): bool
{
return class_exists('\PhpParser\Node\Stmt\Throw');
}

public static function isThrow(Node $stmt): bool
{
if (self::usePHPParserV4()) {
return $stmt instanceof PhpParser\Node\Stmt\Throw_;
}

return $stmt instanceof PhpParser\Node\Stmt\Expression
&& $stmt->expr instanceof PhpParser\Node\Expr\Throw_;
}
}

0 comments on commit b3ab2a5

Please sign in to comment.