Skip to content

Commit 56db2a0

Browse files
phpstan-botVincentLangletclaude
authored
Resolve the base of $prop[] = ... to its readable type instead of its writable type (#5996)
Co-authored-by: VincentLanglet <9052536+VincentLanglet@users.noreply.github.com> Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
1 parent eeb706a commit 56db2a0

10 files changed

Lines changed: 110 additions & 7 deletions

‎src/Analyser/ExprHandler/AssignHandler.php‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -558,8 +558,11 @@ public function processAssignVar(
558558
}
559559

560560
// 1. eval root expr
561+
// The root is read to obtain the container that receives the offset write, so a
562+
// property root must resolve to its readable type (not its writable one) even
563+
// though it sits on the left-hand side of the assignment.
561564
if ($enterExpressionAssign) {
562-
$scope = $scope->enterExpressionAssign($var);
565+
$scope = $scope->enterExpressionAssign($var, false);
563566
}
564567
$result = $nodeScopeResolver->processExprNode($stmt, $var, $scope, $storage, $nodeCallback, $context->enterDeep());
565568
$hasYield = $result->hasYield();

‎src/Analyser/ExprHandler/PropertyFetchHandler.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ private function propertyFetchType(MutatingScope $scope, Type $fetchedOnType, st
144144
return null;
145145
}
146146

147-
if ($scope->isInExpressionAssign($propertyFetch)) {
147+
if ($scope->isInWriteExpressionAssign($propertyFetch)) {
148148
return $propertyReflection->getWritableType();
149149
}
150150

‎src/Analyser/ExprHandler/StaticPropertyFetchHandler.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ private function propertyFetchType(MutatingScope $scope, Type $fetchedOnType, st
154154
return null;
155155
}
156156

157-
if ($scope->isInExpressionAssign($propertyFetch)) {
157+
if ($scope->isInWriteExpressionAssign($propertyFetch)) {
158158
return $propertyReflection->getWritableType();
159159
}
160160

‎src/Analyser/InternalScopeFactory.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ interface InternalScopeFactory
1616
* @param array<string, ExpressionTypeHolder> $nativeExpressionTypes
1717
* @param array<string, ConditionalExpressionHolder[]> $conditionalExpressions
1818
* @param list<non-empty-string> $inClosureBindScopeClasses
19-
* @param array<string, true> $currentlyAssignedExpressions
19+
* @param array<string, bool> $currentlyAssignedExpressions
2020
* @param array<string, true> $currentlyAllowedUndefinedExpressions
2121
* @param list<array{FunctionReflection|MethodReflection|null, ParameterReflection|null}> $inFunctionCallsStack
2222
*/

‎src/Analyser/MutatingScope.php‎

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ class MutatingScope implements Scope, NodeCallbackInvoker, CollectedDataEmitter
170170
* @param array<string, ExpressionTypeHolder> $expressionTypes
171171
* @param array<string, ConditionalExpressionHolder[]> $conditionalExpressions
172172
* @param list<non-empty-string> $inClosureBindScopeClasses
173-
* @param array<string, true> $currentlyAssignedExpressions
173+
* @param array<string, bool> $currentlyAssignedExpressions true when the expression is a plain write target (its writable type applies), false when it is read-modified in place (e.g. the base of `$prop[] = ...`), where its readable type applies
174174
* @param array<string, true> $currentlyAllowedUndefinedExpressions
175175
* @param array<string, ExpressionTypeHolder> $nativeExpressionTypes
176176
* @param list<array{MethodReflection|FunctionReflection|null, ParameterReflection|null}> $inFunctionCallsStack
@@ -2554,11 +2554,11 @@ public function enterCatchType(Type $catchType, ?string $variableName): self
25542554
);
25552555
}
25562556

2557-
public function enterExpressionAssign(Expr $expr): self
2557+
public function enterExpressionAssign(Expr $expr, bool $isPlainWrite = true): self
25582558
{
25592559
$exprString = $this->getNodeKey($expr);
25602560
$currentlyAssignedExpressions = $this->currentlyAssignedExpressions;
2561-
$currentlyAssignedExpressions[$exprString] = true;
2561+
$currentlyAssignedExpressions[$exprString] = $isPlainWrite;
25622562

25632563
$scope = $this->scopeFactory->create(
25642564
$this->context,
@@ -2627,6 +2627,21 @@ public function isInExpressionAssign(Expr $expr): bool
26272627
return array_key_exists($exprString, $this->currentlyAssignedExpressions);
26282628
}
26292629

2630+
/**
2631+
* Whether the expression is a plain write target of an assignment, as opposed to being
2632+
* read-modified in place (e.g. the base of `$prop[] = ...`). Used to decide whether a
2633+
* property fetch resolves to its writable or readable type.
2634+
*/
2635+
public function isInWriteExpressionAssign(Expr $expr): bool
2636+
{
2637+
if (count($this->currentlyAssignedExpressions) === 0) {
2638+
return false;
2639+
}
2640+
2641+
$exprString = $this->getNodeKey($expr);
2642+
return ($this->currentlyAssignedExpressions[$exprString] ?? false) === true;
2643+
}
2644+
26302645
public function setAllowedUndefinedExpression(Expr $expr): self
26312646
{
26322647
if ($expr instanceof Expr\StaticPropertyFetch) {

‎tests/PHPStan/Rules/Arrays/NonexistentOffsetInArrayDimFetchRuleTest.php‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,11 @@ public function testBug3784(): void
359359
$this->analyse([__DIR__ . '/data/bug-3784.php'], []);
360360
}
361361

362+
public function testBug14054(): void
363+
{
364+
$this->analyse([__DIR__ . '/data/bug-14054.php'], []);
365+
}
366+
362367
public function testBug3700(): void
363368
{
364369
$this->analyse([__DIR__ . '/data/bug-3700.php'], []);

‎tests/PHPStan/Rules/Arrays/OffsetAccessAssignmentRuleTest.php‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,19 @@ public function testBug11572(): void
201201
]);
202202
}
203203

204+
public function testBug14054(): void
205+
{
206+
$this->checkUnionTypes = true;
207+
$this->analyse([__DIR__ . '/data/bug-14054-property.php'], []);
208+
}
209+
210+
#[RequiresPhp('>= 8.4.0')]
211+
public function testBug14054Hook(): void
212+
{
213+
$this->checkUnionTypes = true;
214+
$this->analyse([__DIR__ . '/data/bug-14054-hook.php'], []);
215+
}
216+
204217
public function testBug8648(): void
205218
{
206219
$this->checkUnionTypes = true;
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php // lint >= 8.4
2+
3+
declare(strict_types = 1);
4+
5+
namespace Bug14054Hook;
6+
7+
final class WithHook
8+
{
9+
10+
/** @var array<int, string> */
11+
public array $data = [] {
12+
set (array|string $value) {
13+
$this->data = (array) $value;
14+
}
15+
}
16+
17+
}
18+
19+
function testHook(WithHook $w): void
20+
{
21+
$w->data[] = 'x';
22+
$w->data['key'] = 'y';
23+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug14054Property;
4+
5+
/**
6+
* @property-read array<int, string> $items
7+
* @property-write array<int, string>|string $items
8+
*/
9+
final class Magic
10+
{
11+
12+
/**
13+
* @return mixed
14+
*/
15+
public function __get(string $name)
16+
{
17+
return [];
18+
}
19+
20+
/**
21+
* @param mixed $value
22+
*/
23+
public function __set(string $name, $value): void
24+
{
25+
}
26+
27+
}
28+
29+
function testMagic(Magic $m): void
30+
{
31+
$m->items[] = 'x';
32+
$m->items['key'] = 'y';
33+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug14054;
4+
5+
function doFoo(): void
6+
{
7+
$xml = new \SimpleXMLElement('<root/>');
8+
$xml->test[] = 'bla';
9+
$xml->test['key'] = 'bla';
10+
$xml->child->test[] = 'bla';
11+
}

0 commit comments

Comments
 (0)