Skip to content

Commit 6dc37db

Browse files
committed
Count a tracked chain link as evidence of an existing offset only for an unmodified foreach key
A nested write such as $convert[$outerKey][rand(0, 100)] = ... treated the offset as already present whenever the tracked $convert[$outerKey] was a list and the offset fit its key type, so the write kept the list. The shortcut is meant for offsets that are the key of a foreach over the written structure; it now requires the offset to be a foreach key that has not been reassigned. Amp-Thread-ID: https://ampcode.com/threads/T-01a17eac-1199-74a6-b3f7-dfbd8a084465
1 parent 671e1ed commit 6dc37db

4 files changed

Lines changed: 123 additions & 40 deletions

File tree

‎src/Analyser/ExprHandler/AssignHandler.php‎

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
use PHPStan\Node\Expr\IntertwinedVariableByReferenceWithExpr;
6262
use PHPStan\Node\Expr\NativeTypeExpr;
6363
use PHPStan\Node\Expr\OffsetWriteBaseExpr;
64+
use PHPStan\Node\Expr\OriginalForeachKeyExpr;
6465
use PHPStan\Node\Expr\SetExistingOffsetValueTypeExpr;
6566
use PHPStan\Node\Expr\SetOffsetValueTypeExpr;
6667
use PHPStan\Node\Expr\TypeExpr;
@@ -2958,7 +2959,7 @@ private function produceArrayDimFetchAssignValueToWrite(NodeScopeResolver $nodeS
29582959
&& !$offsetValueType->hasOffsetValueType($offsetType)->no()
29592960
&& (
29602961
$trackedDimFetch === $writtenDimFetch
2961-
|| $this->trackedLinkImpliesOffset($offsetValueType, $offsetType)
2962+
|| $this->trackedLinkImpliesOffset($offsetValueType, $offsetType, $writtenDimFetch, $scope)
29622963
)
29632964
) {
29642965
$hasOffsetType = null;
@@ -3140,16 +3141,27 @@ private function denotesSameOffsetEveryTime(Expr $expr): bool
31403141
/**
31413142
* A link of the chain other than the written one being tracked does not
31423143
* prove the written offset is there. It is the usual evidence when the
3143-
* offset comes from the written structure itself, like in
3144-
* `foreach ($rows as $k => $v) { $matrix[$i][$k] = ...; }`, so it only
3145-
* counts as long as the offset stays within the keys the container can have.
3144+
* offset is the key of a foreach over the written structure, like in
3145+
* `foreach ($rows as $k => $row) { foreach ($row as $c => $v) { $rows[$k][$c] = ...; } }`,
3146+
* so it only counts for an unmodified foreach key that stays within the keys
3147+
* the container can have. Any other offset in that range, like `rand(0, 100)`
3148+
* on a list, may be a new key.
31463149
*/
3147-
private function trackedLinkImpliesOffset(Type $offsetValueType, Type $offsetType): bool
3150+
private function trackedLinkImpliesOffset(Type $offsetValueType, Type $offsetType, ArrayDimFetch $writtenDimFetch, MutatingScope $scope): bool
31483151
{
31493152
if (!$offsetValueType->isArray()->yes()) {
31503153
return true;
31513154
}
31523155

3156+
$dim = $writtenDimFetch->dim;
3157+
if (
3158+
!$dim instanceof Variable
3159+
|| !is_string($dim->name)
3160+
|| !$scope->hasExpressionType(new OriginalForeachKeyExpr($dim->name))->yes()
3161+
) {
3162+
return false;
3163+
}
3164+
31533165
return $offsetValueType->getIterableKeyType()->isSuperTypeOf($offsetType)->yes();
31543166
}
31553167

‎tests/PHPStan/Rules/Variables/data/bug-14124.php‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,60 @@ function example3b(array &$convert): void
3131
}
3232
assertType('array<string, list<string>>', $convert);
3333
}
34+
35+
/**
36+
* @param array<string, list<string>> $convert
37+
* @param-out array<string, array<int, string>> $convert
38+
*/
39+
function example3c(array &$convert): void
40+
{
41+
foreach ($convert as $outerKey => $inner) {
42+
foreach ($inner as $val) {
43+
$convert[$outerKey][rand(0, 100)] = strtoupper($val);
44+
}
45+
}
46+
assertType('array<string, array<int<0, max>, string>>', $convert);
47+
}
48+
49+
/**
50+
* @param array<string, list<string>> $matrix
51+
*/
52+
function randomKeyAfterIsset(array $matrix, string $row): void
53+
{
54+
if (!isset($matrix[$row])) {
55+
return;
56+
}
57+
58+
$matrix[$row][rand(0, 100)] = 'x';
59+
assertType('non-empty-array<int<0, max>, string>', $matrix[$row]);
60+
}
61+
62+
/**
63+
* @param array<string, list<string>> $rows
64+
*/
65+
function keyReassignedInOneBranch(array $rows): void
66+
{
67+
foreach ($rows as $k => $row) {
68+
foreach ($row as $c => $v) {
69+
if (rand(0, 1) === 1) {
70+
$c = rand(0, 100);
71+
}
72+
$rows[$k][$c] = $v;
73+
}
74+
}
75+
assertType('array<string, array<int<0, max>, string>>', $rows);
76+
}
77+
78+
/**
79+
* @param array<string, string> $requirements
80+
*/
81+
function literalKeyAfterForeachKeyWrites(array $requirements): void
82+
{
83+
$schema = [];
84+
$schema['parameters'] = [];
85+
foreach ($requirements as $name => $requirement) {
86+
$schema['parameters'][$name] = ['in' => 'path', 'schema' => $requirement];
87+
}
88+
$schema['parameters']['Accept-Language'] = ['examples' => [1]];
89+
assertType("non-empty-array<string, array{examples: array{1}}|array{in: 'path', schema: string}>&hasOffsetValue('Accept-Language', array{examples: array{1}})", $schema['parameters']);
90+
}

‎turbo-ext/src/AssignHandler.cpp‎

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3681,7 +3681,7 @@ class AssignHandler
36813681
if (Z_TYPE_P(trackedDimFetch) == IS_OBJECT && Z_TYPE_P(writtenDimFetch) == IS_OBJECT && Z_OBJ_P(trackedDimFetch) == Z_OBJ_P(writtenDimFetch)) {
36823682
existing = true;
36833683
} else {
3684-
AH_OKB(trackedLinkImpliesOffset(offsetValueType.raw(), offsetType, existing));
3684+
AH_OKB(trackedLinkImpliesOffset(offsetValueType.raw(), offsetType, writtenDimFetch, scope, existing));
36853685
}
36863686
}
36873687
}
@@ -3937,13 +3937,12 @@ class AssignHandler
39373937
return Z_TYPE_P(value) == IS_LONG && Z_LVAL_P(value) == 1;
39383938
}
39393939

3940-
/* private (twin 2819): a link of the chain other than the written one
3941-
* being tracked does not prove the written offset is there. It is the
3942-
* usual evidence when the offset comes from the written structure itself,
3943-
* like in `foreach ($rows as $k => $v) { $matrix[$i][$k] = ...; }`, so it
3944-
* only counts as long as the offset stays within the keys the container
3945-
* can have. false = pending exception */
3946-
[[nodiscard]] static bool trackedLinkImpliesOffset(zval *offsetValueType, zval *offsetType, bool &out)
3940+
/* (twin) a link of the chain other than the written one being tracked
3941+
* does not prove the written offset is there. It is the usual evidence
3942+
* when the offset is the key of a foreach over the written structure, so
3943+
* it only counts for an unmodified foreach key that stays within the keys
3944+
* the container can have. false = pending exception */
3945+
[[nodiscard]] static bool trackedLinkImpliesOffset(zval *offsetValueType, zval *offsetType, zval *writtenDimFetch, zval *scope, bool &out)
39473946
{
39483947
zend_long isArray = ahTypeOpTri(offsetValueType, PT_OP_IS_ARRAY, 0, NULL);
39493948
AH_OKB(isArray >= 0);
@@ -3952,6 +3951,18 @@ class AssignHandler
39523951
return true;
39533952
}
39543953

3954+
out = false;
3955+
zval *dim = AH_PROP(writtenDimFetch, dim);
3956+
if (!ahIs(dim, PT_CLASS_VARIABLE)) return true;
3957+
zend_string *dimName = ahVariableName(dim);
3958+
if (dimName == NULL) return true;
3959+
zval nameZv;
3960+
ZVAL_STR(&nameZv, dimName);
3961+
AH_VALB(originalKeyExpr, pt_type_new(PT_CLASS_ORIGINAL_FOREACH_KEY_EXPR, 1, &nameZv));
3962+
zend_long tracked = pt_mutating_scope_has_expression_type(Z_OBJ_P(scope), originalKeyExpr.raw());
3963+
AH_OKB(tracked >= 0);
3964+
if (tracked != PT_TRI_YES) return true;
3965+
39553966
AH_VALB(keyType, ahTypeOp(offsetValueType, PT_OP_GET_ITERABLE_KEY_TYPE, 0, NULL));
39563967
zend_long isSuper = ahTypeOpTri(keyType.raw(), PT_OP_IS_SUPER_TYPE_OF, 1, offsetType);
39573968
AH_OKB(isSuper >= 0);

‎turbo-ext/src/generated/AssignHandler.h‎

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -223,19 +223,20 @@ inline constexpr char strings[] =
223223
"offsetType\0" /* 3720 */
224224
"shouldKeepList\0" /* 3731 */
225225
"denotesSameOffsetEveryTime\0" /* 3746 */
226-
"trackedLinkImpliesOffset\0" /* 3773 */
227-
"a\0" /* 3798 */
228-
"b\0" /* 3800 */
229-
"isSameVariable\0" /* 3802 */
230-
"isSameOffset\0" /* 3817 */
231-
"getLiteralArrayKeyType\0" /* 3830 */
232-
"propertyFetch\0" /* 3853 */
233-
"PhpParser\\Node\\Expr\\PropertyFetch|PhpParser\\Node\\Expr\\StaticPropertyFetch\0" /* 3867 */
234-
"getOriginalPropertyType\0" /* 3941 */
235-
"array\0" /* 3965 */
236-
"hasArrayReference\0" /* 3971 */
237-
"rhs\0" /* 3989 */
238-
"redundant"; /* 3993 */
226+
"writtenDimFetch\0" /* 3773 */
227+
"trackedLinkImpliesOffset\0" /* 3789 */
228+
"a\0" /* 3814 */
229+
"b\0" /* 3816 */
230+
"isSameVariable\0" /* 3818 */
231+
"isSameOffset\0" /* 3833 */
232+
"getLiteralArrayKeyType\0" /* 3846 */
233+
"propertyFetch\0" /* 3869 */
234+
"PhpParser\\Node\\Expr\\PropertyFetch|PhpParser\\Node\\Expr\\StaticPropertyFetch\0" /* 3883 */
235+
"getOriginalPropertyType\0" /* 3957 */
236+
"array\0" /* 3981 */
237+
"hasArrayReference\0" /* 3987 */
238+
"rhs\0" /* 4005 */
239+
"redundant"; /* 4009 */
239240
inline constexpr reg::PackedArg args[] = {
240241
reg::packed(0, 0, 25), /* __construct $templateArgumentObserver */
241242
reg::packed(76, 0, 99), /* __construct $varAnnotationProcessor */
@@ -479,22 +480,24 @@ inline constexpr reg::PackedArg args[] = {
479480
reg::packed(1388, MAY_BE_BOOL), /* denotesSameOffsetEveryTime return */
480481
reg::packed(3589, 0, 2030), /* trackedLinkImpliesOffset $offsetValueType */
481482
reg::packed(3720, 0, 2030), /* trackedLinkImpliesOffset $offsetType */
483+
reg::packed(3773, 0, 3686), /* trackedLinkImpliesOffset $writtenDimFetch */
484+
reg::packed(1476, 0, 1482), /* trackedLinkImpliesOffset $scope */
482485
reg::packed(1388, MAY_BE_BOOL), /* trackedLinkImpliesOffset return */
483-
reg::packed(3798, 0, 1368), /* isSameVariable $a */
484-
reg::packed(3800, 0, 1368), /* isSameVariable $b */
486+
reg::packed(3814, 0, 1368), /* isSameVariable $a */
487+
reg::packed(3816, 0, 1368), /* isSameVariable $b */
485488
reg::packed(1388, MAY_BE_BOOL), /* isSameVariable return */
486-
reg::packed(3798, 0, 1368), /* isSameOffset $a */
487-
reg::packed(3800, 0, 1368), /* isSameOffset $b */
489+
reg::packed(3814, 0, 1368), /* isSameOffset $a */
490+
reg::packed(3816, 0, 1368), /* isSameOffset $b */
488491
reg::packed(1388, MAY_BE_BOOL), /* isSameOffset return */
489492
reg::packed(1363, 0, 1368), /* getLiteralArrayKeyType $expr */
490493
reg::packed(1388, MAY_BE_NULL, 2030), /* getLiteralArrayKeyType return */
491494
reg::packed(1398, 0, 1416), /* getOriginalPropertyType $nodeScopeResolver */
492-
reg::packed(3853, 0, 3867), /* getOriginalPropertyType $propertyFetch */
495+
reg::packed(3869, 0, 3883), /* getOriginalPropertyType $propertyFetch */
493496
reg::packed(1476, 0, 1482), /* getOriginalPropertyType $scope */
494497
reg::packed(1388, 0, 2030), /* getOriginalPropertyType return */
495-
reg::packed(3965, 0, 3408), /* hasArrayReference $array */
498+
reg::packed(3981, 0, 3408), /* hasArrayReference $array */
496499
reg::packed(1388, MAY_BE_BOOL), /* hasArrayReference return */
497-
reg::packed(3989, 0, 1618), /* redundant $rhs */
500+
reg::packed(4005, 0, 1618), /* redundant $rhs */
498501
reg::packed(1967, 0, 1368), /* redundant $target */
499502
reg::packed(1513, 0, 1521), /* redundant $storage */
500503
reg::packed(1388, MAY_BE_NULL, 2030), /* redundant return */
@@ -541,13 +544,13 @@ inline constexpr sigtab::Sig advanceImplicitIndex = { { 3556 /* advanceImplicitI
541544
inline constexpr sigtab::Sig produceArrayDimFetchAssignValueToWrite = { { 3633 /* produceArrayDimFetchAssignValueToWrite */, 8, 222, 8, 230, ZEND_ACC_PRIVATE } };
542545
inline constexpr sigtab::Sig shouldKeepList = { { 3731 /* shouldKeepList */, 6, 231, 6, 237, ZEND_ACC_PRIVATE } };
543546
inline constexpr sigtab::Sig denotesSameOffsetEveryTime = { { 3746 /* denotesSameOffsetEveryTime */, 1, 238, 1, 239, ZEND_ACC_PRIVATE } };
544-
inline constexpr sigtab::Sig trackedLinkImpliesOffset = { { 3773 /* trackedLinkImpliesOffset */, 2, 240, 2, 242, ZEND_ACC_PRIVATE } };
545-
inline constexpr sigtab::Sig isSameVariable = { { 3802 /* isSameVariable */, 2, 243, 2, 245, ZEND_ACC_PRIVATE } };
546-
inline constexpr sigtab::Sig isSameOffset = { { 3817 /* isSameOffset */, 2, 246, 2, 248, ZEND_ACC_PRIVATE } };
547-
inline constexpr sigtab::Sig getLiteralArrayKeyType = { { 3830 /* getLiteralArrayKeyType */, 1, 249, 1, 250, ZEND_ACC_PRIVATE } };
548-
inline constexpr sigtab::Sig getOriginalPropertyType = { { 3941 /* getOriginalPropertyType */, 3, 251, 3, 254, ZEND_ACC_PRIVATE } };
549-
inline constexpr sigtab::Sig hasArrayReference = { { 3971 /* hasArrayReference */, 1, 255, 1, 256, ZEND_ACC_PRIVATE | ZEND_ACC_STATIC } };
550-
inline constexpr sigtab::Sig redundant = { { 3993 /* redundant */, 3, 257, 3, 260, ZEND_ACC_PRIVATE | ZEND_ACC_STATIC } };
547+
inline constexpr sigtab::Sig trackedLinkImpliesOffset = { { 3789 /* trackedLinkImpliesOffset */, 4, 240, 4, 244, ZEND_ACC_PRIVATE } };
548+
inline constexpr sigtab::Sig isSameVariable = { { 3818 /* isSameVariable */, 2, 245, 2, 247, ZEND_ACC_PRIVATE } };
549+
inline constexpr sigtab::Sig isSameOffset = { { 3833 /* isSameOffset */, 2, 248, 2, 250, ZEND_ACC_PRIVATE } };
550+
inline constexpr sigtab::Sig getLiteralArrayKeyType = { { 3846 /* getLiteralArrayKeyType */, 1, 251, 1, 252, ZEND_ACC_PRIVATE } };
551+
inline constexpr sigtab::Sig getOriginalPropertyType = { { 3957 /* getOriginalPropertyType */, 3, 253, 3, 256, ZEND_ACC_PRIVATE } };
552+
inline constexpr sigtab::Sig hasArrayReference = { { 3987 /* hasArrayReference */, 1, 257, 1, 258, ZEND_ACC_PRIVATE | ZEND_ACC_STATIC } };
553+
inline constexpr sigtab::Sig redundant = { { 4009 /* redundant */, 3, 259, 3, 262, ZEND_ACC_PRIVATE | ZEND_ACC_STATIC } };
551554
} // namespace sig
552555

553556
} // namespace ptdecl::AssignHandler

0 commit comments

Comments
 (0)