2121use PhpParser \Node \Expr \Variable ;
2222use PhpParser \Node \Name ;
2323use PhpParser \Node \Stmt ;
24+ use PhpParser \NodeFinder ;
2425use PHPStan \Analyser \AssignTargetWalkMode ;
2526use PHPStan \Analyser \ConditionalExpressionHolder ;
2627use PHPStan \Analyser \ExpressionContext ;
7980use PHPStan \Type \TypeUtils ;
8081use PHPStan \Type \UnionType ;
8182use TypeError ;
83+ use function array_key_exists ;
8284use function array_key_last ;
8385use function array_merge ;
8486use function array_pop ;
@@ -100,6 +102,8 @@ final class AssignHandler implements ExprHandler
100102
101103 private const TERNARY_ARM_EXCLUDED_VALUES_LIMIT = 3 ;
102104
105+ private const DERIVED_CONDITIONAL_EXPRESSIONS_LIMIT = 16 ;
106+
103107 public function __construct (
104108 private VarAnnotationProcessor $ varAnnotationProcessor ,
105109 private TypeSpecifier $ typeSpecifier ,
@@ -950,6 +954,8 @@ public function applyWrite(
950954 $ conditionalExpressions = $ this ->processInArrayForConditionalExpressionsAfterAssign ($ scopeBeforeAssignEval , $ var ->name , $ conditionalExpressions , $ assignedExpr , $ type , $ impurePoints );
951955 }
952956
957+ $ conditionalExpressions = $ this ->processDerivedConditionalExpressionsAfterAssign ($ scopeBeforeAssignEval , $ var ->name , $ conditionalExpressions , $ assignedExpr , $ type , $ impurePoints );
958+
953959 $ truthyType = TypeCombinator ::removeFalsey ($ type );
954960 // Value comparison, not identity: remove() happens to hand back the very same
955961 // instance when it removes nothing, but that is not part of its contract — the
@@ -1663,6 +1669,87 @@ private function processMatchForConditionalExpressionsAfterAssign(
16631669 return $ newConditionalExpressions ;
16641670 }
16651671
1672+ /**
1673+ * Propagates conditional expressions through a derived assignment: when the
1674+ * right-hand side reads a variable that existing conditional expressions describe
1675+ * (e.g. `if $key = 'test1' then $functionName is 'Test'`), the assigned variable
1676+ * gets its own conditional expressions under the same conditions, with the
1677+ * right-hand side re-evaluated under each consequent
1678+ * (`if $key = 'test1' then $functionToCall is 'fetchTest'`).
1679+ *
1680+ * @param array<string, ConditionalExpressionHolder[]> $conditionalExpressions
1681+ * @param ImpurePoint[] $rhsImpurePoints
1682+ * @return array<string, ConditionalExpressionHolder[]>
1683+ */
1684+ private function processDerivedConditionalExpressionsAfterAssign (
1685+ MutatingScope $ scope ,
1686+ string $ variableName ,
1687+ array $ conditionalExpressions ,
1688+ Expr $ assignedExpr ,
1689+ Type $ assignedType ,
1690+ array $ rhsImpurePoints ,
1691+ ): array
1692+ {
1693+ if (count ($ rhsImpurePoints ) > 0 ) {
1694+ return $ conditionalExpressions ;
1695+ }
1696+ $ scopeConditionalExpressions = $ scope ->getConditionalExpressions ();
1697+ if (count ($ scopeConditionalExpressions ) === 0 ) {
1698+ return $ conditionalExpressions ;
1699+ }
1700+
1701+ $ targetExprString = '$ ' . $ variableName ;
1702+ $ evaluations = 0 ;
1703+ $ seenReadExprStrings = [];
1704+ /** @var Variable[] $readVariables */
1705+ $ readVariables = (new NodeFinder ())->findInstanceOf ([$ assignedExpr ], Variable::class);
1706+ foreach ($ readVariables as $ readVariable ) {
1707+ if (!is_string ($ readVariable ->name ) || $ readVariable ->name === $ variableName ) {
1708+ continue ;
1709+ }
1710+ $ readExprString = '$ ' . $ readVariable ->name ;
1711+ if (array_key_exists ($ readExprString , $ seenReadExprStrings )) {
1712+ continue ;
1713+ }
1714+ $ seenReadExprStrings [$ readExprString ] = true ;
1715+
1716+ foreach ($ scopeConditionalExpressions [$ readExprString ] ?? [] as $ holder ) {
1717+ $ consequent = $ holder ->getTypeHolder ();
1718+ if (!$ consequent ->getCertainty ()->yes ()) {
1719+ continue ;
1720+ }
1721+
1722+ $ conditionHolders = $ holder ->getConditionExpressionTypeHolders ();
1723+ $ evalScope = $ scope ;
1724+ foreach ($ conditionHolders as $ conditionExprString => $ conditionHolder ) {
1725+ if ($ conditionExprString === $ targetExprString || !$ conditionHolder ->getCertainty ()->yes ()) {
1726+ // a condition on the just-overwritten variable is stale
1727+ continue 2 ;
1728+ }
1729+ $ evalScope = $ evalScope ->assignExpression ($ conditionHolder ->getExpr (), $ conditionHolder ->getType (), $ conditionHolder ->getType ());
1730+ }
1731+
1732+ if (++$ evaluations > self ::DERIVED_CONDITIONAL_EXPRESSIONS_LIMIT ) {
1733+ return $ conditionalExpressions ;
1734+ }
1735+
1736+ $ evalScope = $ evalScope ->assignExpression ($ consequent ->getExpr (), $ consequent ->getType (), $ consequent ->getType ());
1737+ $ derivedType = $ evalScope ->getType ($ assignedExpr );
1738+ if ($ derivedType ->equals ($ assignedType )) {
1739+ continue ;
1740+ }
1741+
1742+ $ derivedHolder = new ConditionalExpressionHolder (
1743+ $ conditionHolders ,
1744+ ExpressionTypeHolder ::createYes (new Variable ($ variableName ), $ derivedType ),
1745+ );
1746+ $ conditionalExpressions [$ targetExprString ][$ derivedHolder ->getKey ()] = $ derivedHolder ;
1747+ }
1748+ }
1749+
1750+ return $ conditionalExpressions ;
1751+ }
1752+
16661753 /**
16671754 * Records the reverse implication of a `$var = in_array($needle, [...known values])`
16681755 * assignment: a needle that is one of the haystack's always-present values makes the
0 commit comments