Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Added ExpressionResultTest
  • Loading branch information
staabm committed Jul 21, 2025
commit 4e0c9c0296cd5791954a0ac479f90ad30c760ee1
1 change: 1 addition & 0 deletions src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -3201,6 +3201,7 @@ static function (): void {
$impurePoints = [
new ImpurePoint($scope, $expr, $identifier, $identifier, true),
];
$isAlwaysTerminating = true;
if ($expr->expr !== null) {
$result = $this->processExprNode($stmt, $expr->expr, $scope, $nodeCallback, $context->enterDeep());
$hasYield = $result->hasYield();
Expand Down
77 changes: 77 additions & 0 deletions tests/PHPStan/Analyser/ExpressionResultTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php declare(strict_types = 1);

namespace PHPStan\Analyser;

use PhpParser\Node\Stmt;
use PHPStan\Parser\Parser;
use PHPStan\ShouldNotHappenException;
use PHPStan\Testing\PHPStanTestCase;
use PHPStan\TrinaryLogic;
use PHPStan\Type\ArrayType;
use PHPStan\Type\IntegerType;
use PHPStan\Type\MixedType;
use PHPStan\Type\StringType;
use PHPUnit\Framework\Attributes\DataProvider;
use function count;
use function get_class;
use function sprintf;

class ExpressionResultTest extends PHPStanTestCase
{

public static function dataIsAlwaysTerminating(): array
{
return [
[
'sprintf("hello %s", "abc");',
false,
],
[
'sprintf("hello %s", exit());',
true,
],
];
}

#[DataProvider('dataIsAlwaysTerminating')]
public function testIsAlwaysTerminating(
string $code,
bool $expectedIsAlwaysTerminating,
): void
{
/** @var Parser $parser */
$parser = self::getContainer()->getService('currentPhpVersionRichParser');

/** @var Stmt[] $stmts */
$stmts = $parser->parseString(sprintf('<?php %s', $code));
if (count($stmts) !== 1) {
throw new ShouldNotHappenException('Expecting code which evaluates to a single statement, got: ' . count($stmts));
}
if (!$stmts[0] instanceof Stmt\Expression) {
throw new ShouldNotHappenException('Expecting code contains a single statement expression, got: ' . get_class($stmts[0]));
}
$stmt = $stmts[0];
$expr = $stmt->expr;

/** @var NodeScopeResolver $nodeScopeResolver */
$nodeScopeResolver = self::getContainer()->getByType(NodeScopeResolver::class);
/** @var ScopeFactory $scopeFactory */
$scopeFactory = self::getContainer()->getByType(ScopeFactory::class);
$scope = $scopeFactory->create(ScopeContext::create('test.php'))
->assignVariable('string', new StringType(), new StringType(), TrinaryLogic::createYes())
->assignVariable('x', new IntegerType(), new IntegerType(), TrinaryLogic::createYes())
->assignVariable('cond', new MixedType(), new MixedType(), TrinaryLogic::createYes())
->assignVariable('arr', new ArrayType(new MixedType(), new MixedType()), new ArrayType(new MixedType(), new MixedType()), TrinaryLogic::createYes());

$result = $nodeScopeResolver->processExprNode(
$stmt,
$expr,
$scope,
static function (): void {
},
ExpressionContext::createTopLevel(),
);
$this->assertSame($expectedIsAlwaysTerminating, $result->isAlwaysTerminating());
}

}