Skip to content
Open
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
47 changes: 47 additions & 0 deletions src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -3956,6 +3956,16 @@ public function mergeWith(?self $otherScope): self

$mergedExpressionTypes = $this->mergeVariableHolders($ourExpressionTypes, $theirExpressionTypes);
$conditionalExpressions = $this->intersectConditionalExpressions($otherScope->conditionalExpressions);
$conditionalExpressions = $this->preserveVacuousConditionalExpressions(
$conditionalExpressions,
$this->conditionalExpressions,
$theirExpressionTypes,
);
$conditionalExpressions = $this->preserveVacuousConditionalExpressions(
$conditionalExpressions,
$otherScope->conditionalExpressions,
$ourExpressionTypes,
);
$conditionalExpressions = $this->createConditionalExpressions(
$conditionalExpressions,
$ourExpressionTypes,
Expand Down Expand Up @@ -4055,6 +4065,43 @@ private function intersectConditionalExpressions(array $otherConditionalExpressi
return $newConditionalExpressions;
}

/**
* @param array<string, ConditionalExpressionHolder[]> $currentConditionalExpressions
* @param array<string, ConditionalExpressionHolder[]> $sourceConditionalExpressions
* @param array<string, ExpressionTypeHolder> $otherExpressionTypes
* @return array<string, ConditionalExpressionHolder[]>
*/
private function preserveVacuousConditionalExpressions(
array $currentConditionalExpressions,
array $sourceConditionalExpressions,
array $otherExpressionTypes,
): array
{
foreach ($sourceConditionalExpressions as $exprString => $holders) {
foreach ($holders as $key => $holder) {
if (isset($currentConditionalExpressions[$exprString][$key])) {
continue;
}

foreach ($holder->getConditionExpressionTypeHolders() as $guardExprString => $guardTypeHolder) {
if (!array_key_exists($guardExprString, $otherExpressionTypes)) {
continue;
}

$otherType = $otherExpressionTypes[$guardExprString]->getType();
$guardType = $guardTypeHolder->getType();

if ($otherType->isSuperTypeOf($guardType)->no()) {
$currentConditionalExpressions[$exprString][$key] = $holder;
break;
}
}
}
}

return $currentConditionalExpressions;
}

/**
* @param array<string, ConditionalExpressionHolder[]> $newConditionalExpressions
* @param array<string, ConditionalExpressionHolder[]> $existingConditionalExpressions
Expand Down
2 changes: 1 addition & 1 deletion src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -3217,7 +3217,7 @@ function (MutatingScope $scope) use ($stmt, $expr, $nodeCallback, $context, $sto
$this->callNodeCallback($nodeCallback, new InvalidateExprNode($normalizedExpr->var), $scope, $storage);
$scope = $scope->invalidateExpression($normalizedExpr->var, true, $methodReflection->getDeclaringClass());
}
if ($parametersAcceptor !== null && !$methodReflection->isStatic()) {
if (!$methodReflection->isStatic()) {
$selfOutType = $methodReflection->getSelfOutType();
if ($selfOutType !== null) {
$scope = $scope->assignExpression(
Expand Down
5 changes: 3 additions & 2 deletions src/Reflection/InitializerExprTypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -2210,13 +2210,14 @@ private function integerRangeMath(Type $range, BinaryOp $node, Type $operand): T
$min = null;
}

if ($operand->getMax() === null) {
$operandMax = $operand->getMax();
if ($operandMax === null) {
$min = null;
$max = null;
} elseif ($rangeMax !== null) {
if ($rangeMin !== null && $operand->getMin() === null) {
/** @var int|float $min */
$min = $rangeMin - $operand->getMax();
$min = $rangeMin - $operandMax;
$max = null;
} elseif ($operand->getMin() !== null) {
/** @var int|float $max */
Expand Down
27 changes: 3 additions & 24 deletions tests/PHPStan/Rules/Variables/DefinedVariableRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -911,12 +911,7 @@ public function testBug4173(): void
$this->polluteScopeWithLoopInitialAssignments = false;
$this->checkMaybeUndefinedVariables = true;
$this->polluteScopeWithAlwaysIterableForeach = true;
$this->analyse([__DIR__ . '/data/bug-4173.php'], [
[
'Variable $value might not be defined.', // could be fixed
30,
],
]);
$this->analyse([__DIR__ . '/data/bug-4173.php'], []);
}

public function testBug5805(): void
Expand Down Expand Up @@ -1119,29 +1114,13 @@ public function testDynamicAccess(): void
18,
],
[
'Variable $foo might not be defined.',
36,
],
[
'Variable $foo might not be defined.',
37,
],
[
'Variable $bar might not be defined.',
'Undefined variable: $bar',
38,
],
[
'Variable $bar might not be defined.',
40,
],
[
'Variable $foo might not be defined.',
'Undefined variable: $foo',
41,
],
[
'Variable $bar might not be defined.',
42,
],
[
'Undefined variable: $buz',
44,
Expand Down
Loading