Skip to content

Commit be1a369

Browse files
VincentLangletphpstan-bot
authored andcommitted
Propagate array dim fetch narrowings to parent variable in BooleanAnd falsey and BooleanOr truthy intersectWith
- Add `propagateArrayDimFetchNarrowingsToParent()` to `TypeSpecifier` that enriches normalized `SpecifiedTypes` by computing a parent array variable's narrowed type from its child `ArrayDimFetch` sureType using `HasOffsetValueType`, mirroring what `MutatingScope::specifyExpressionType` already does when applying narrowings - Call the new method in the `BooleanAnd` falsey branch and the `BooleanOr` truthy branch before `intersectWith`, so that both sides have a sureType for the parent variable and the intersection correctly produces the union - Skip propagation for nested `ArrayDimFetch` parents to avoid over-narrowing in cases where the intermediate offset may not exist - The fix handles all type-check functions (`is_string`, `is_int`, `is_array`, etc.) and `instanceof` uniformly since they all go through the same conditional return type / type specifier machinery
1 parent 196eba6 commit be1a369

2 files changed

Lines changed: 121 additions & 2 deletions

File tree

src/Analyser/TypeSpecifier.php

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -734,7 +734,15 @@ public function specifyTypesInCondition(
734734
$leftTypes = $this->specifyTypesInCondition($scope, $expr->left, $context)->setRootExpr($expr);
735735
$rightScope = $scope->filterByTruthyValue($expr->left);
736736
$rightTypes = $this->specifyTypesInCondition($rightScope, $expr->right, $context)->setRootExpr($expr);
737-
$types = $context->true() ? $leftTypes->unionWith($rightTypes) : $leftTypes->normalize($scope)->intersectWith($rightTypes->normalize($rightScope));
737+
if ($context->true()) {
738+
$types = $leftTypes->unionWith($rightTypes);
739+
} else {
740+
$normalizedLeft = $leftTypes->normalize($scope);
741+
$normalizedRight = $rightTypes->normalize($rightScope);
742+
$normalizedLeft = $this->propagateArrayDimFetchNarrowingsToParent($normalizedLeft, $scope);
743+
$normalizedRight = $this->propagateArrayDimFetchNarrowingsToParent($normalizedRight, $rightScope);
744+
$types = $normalizedLeft->intersectWith($normalizedRight);
745+
}
738746
if ($context->false()) {
739747
$leftTypesForHolders = $leftTypes;
740748
$rightTypesForHolders = $rightTypes;
@@ -788,7 +796,11 @@ public function specifyTypesInCondition(
788796
) {
789797
$types = $leftTypes->normalize($scope);
790798
} else {
791-
$types = $leftTypes->normalize($scope)->intersectWith($rightTypes->normalize($rightScope));
799+
$normalizedLeft = $leftTypes->normalize($scope);
800+
$normalizedRight = $rightTypes->normalize($rightScope);
801+
$normalizedLeft = $this->propagateArrayDimFetchNarrowingsToParent($normalizedLeft, $scope);
802+
$normalizedRight = $this->propagateArrayDimFetchNarrowingsToParent($normalizedRight, $rightScope);
803+
$types = $normalizedLeft->intersectWith($normalizedRight);
792804
$types = $this->augmentBooleanOrTruthyWithConditionalHolders($scope, $rightScope, $expr, $types);
793805
}
794806
} else {
@@ -2076,6 +2088,54 @@ private function augmentBooleanOrTruthyWithConditionalHolders(MutatingScope $sco
20762088
return $types;
20772089
}
20782090

2091+
private function propagateArrayDimFetchNarrowingsToParent(SpecifiedTypes $normalizedTypes, Scope $scope): SpecifiedTypes
2092+
{
2093+
$additionalSureTypes = [];
2094+
foreach ($normalizedTypes->getSureTypes() as $exprString => [$exprNode, $type]) {
2095+
if (
2096+
!$exprNode instanceof ArrayDimFetch
2097+
|| $exprNode->dim === null
2098+
|| $exprNode->var instanceof ArrayDimFetch
2099+
) {
2100+
continue;
2101+
}
2102+
2103+
$dimType = $scope->getType($exprNode->dim)->toArrayKey();
2104+
if (!$dimType instanceof ConstantIntegerType && !$dimType instanceof ConstantStringType) { // @phpstan-ignore phpstanApi.instanceofType
2105+
continue;
2106+
}
2107+
2108+
$parentExprString = $this->exprPrinter->printExpr($exprNode->var);
2109+
if (isset($normalizedTypes->getSureTypes()[$parentExprString]) || isset($additionalSureTypes[$parentExprString])) {
2110+
continue;
2111+
}
2112+
2113+
$parentType = $scope->getType($exprNode->var);
2114+
if ($parentType instanceof MixedType || !$parentType->isArray()->yes()) {
2115+
continue;
2116+
}
2117+
2118+
$narrowedParentType = TypeCombinator::intersect(
2119+
$parentType,
2120+
new HasOffsetValueType($dimType, $type),
2121+
);
2122+
if ($narrowedParentType instanceof NeverType) {
2123+
continue;
2124+
}
2125+
2126+
$additionalSureTypes[$parentExprString] = [$exprNode->var, $narrowedParentType];
2127+
}
2128+
2129+
if ($additionalSureTypes === []) {
2130+
return $normalizedTypes;
2131+
}
2132+
2133+
return new SpecifiedTypes(
2134+
$normalizedTypes->getSureTypes() + $additionalSureTypes,
2135+
[],
2136+
);
2137+
}
2138+
20792139
/**
20802140
* @return array<string, ConditionalExpressionHolder[]>
20812141
*/
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
namespace Bug14566;
6+
7+
use function PHPStan\Testing\assertType;
8+
9+
/**
10+
* @param array{}|array{hi: 'hello'}|array{hi: array{0: 42, 1?: 42}} $test
11+
*/
12+
function foo(array $test): void {
13+
if (isset($test['hi']) && is_string($test['hi'])) {
14+
return;
15+
}
16+
assertType("array{}|array{hi: array{0: 42, 1?: 42}}", $test);
17+
assertType("array{0: 42, 1?: 42}", $test['hi']);
18+
}
19+
20+
/**
21+
* @param array{}|array{hi: 'hello'}|array{hi: array{0: 42, 1?: 42}} $test
22+
*/
23+
function fooOr(array $test): void {
24+
if (!isset($test['hi']) || !is_string($test['hi'])) {
25+
assertType("array{}|array{hi: array{0: 42, 1?: 42}}", $test);
26+
return;
27+
}
28+
assertType("array{hi: 'hello'}", $test);
29+
}
30+
31+
/**
32+
* @param array{}|array{hi: 42}|array{hi: 'hello'} $test
33+
*/
34+
function fooIsInt(array $test): void {
35+
if (isset($test['hi']) && is_int($test['hi'])) {
36+
return;
37+
}
38+
assertType("array{}|array{hi: 'hello'}", $test);
39+
}
40+
41+
/**
42+
* @param array{}|array{hi: 'hello'}|array{hi: array{0: 42}} $test
43+
*/
44+
function fooIsArray(array $test): void {
45+
if (isset($test['hi']) && is_array($test['hi'])) {
46+
return;
47+
}
48+
assertType("array{}|array{hi: 'hello'}", $test);
49+
}
50+
51+
/**
52+
* @param array{}|array{hi: \stdClass}|array{hi: 'hello'} $test
53+
*/
54+
function fooInstanceof(array $test): void {
55+
if (isset($test['hi']) && $test['hi'] instanceof \stdClass) {
56+
return;
57+
}
58+
assertType("array{}|array{hi: 'hello'}", $test);
59+
}

0 commit comments

Comments
 (0)