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
6 changes: 6 additions & 0 deletions src/Analyser/ExprHandler/AssignHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,7 @@ public function processAssignVar(

if ($varType->isArray()->yes() || !(new ObjectType(ArrayAccess::class))->isSuperTypeOf($varType)->yes()) {
if ($var instanceof Variable && is_string($var->name)) {
$preservedDimFetchTypes = $scope->getDynamicArrayDimFetchExpressionTypes($var->name);
$nodeScopeResolver->callNodeCallback($nodeCallback, new VariableAssignNode($var, new TypeExpr($valueToWrite)), $scopeBeforeAssignEval, $storage);
$scope = $scope->assignVariable($var->name, $valueToWrite, $nativeValueToWrite, TrinaryLogic::createYes());
} else {
Expand Down Expand Up @@ -505,6 +506,11 @@ public function processAssignVar(
$scope = $scope->assignExpression($expr, $type, $nativeType);
}

if (isset($preservedDimFetchTypes)) {
$scope = $scope->restoreExpressionTypes($preservedDimFetchTypes);
unset($preservedDimFetchTypes);
}

$setVarType = $scope->getType($originalVar->var);
if (
!$setVarType instanceof ErrorType
Expand Down
62 changes: 62 additions & 0 deletions src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -2857,6 +2857,68 @@ public function assignInitializedProperty(Type $fetchedOnType, string $propertyN
return $this->assignExpression(new PropertyInitializationExpr($propertyName), new MixedType(), new MixedType());
}

/**
* @return list<array{string, ExpressionTypeHolder, ExpressionTypeHolder}>
*/
public function getDynamicArrayDimFetchExpressionTypes(string $variableName): array
{
$result = [];
foreach ($this->expressionTypes as $exprString => $exprTypeHolder) {
$expr = $exprTypeHolder->getExpr();
if (!$expr instanceof Expr\ArrayDimFetch) {
continue;
}
if (!$expr->var instanceof Variable || !is_string($expr->var->name) || $expr->var->name !== $variableName) {
continue;
}
if ($expr->dim === null || $expr->dim instanceof Scalar) {
continue;
}
$nativeHolder = $this->nativeExpressionTypes[$exprString] ?? $exprTypeHolder;
$result[] = [$exprString, $exprTypeHolder, $nativeHolder];
}
return $result;
}

/**
* @param list<array{string, ExpressionTypeHolder, ExpressionTypeHolder}> $holders
*/
public function restoreExpressionTypes(array $holders): self
{
$expressionTypes = $this->expressionTypes;
$nativeExpressionTypes = $this->nativeExpressionTypes;
$changed = false;
foreach ($holders as [$exprString, $holder, $nativeHolder]) {
if (isset($expressionTypes[$exprString])) {
continue;
}
$expressionTypes[$exprString] = $holder;
$nativeExpressionTypes[$exprString] = $nativeHolder;
$changed = true;
}
if (!$changed) {
return $this;
}
return $this->scopeFactory->create(
$this->context,
$this->isDeclareStrictTypes(),
$this->getFunction(),
$this->getNamespace(),
$expressionTypes,
$nativeExpressionTypes,
$this->conditionalExpressions,
$this->inClosureBindScopeClasses,
$this->anonymousFunctionReflection,
$this->inFirstLevelStatement,
$this->currentlyAssignedExpressions,
$this->currentlyAllowedUndefinedExpressions,
$this->inFunctionCallsStack,
$this->afterExtractCall,
$this->parentScope,
$this->nativeTypesPromoted,
);
}

public function invalidateExpression(Expr $expressionToInvalidate, bool $requireMoreCharacters = false, ?ClassReflection $invalidatingClass = null): self
{
$expressionTypes = $this->expressionTypes;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1263,4 +1263,12 @@ public function testBug14308(): void
$this->analyse([__DIR__ . '/data/bug-14308.php'], []);
}

#[RequiresPhp('>= 8.1')]
public function testBug14347(): void
{
$this->reportPossiblyNonexistentConstantArrayOffset = true;

$this->analyse([__DIR__ . '/data/bug-14347.php'], []);
}

}
38 changes: 38 additions & 0 deletions tests/PHPStan/Rules/Arrays/data/bug-14347.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php // lint >= 8.1

declare(strict_types = 1);

namespace Bug14347;

enum Suit: string {
case Clubs = 'c';
case Diamonds = 'd';
case Hearts = 'h';
case Spades = 's';
}
/**
* @param array<array-key, Suit> $cards
* @return array<non-empty-string, non-negative-int>
*/
function countCards(array $cards): array {
$cardCounts = ['all' => 0];
foreach ($cards as $card) {
$cardCounts['all']++;
$cardCounts[$card->value] ??= 0;
$cardCounts[$card->value]++;
}
return $cardCounts;
}
/**
* @param array<array-key, Suit> $cards
* @return array<non-empty-string, non-negative-int>
*/
function countCardsBroken(array $cards): array {
$cardCounts = ['all' => 0];
foreach ($cards as $card) {
$cardCounts[$card->value] ??= 0;
$cardCounts['all']++;
$cardCounts[$card->value]++;
}
return $cardCounts;
}
Loading