Skip to content

Commit

Permalink
Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Jan 8, 2022
1 parent 8d11f35 commit 652a17f
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 26 deletions.
56 changes: 30 additions & 26 deletions src/Analyser/NodeScopeResolver.php
Expand Up @@ -3159,39 +3159,39 @@ private function processAssignVar(
$scope = $result->getScope();

$varType = $scope->getType($var);
if (!(new ObjectType(ArrayAccess::class))->isSuperTypeOf($varType)->yes()) {
$getValueToWrite = static function (Type $initialType) use ($offsetTypes, $valueToWrite): Type {
// 4. compose types
if ($initialType instanceof ErrorType) {
$initialType = new ConstantArrayType([], []);
}
$offsetValueType = $initialType;
$offsetValueTypeStack = [$offsetValueType];
foreach (array_slice($offsetTypes, 0, -1) as $offsetType) {
if ($offsetType === null) {
$offsetValueType = new ConstantArrayType([], []);
$getValueToWrite = static function (Type $initialType) use ($offsetTypes, $valueToWrite): Type {
// 4. compose types
if ($initialType instanceof ErrorType) {
$initialType = new ConstantArrayType([], []);
}
$offsetValueType = $initialType;
$offsetValueTypeStack = [$offsetValueType];
foreach (array_slice($offsetTypes, 0, -1) as $offsetType) {
if ($offsetType === null) {
$offsetValueType = new ConstantArrayType([], []);

} else {
$offsetValueType = $offsetValueType->getOffsetValueType($offsetType);
if ($offsetValueType instanceof ErrorType) {
$offsetValueType = new ConstantArrayType([], []);
}
} else {
$offsetValueType = $offsetValueType->getOffsetValueType($offsetType);
if ($offsetValueType instanceof ErrorType) {
$offsetValueType = new ConstantArrayType([], []);
}

$offsetValueTypeStack[] = $offsetValueType;
}

foreach (array_reverse($offsetTypes) as $i => $offsetType) {
/** @var Type $offsetValueType */
$offsetValueType = array_pop($offsetValueTypeStack);
$valueToWrite = $offsetValueType->setOffsetValueType($offsetType, $valueToWrite, $i === 0);
}
$offsetValueTypeStack[] = $offsetValueType;
}

foreach (array_reverse($offsetTypes) as $i => $offsetType) {
/** @var Type $offsetValueType */
$offsetValueType = array_pop($offsetValueTypeStack);
$valueToWrite = $offsetValueType->setOffsetValueType($offsetType, $valueToWrite, $i === 0);
}

return $valueToWrite;
};
return $valueToWrite;
};

$valueToWrite = $getValueToWrite($varType);
$valueToWrite = $getValueToWrite($varType);

if (!(new ObjectType(ArrayAccess::class))->isSuperTypeOf($varType)->yes()) {
if ($var instanceof Variable && is_string($var->name)) {
$scope = $scope->assignVariable($var->name, $valueToWrite);
} else {
Expand All @@ -3213,6 +3213,10 @@ private function processAssignVar(
);
}
}
} else {
if ($var instanceof PropertyFetch || $var instanceof StaticPropertyFetch) {
$nodeCallback(new PropertyAssignNode($var, $getValueToWrite(new ErrorType())), $scope);
}
}
} elseif ($var instanceof PropertyFetch) {
$objectResult = $this->processExprNode($var->var, $scope, $nodeCallback, $context);
Expand Down
17 changes: 17 additions & 0 deletions tests/PHPStan/Rules/DeadCode/data/unused-private-property.php
Expand Up @@ -213,3 +213,20 @@ public function getFoo()
}

}

class WriteToCollection
{

/** @var \ArrayAccess<int, string> */
private $collection1;

/** @var \ArrayAccess<int, string>&\Countable */
private $collection2;

public function foo(): void
{
$this->collection1[] = 1;
$this->collection2[] = 2;
}

}

0 comments on commit 652a17f

Please sign in to comment.