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
12 changes: 12 additions & 0 deletions src/Analyser/EnsuredNonNullabilityResultExpression.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public function __construct(
private Type $originalType,
private Type $originalNativeType,
private TrinaryLogic $certainty,
private bool $existedInOriginalScope = true,
private bool $sideEffectSave = false,
)
{
}
Expand All @@ -38,4 +40,14 @@ public function getCertainty(): TrinaryLogic
return $this->certainty;
}

public function existedInOriginalScope(): bool
{
return $this->existedInOriginalScope;
}

public function isSideEffectSave(): bool
{
return $this->sideEffectSave;
}

}
28 changes: 28 additions & 0 deletions src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -3547,6 +3547,34 @@ public function specifyExpressionType(Expr $expr, Type $type, Type $nativeType,
return $scope;
}

public function unsetExpressionType(Expr $expr): self
{
$exprString = $this->getNodeKey($expr);
$expressionTypes = $this->expressionTypes;
$nativeTypes = $this->nativeExpressionTypes;
unset($expressionTypes[$exprString]);
unset($nativeTypes[$exprString]);

return $this->scopeFactory->create(
$this->context,
$this->isDeclareStrictTypes(),
$this->getFunction(),
$this->getNamespace(),
$expressionTypes,
$nativeTypes,
$this->conditionalExpressions,
$this->inClosureBindScopeClasses,
$this->anonymousFunctionReflection,
$this->inFirstLevelStatement,
$this->currentlyAssignedExpressions,
$this->currentlyAllowedUndefinedExpressions,
$this->inFunctionCallsStack,
$this->afterExtractCall,
$this->parentScope,
$this->nativeTypesPromoted,
);
}

public function assignExpression(Expr $expr, Type $type, Type $nativeType): self
{
$scope = $this;
Expand Down
23 changes: 19 additions & 4 deletions src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -2473,7 +2473,8 @@ private function ensureShallowNonNullability(MutatingScope $scope, Scope $origin
// keep certainty
$certainty = TrinaryLogic::createYes();
$hasExpressionType = $originalScope->hasExpressionType($exprToSpecify);
if (!$hasExpressionType->no()) {
$existedInOriginalScope = !$hasExpressionType->no();
if ($existedInOriginalScope) {
$certainty = $hasExpressionType;
}

Expand All @@ -2484,7 +2485,7 @@ private function ensureShallowNonNullability(MutatingScope $scope, Scope $origin
$originalNativeType = $originalScope->getNativeType($exprToSpecify);

return new EnsuredNonNullabilityResult($scope, [
new EnsuredNonNullabilityResultExpression($exprToSpecify, $originalExprType, $originalNativeType, $certainty),
new EnsuredNonNullabilityResultExpression($exprToSpecify, $originalExprType, $originalNativeType, $certainty, $existedInOriginalScope, true),
]);
}
return new EnsuredNonNullabilityResult($scope, []);
Expand All @@ -2493,7 +2494,7 @@ private function ensureShallowNonNullability(MutatingScope $scope, Scope $origin
$nativeType = $scope->getNativeType($exprToSpecify);

$specifiedExpressions = [
new EnsuredNonNullabilityResultExpression($exprToSpecify, $exprType, $nativeType, $certainty),
new EnsuredNonNullabilityResultExpression($exprToSpecify, $exprType, $nativeType, $certainty, $existedInOriginalScope),
];

// When narrowing an ArrayDimFetch, specifyExpressionType also recursively
Expand All @@ -2503,14 +2504,17 @@ private function ensureShallowNonNullability(MutatingScope $scope, Scope $origin
$parentExpr = $exprToSpecify->var;
$parentCertainty = TrinaryLogic::createYes();
$hasParentExpressionType = $originalScope->hasExpressionType($parentExpr);
if (!$hasParentExpressionType->no()) {
$parentExistedInOriginalScope = !$hasParentExpressionType->no();
if ($parentExistedInOriginalScope) {
$parentCertainty = $hasParentExpressionType;
}
array_unshift($specifiedExpressions, new EnsuredNonNullabilityResultExpression(
$parentExpr,
$scope->getType($parentExpr),
$scope->getNativeType($parentExpr),
$parentCertainty,
$parentExistedInOriginalScope,
true,
));
}

Expand Down Expand Up @@ -2556,6 +2560,17 @@ private function revertNonNullability(MutatingScope $scope, array $specifiedExpr
);
}

// specifyExpressionType recursively narrows parent array types as a side effect,
// which can introduce expression type holders that didn't exist in the original scope.
// Clean up side-effect saves that shouldn't have expression types.
foreach ($specifiedExpressions as $specifiedExpressionResult) {
if (!$specifiedExpressionResult->isSideEffectSave() || $specifiedExpressionResult->existedInOriginalScope()) {
continue;
}

$scope = $scope->unsetExpressionType($specifiedExpressionResult->getExpression());
}

return $scope;
}

Expand Down
5 changes: 5 additions & 0 deletions tests/PHPStan/Rules/Variables/NullCoalesceRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -362,4 +362,9 @@ public function testBug14213(): void
]);
}

public function testBug13921(): void
{
$this->analyse([__DIR__ . '/data/bug-13921.php'], []);
}

}
9 changes: 9 additions & 0 deletions tests/PHPStan/Rules/Variables/data/bug-13921.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php declare(strict_types = 1);

namespace Bug13921;

/** @param list<array<?string>> $x */
function foo(array $x): void {
var_dump($x[0]['bar'] ?? null);
var_dump($x[0] ?? null);
}
Loading