Skip to content

Commit 9267117

Browse files
committed
Keep the specified type of an expression while matching conditional expressions
A matched holder no longer replaces what the narrowing itself specified, so the holders matched after it still see the narrowed type: in if ($emptyData instanceof Closure), a chain through $builder->getEmptyData() no longer turns $emptyData back into mixed and loses $lazyEmptyData. Amp-Thread-ID: https://ampcode.com/threads/T-01a2214e-8521-7498-a0c4-2256a346738b
1 parent 21c16c5 commit 9267117

4 files changed

Lines changed: 65 additions & 7 deletions

File tree

‎src/Analyser/ScopeOps.php‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1214,6 +1214,11 @@ public static function matchConditionalExpressions(array $conditionalExpressions
12141214
}
12151215

12161216
$conditions[$conditionalExprString][] = $conditionalExpression;
1217+
// a holder narrows what the specification already established,
1218+
// so the later holders keep matching against the specification
1219+
if (array_key_exists($conditionalExprString, $originallySpecifiedExprStrings)) {
1220+
continue;
1221+
}
12171222
$specifiedExpressions[$conditionalExprString] = $conditionalExpression->getTypeHolder();
12181223
}
12191224

@@ -1234,6 +1239,9 @@ public static function matchConditionalExpressions(array $conditionalExpressions
12341239
}
12351240

12361241
$conditions[$conditionalExprString][] = $conditionalExpression;
1242+
if (array_key_exists($conditionalExprString, $originallySpecifiedExprStrings)) {
1243+
continue;
1244+
}
12371245
$specifiedExpressions[$conditionalExprString] = $conditionalExpression->getTypeHolder();
12381246
}
12391247
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace ConditionalHolderKeepsSpecifiedType;
4+
5+
use PHPStan\TrinaryLogic;
6+
use function PHPStan\Testing\assertType;
7+
use function PHPStan\Testing\assertVariableCertainty;
8+
9+
interface Builder
10+
{
11+
12+
/** @return mixed */
13+
public function getEmptyData();
14+
15+
}
16+
17+
function ifElse(Builder $builder, bool $withMinutes): void
18+
{
19+
if ($builder->getEmptyData()) {
20+
$emptyData = $builder->getEmptyData();
21+
} else {
22+
$emptyData = [];
23+
}
24+
25+
if ($emptyData instanceof \Closure) {
26+
$lazyEmptyData = static fn ($option) => $option;
27+
}
28+
29+
if ($withMinutes) {
30+
if ($emptyData instanceof \Closure) {
31+
assertType('Closure', $emptyData);
32+
assertVariableCertainty(TrinaryLogic::createYes(), $lazyEmptyData);
33+
}
34+
}
35+
}
36+
37+
function shortTernary(Builder $builder, bool $withMinutes): void
38+
{
39+
$emptyData = $builder->getEmptyData() ?: [];
40+
41+
if ($emptyData instanceof \Closure) {
42+
$lazyEmptyData = static fn ($option) => $option;
43+
}
44+
45+
if ($withMinutes) {
46+
if ($emptyData instanceof \Closure) {
47+
assertType('Closure', $emptyData);
48+
assertVariableCertainty(TrinaryLogic::createYes(), $lazyEmptyData);
49+
}
50+
}
51+
}

‎tests/PHPStan/Rules/Properties/TypesAssignedToPropertiesRuleTest.php‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -176,10 +176,6 @@ public function testTypesAssignedToPropertiesExpressionNames(): void
176176
'Property PropertiesFromArrayIntoObject\Foo::$lall (int) does not accept string.',
177177
69,
178178
],
179-
[
180-
'Property PropertiesFromArrayIntoObject\Foo::$foo (string) does not accept float.',
181-
83,
182-
],
183179
[
184180
'Property PropertiesFromArrayIntoObject\Foo::$foo (string) does not accept float|int|string.',
185181
97,

‎turbo-ext/src/ScopeOps.cpp‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1371,7 +1371,7 @@ class ScopeOps
13711371
continue;
13721372
}
13731373

1374-
recordMatchedCondition(conditions, specified, conditionalKey, conditionalIdx, holder, typeHolder);
1374+
recordMatchedCondition(conditions, specified, specifiedInput, conditionalKey, conditionalIdx, holder, typeHolder);
13751375
}
13761376

13771377
/* Pass 2: supertype match, only when Pass 1 found nothing */
@@ -1408,7 +1408,7 @@ class ScopeOps
14081408
zv::Ref typeHolder = zv::ObjRef(holder.asObject()).propAt(PT_CEH_PROP_TYPEHOLDER);
14091409
if (pt_holder_certainty_value(typeHolder.asObject()) == PT_TRI_NO) continue;
14101410

1411-
recordMatchedCondition(conditions, specified, conditionalKey, conditionalIdx, holder, typeHolder);
1411+
recordMatchedCondition(conditions, specified, specifiedInput, conditionalKey, conditionalIdx, holder, typeHolder);
14121412
}
14131413
}
14141414

@@ -2220,7 +2220,7 @@ class ScopeOps
22202220
zend_hash_merge(candidates.table(), Z_ARRVAL_P(targets), NULL, 0);
22212221
}
22222222

2223-
static void recordMatchedCondition(zv::Arr &conditions, zv::Arr &specified, zend_string *condKey, zend_ulong condIdx, zv::Ref conditionalExpression, zv::Ref typeHolder)
2223+
static void recordMatchedCondition(zv::Arr &conditions, zv::Arr &specified, zv::TableRef originallySpecified, zend_string *condKey, zend_ulong condIdx, zv::Ref conditionalExpression, zv::Ref typeHolder)
22242224
{
22252225
zval *group = pt_ht_find(conditions.table(), condKey, condIdx);
22262226
if (group == NULL) {
@@ -2231,6 +2231,9 @@ class ScopeOps
22312231
}
22322232
Z_ADDREF_P(conditionalExpression.raw());
22332233
add_next_index_zval(group, conditionalExpression.raw());
2234+
/* a holder narrows what the specification already established, so
2235+
* the later holders keep matching against the specification */
2236+
if (pt_ht_exists(originallySpecified.table(), condKey, condIdx)) return;
22342237
Z_TRY_ADDREF_P(typeHolder.raw());
22352238
pt_ht_update(specified.table(), condKey, condIdx, typeHolder.raw());
22362239
}

0 commit comments

Comments
 (0)