|
33 | 33 | use PHPStan\Analyser\ExprHandler; |
34 | 34 | use PHPStan\Analyser\ExprHandler\Helper\DefaultNarrowingHelper; |
35 | 35 | use PHPStan\Analyser\ExprHandler\Helper\IdenticalNarrowingHelper; |
| 36 | +use PHPStan\Analyser\ExprHandler\Helper\IllegalOffsetTypeHelper; |
36 | 37 | use PHPStan\Analyser\ExprHandler\Helper\MethodThrowPointHelper; |
37 | 38 | use PHPStan\Analyser\ExprHandler\Helper\NonNullabilityHelper; |
38 | 39 | use PHPStan\Analyser\ExprHandler\Helper\VirtualExprResultHelper; |
@@ -286,6 +287,8 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex |
286 | 287 | VariableFlowBuilder::targetRead($expr->var, $storage, false), |
287 | 288 | $assignedExprResult->getVariableFlow(), |
288 | 289 | $valueFlowWrite !== null && $context->isValueConsumed() ? VariableFlow::inputs($valueFlowWrite->getId(), $context->getValueFlowTarget() !== null ? $context->getValueFlowTarget()->getId() : null) : null, |
| 290 | + // an illegal offset throws before the write |
| 291 | + count($target->getIllegalOffsetDimFetches()) > 0 ? VariableFlow::throwing(new ObjectType(TypeError::class), true) : null, |
289 | 292 | VariableFlowBuilder::targetWrite($expr->var, VariableWrite::KIND_ASSIGN, $scope, $storage, $redundantType), |
290 | 293 | ); |
291 | 294 | if ($expr instanceof Assign && $expr->expr instanceof Expr\Array_ && self::hasArrayReference($expr->expr)) { |
@@ -852,6 +855,35 @@ private function doPrepareTarget( |
852 | 855 | $nodeScopeResolver->callNodeCallback($nodeCallback, $dimFetch, $enterExpressionAssign ? $callbackScope->enterExpressionAssign($dimFetch) : $callbackScope, $storage); |
853 | 856 | } |
854 | 857 |
|
| 858 | + // the links whose offset may throw TypeError - PHP throws only after |
| 859 | + // evaluating the assigned value, so applyWrite() creates their throw points |
| 860 | + $illegalOffsetDimFetches = []; |
| 861 | + if ($this->phpVersion->throwsTypeErrorForIllegalOffsets()) { |
| 862 | + foreach ($offsetTypes as $i => [$offsetType, $offsetDimFetch]) { |
| 863 | + // $arr[] = ... never throws for the offset |
| 864 | + if ($offsetType === null) { |
| 865 | + continue; |
| 866 | + } |
| 867 | + if (!IllegalOffsetTypeHelper::mayOffsetThrowTypeError($offsetType)) { |
| 868 | + continue; |
| 869 | + } |
| 870 | + // writes to ArrayAccess objects go through offsetSet() whose throw points applyWrite() models; |
| 871 | + // the container is the previous chain link - the root for the first dimension |
| 872 | + $containerResult = $i === 0 ? $varResult : $deferredDimFetchResults[$i - 1][1]; |
| 873 | + $containerType = $containerResult->getType(); |
| 874 | + if ((new ObjectType(ArrayAccess::class))->isSuperTypeOf($containerType)->yes()) { |
| 875 | + continue; |
| 876 | + } |
| 877 | + // `$lvalue ??= ...` throws reading an array or a string container, |
| 878 | + // before the assigned value is evaluated |
| 879 | + if ($mode->issetSemanticsForRead() && ($containerType->isArray()->yes() || $containerType->isString()->yes())) { |
| 880 | + $throwPoints[] = InternalThrowPoint::createExplicit($scope, new ObjectType(TypeError::class), $offsetDimFetch, false); |
| 881 | + continue; |
| 882 | + } |
| 883 | + $illegalOffsetDimFetches[] = $offsetDimFetch; |
| 884 | + } |
| 885 | + } |
| 886 | + |
855 | 887 | if ($mode->issetSemanticsForRead()) { |
856 | 888 | // `$lvalue ??= ...` reads the chain with isset() semantics. The root |
857 | 889 | // and dimensions were just walked, so each chain link's read is |
@@ -931,6 +963,7 @@ private function doPrepareTarget( |
931 | 963 | targetReadResult: $targetReadResult, |
932 | 964 | targetChainResults: $targetChainResults, |
933 | 965 | dependencies: $targetDependencies, |
| 966 | + illegalOffsetDimFetches: $illegalOffsetDimFetches, |
934 | 967 | ); |
935 | 968 | } |
936 | 969 |
|
@@ -1438,6 +1471,25 @@ public function applyWrite( |
1438 | 1471 | $varType = new ConstantArrayType([], []); |
1439 | 1472 | $varNativeType = new ConstantArrayType([], []); |
1440 | 1473 | } |
| 1474 | + |
| 1475 | + // an illegal offset throws TypeError after the assigned value was |
| 1476 | + // evaluated and an undefined root was turned into an array |
| 1477 | + $illegalOffsetDimFetches = $target->getIllegalOffsetDimFetches(); |
| 1478 | + if (count($illegalOffsetDimFetches) > 0) { |
| 1479 | + $throwPointScope = $scope; |
| 1480 | + if (!$isImplicitArrayCreation->no() && $var instanceof Variable && is_string($var->name)) { |
| 1481 | + $throwPointScope = $throwPointScope->assignVariable( |
| 1482 | + $var->name, |
| 1483 | + TypeCombinator::union($varType, new ConstantArrayType([], [])), |
| 1484 | + TypeCombinator::union($varNativeType, new ConstantArrayType([], [])), |
| 1485 | + TrinaryLogic::createYes(), |
| 1486 | + [], |
| 1487 | + ); |
| 1488 | + } |
| 1489 | + foreach ($illegalOffsetDimFetches as $illegalOffsetDimFetch) { |
| 1490 | + $throwPoints[] = InternalThrowPoint::createExplicit($throwPointScope, new ObjectType(TypeError::class), $illegalOffsetDimFetch, false); |
| 1491 | + } |
| 1492 | + } |
1441 | 1493 | $offsetValueType = $varType; |
1442 | 1494 | $offsetNativeValueType = $varNativeType; |
1443 | 1495 |
|
|
0 commit comments