Skip to content
Closed
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
26 changes: 26 additions & 0 deletions src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@
use UnhandledMatchError;
use function array_fill_keys;
use function array_filter;
use function array_flip;
use function array_key_exists;
use function array_key_last;
use function array_keys;
Expand Down Expand Up @@ -4355,6 +4356,7 @@ function (MutatingScope $scope) use ($stmt, $expr, $nodeCallback, $context, $sto
$filteringExprs = [];
$armCondScope = $matchScope;
$condNodes = [];
$armCondResultScope = $matchScope;
foreach ($arm->conds as $j => $armCond) {
if (isset($armCondsToSkip[$i][$j])) {
continue;
Expand All @@ -4376,6 +4378,30 @@ function (MutatingScope $scope) use ($stmt, $expr, $nodeCallback, $context, $sto

$filteringExpr = $this->getFilteringExprForMatchArm($expr, $filteringExprs);
$bodyScope = $matchScope->filterByTruthyValue($filteringExpr);
$condResultScope = $armCondResultScope;
$matchScopeKnownVars = array_flip(array_merge($matchScope->getDefinedVariables(), $matchScope->getMaybeDefinedVariables()));
foreach ($condResultScope->getDefinedVariables() as $varName) {
if (isset($matchScopeKnownVars[$varName])) {
continue;
}
$bodyScope = $bodyScope->assignVariable(
$varName,
$condResultScope->getVariableType($varName),
$condResultScope->getNativeType(new Variable($varName)),
$condResultScope->hasVariableType($varName),
);
}
foreach ($condResultScope->getMaybeDefinedVariables() as $varName) {
if (isset($matchScopeKnownVars[$varName])) {
continue;
}
$bodyScope = $bodyScope->assignVariable(
$varName,
$condResultScope->getVariableType($varName),
$condResultScope->getNativeType(new Variable($varName)),
$condResultScope->hasVariableType($varName),
);
}
$matchArmBody = new MatchExpressionArmBody($bodyScope, $arm->body);
$armNodes[$i] = new MatchExpressionArm($matchArmBody, $condNodes, $arm->getStartLine());

Expand Down
11 changes: 11 additions & 0 deletions tests/PHPStan/Rules/Variables/DefinedVariableRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1257,4 +1257,15 @@ public function testBug12944(): void
$this->analyse([__DIR__ . '/data/bug-12944.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'], []);
}

}
10 changes: 10 additions & 0 deletions tests/PHPStan/Rules/Variables/data/bug-13981.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php // lint >= 8.0

declare(strict_types = 1);

namespace Bug13981;

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