Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
13 changes: 10 additions & 3 deletions src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -4386,6 +4386,8 @@ function (MutatingScope $scope) use ($stmt, $expr, $nodeCallback, $context, $sto
$filteringExprs = [];
$armCondScope = $matchScope;
$condNodes = [];
$armCondResultScope = $matchScope;
$bodyScope = null;
foreach ($arm->conds as $j => $armCond) {
if (isset($armCondsToSkip[$i][$j])) {
continue;
Expand All @@ -4401,12 +4403,17 @@ function (MutatingScope $scope) use ($stmt, $expr, $nodeCallback, $context, $sto
if ($armCondType->isTrue()->yes()) {
$hasAlwaysTrueCond = true;
}
$armCondScope = $armCondResult->getScope()->filterByFalseyValue($armCondExpr);
$armCondScope = $armCondResultScope->filterByFalseyValue($armCondExpr);
if ($bodyScope === null) {
$bodyScope = $armCondResultScope->filterByTruthyValue($armCondExpr);
} else {
$bodyScope = $bodyScope->mergeWith($armCondResultScope->filterByTruthyValue($armCondExpr));
}
Comment on lines +4406 to +4411
Copy link
Contributor

@staabm staabm Mar 5, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seeing this I feared this would slow down match/match-arm analysis.

running vendor/bin/phpunit tests/PHPStan/Analyser/AnalyserIntegrationTest.php in contrast reveals though, that it now runs in 20-21 seconds while it took 21-22 seconds before -> so we actually got a bit faster :)

$filteringExprs[] = $armCond;
}

$filteringExpr = $this->getFilteringExprForMatchArm($expr, $filteringExprs);
$bodyScope = $matchScope->filterByTruthyValue($filteringExpr);
$bodyScope ??= $matchScope->filterByTruthyValue($filteringExpr);
$matchArmBody = new MatchExpressionArmBody($bodyScope, $arm->body);
$armNodes[$i] = new MatchExpressionArm($matchArmBody, $condNodes, $arm->getStartLine());

Expand All @@ -4423,7 +4430,7 @@ function (MutatingScope $scope) use ($stmt, $expr, $nodeCallback, $context, $sto
$hasYield = $hasYield || $armResult->hasYield();
$throwPoints = array_merge($throwPoints, $armResult->getThrowPoints());
$impurePoints = array_merge($impurePoints, $armResult->getImpurePoints());
$matchScope = $matchScope->filterByFalseyValue($filteringExpr);
$matchScope = $armCondScope->filterByFalseyValue($filteringExpr);
}

if (!$hasDefaultCond && !$hasAlwaysTrueCond && $condType->isBoolean()->yes() && $condType->isConstantScalarValue()->yes()) {
Expand Down
20 changes: 20 additions & 0 deletions tests/PHPStan/Rules/Variables/DefinedVariableRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1235,6 +1235,26 @@ public function testBug10909(): void
$this->analyse([__DIR__ . '/data/bug-10909.php'], []);
}

#[RequiresPhp('>= 8.0')]
public function testBug13981(): void
{
$this->cliArgumentsVariablesRegistered = true;
$this->polluteScopeWithLoopInitialAssignments = true;
$this->checkMaybeUndefinedVariables = true;
$this->polluteScopeWithAlwaysIterableForeach = true;

$this->analyse([__DIR__ . '/data/bug-13981.php'], [
[
'Undefined variable: $baseDir',
34,
],
[
'Variable $baseDir might not be defined.',
46,
],
]);
}

#[RequiresPhp('>= 8.0')]
public function testBug7705(): void
{
Expand Down
61 changes: 61 additions & 0 deletions tests/PHPStan/Rules/Variables/data/bug-13981.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php // lint >= 8.0

declare(strict_types = 1);

namespace Bug13981;

function foo(): string
{
$path = match (true) {
is_dir($baseDir = dirname(__DIR__).'/lang') => $baseDir,
default => '/translations',
};

return $path;
}

function foo2(): string
{
if (rand(0, 1)) {
$baseDir = '';
}

$path = match (true) {
is_dir($baseDir = dirname(__DIR__).'/lang') => $baseDir,
default => '/translations',
};

return $path;
}

function foo3(): string
{
$path = match (true) {
is_dir(dirname(__DIR__).'/lang2') => $baseDir,
is_dir($baseDir = dirname(__DIR__).'/lang') => $baseDir,
default => '/translations',
};

return $path;
}

function foo4(): string
{
$path = match (true) {
is_dir(dirname(__DIR__).'/lang2'),
is_dir($baseDir = dirname(__DIR__).'/lang') => $baseDir,
default => '/translations',
};

return $path;
}

function foo5(): string
{
$path = match (true) {
is_dir($baseDir = dirname(__DIR__).'/lang') => '$baseDir',
default => $baseDir,
};

return $path;
}
Loading