|
7 | 7 | use PhpParser\Node\ComplexType; |
8 | 8 | use PhpParser\Node\Expr; |
9 | 9 | use PhpParser\Node\Expr\Array_; |
| 10 | +use PhpParser\Node\Expr\BinaryOp; |
10 | 11 | use PhpParser\Node\Expr\CallLike; |
11 | 12 | use PhpParser\Node\Expr\ClassConstFetch; |
12 | 13 | use PhpParser\Node\Expr\ConstFetch; |
|
112 | 113 | use function array_filter; |
113 | 114 | use function array_intersect_key; |
114 | 115 | use function array_key_exists; |
| 116 | +use function array_key_first; |
115 | 117 | use function array_keys; |
116 | 118 | use function array_last; |
117 | 119 | use function array_map; |
@@ -159,6 +161,9 @@ class MutatingScope implements Scope, NodeCallbackInvoker, CollectedDataEmitter, |
159 | 161 | /** Past this array nesting, widening between loop passes turns an array value whose nesting grew into mixed. */ |
160 | 162 | private const GENERALIZED_ARRAY_DEPTH_LIMIT = 2; |
161 | 163 |
|
| 164 | + /** Rounds of carrying a narrowing through tracked comparison verdicts per application, a chain `$a < $b < $c` takes one per link. */ |
| 165 | + private const RELATIONAL_FACT_ROUNDS_LIMIT = 4; |
| 166 | + |
162 | 167 | /** Magic methods that let the author decide which properties survive a serialize()/unserialize() round trip. */ |
163 | 168 | private const CUSTOM_SERIALIZATION_METHODS = ['__sleep', '__serialize', '__unserialize']; |
164 | 169 |
|
@@ -4158,6 +4163,58 @@ public function applySpecifiedTypes(SpecifiedTypes $specifiedTypes): self |
4158 | 4163 | $specifiedExpressions[$exprString] = ExpressionTypeHolder::createYes($expr, $holderType); |
4159 | 4164 | } |
4160 | 4165 |
|
| 4166 | + // a tracked comparison verdict carries the narrowing of one operand |
| 4167 | + // over to the other one |
| 4168 | + $changedExpressions = $specifiedExpressions; |
| 4169 | + for ($round = 0; $changedExpressions !== [] && $round < self::RELATIONAL_FACT_ROUNDS_LIMIT; $round++) { |
| 4170 | + $derivedTypes = $scope->deriveFromRelationalFacts($changedExpressions); |
| 4171 | + $changedExpressions = []; |
| 4172 | + while ($derivedTypes !== []) { |
| 4173 | + $exprString = (string) array_key_first($derivedTypes); // @phpstan-ignore cast.useless |
| 4174 | + [$expr, $type] = $derivedTypes[$exprString]; |
| 4175 | + unset($derivedTypes[$exprString]); |
| 4176 | + if ( |
| 4177 | + !array_key_exists($exprString, $scope->expressionTypes) |
| 4178 | + || !$scope->expressionTypes[$exprString]->getCertainty()->yes() |
| 4179 | + ) { |
| 4180 | + continue; |
| 4181 | + } |
| 4182 | + $trackedType = $scope->expressionTypes[$exprString]->getType(); |
| 4183 | + $newType = TypeCombinator::intersect($type, $trackedType); |
| 4184 | + if ($newType->equals($trackedType)) { |
| 4185 | + continue; |
| 4186 | + } |
| 4187 | + $trackedNativeType = array_key_exists($exprString, $scope->nativeExpressionTypes) && $scope->nativeExpressionTypes[$exprString]->getCertainty()->yes() |
| 4188 | + ? $scope->nativeExpressionTypes[$exprString]->getType() |
| 4189 | + : null; |
| 4190 | + $newNativeType = $trackedNativeType !== null ? TypeCombinator::intersect($type, $trackedNativeType) : $type; |
| 4191 | + if (!$scopeIsWorkingCopy) { |
| 4192 | + $scope = $scope->openSpecificationScope(); |
| 4193 | + $scopeIsWorkingCopy = true; |
| 4194 | + } |
| 4195 | + $scope->specifyExpressionTypeInPlace($expr, $newType, $newNativeType, TrinaryLogic::createYes()); |
| 4196 | + |
| 4197 | + $holder = ExpressionTypeHolder::createYes($expr, $scope->expressionTypes[$exprString]->getType()); |
| 4198 | + $changedExpressions[$exprString] = $holder; |
| 4199 | + $specifiedExpressions[$exprString] = $holder; |
| 4200 | + |
| 4201 | + // a count() that cannot be 0 any more makes its array non-empty |
| 4202 | + if ( |
| 4203 | + !$expr instanceof FuncCall |
| 4204 | + || !$expr->name instanceof Name |
| 4205 | + || !in_array($expr->name->toLowerString(), ['count', 'sizeof'], true) |
| 4206 | + || $expr->isFirstClassCallable() |
| 4207 | + || count($expr->getArgs()) !== 1 |
| 4208 | + || $expr->getArgs()[0]->unpack |
| 4209 | + || !(new ConstantIntegerType(0))->isSuperTypeOf($newType)->no() |
| 4210 | + ) { |
| 4211 | + continue; |
| 4212 | + } |
| 4213 | + $countedExpr = $expr->getArgs()[0]->value; |
| 4214 | + $derivedTypes[$scope->getNodeKey($countedExpr)] = [$countedExpr, new NonEmptyArrayType()]; |
| 4215 | + } |
| 4216 | + } |
| 4217 | + |
4161 | 4218 | $scope = $scope->processConditionalExpressionsAfterSpecifying($specifiedExpressions); |
4162 | 4219 |
|
4163 | 4220 | $newConditionalExpressionHolders = $specifiedTypes->getNewConditionalExpressionHolders(); |
@@ -4194,6 +4251,95 @@ public function applySpecifiedTypes(SpecifiedTypes $specifiedTypes): self |
4194 | 4251 | ); |
4195 | 4252 | } |
4196 | 4253 |
|
| 4254 | + /** |
| 4255 | + * The narrowings the tracked `$a === $b` and integer `$a < $b` verdicts |
| 4256 | + * imply for one operand when the other one is among the changed |
| 4257 | + * expressions. A verdict changed itself was narrowed together with its |
| 4258 | + * operands. |
| 4259 | + * |
| 4260 | + * @param array<string, ExpressionTypeHolder> $changedExpressions |
| 4261 | + * @return array<string, array{Expr, Type}> |
| 4262 | + */ |
| 4263 | + private function deriveFromRelationalFacts(array $changedExpressions): array |
| 4264 | + { |
| 4265 | + $derivedTypes = []; |
| 4266 | + foreach ($this->expressionTypes as $factString => $factHolder) { |
| 4267 | + $fact = $factHolder->getExpr(); |
| 4268 | + if (!$fact instanceof BinaryOp || array_key_exists((string) $factString, $changedExpressions)) { // @phpstan-ignore cast.useless |
| 4269 | + continue; |
| 4270 | + } |
| 4271 | + $isIdentical = $fact instanceof BinaryOp\Identical; |
| 4272 | + $isIdentity = $isIdentical || $fact instanceof BinaryOp\NotIdentical; |
| 4273 | + $isSmaller = $fact instanceof BinaryOp\Smaller || $fact instanceof BinaryOp\SmallerOrEqual; |
| 4274 | + if (!$isIdentity && !$isSmaller && !$fact instanceof BinaryOp\Greater && !$fact instanceof BinaryOp\GreaterOrEqual) { |
| 4275 | + continue; |
| 4276 | + } |
| 4277 | + if (!$factHolder->getCertainty()->yes()) { |
| 4278 | + continue; |
| 4279 | + } |
| 4280 | + $verdict = $factHolder->getType(); |
| 4281 | + if ($verdict->isTrue()->yes()) { |
| 4282 | + $holds = true; |
| 4283 | + } elseif ($verdict->isFalse()->yes()) { |
| 4284 | + $holds = false; |
| 4285 | + } else { |
| 4286 | + continue; |
| 4287 | + } |
| 4288 | + |
| 4289 | + $leftKey = $this->getNodeKey($fact->left); |
| 4290 | + $rightKey = $this->getNodeKey($fact->right); |
| 4291 | + $leftChanged = array_key_exists($leftKey, $changedExpressions); |
| 4292 | + $rightChanged = array_key_exists($rightKey, $changedExpressions); |
| 4293 | + if (!$leftChanged && !$rightChanged) { |
| 4294 | + continue; |
| 4295 | + } |
| 4296 | + |
| 4297 | + $implied = []; |
| 4298 | + if ($isIdentity) { |
| 4299 | + if ($holds !== $isIdentical) { |
| 4300 | + continue; |
| 4301 | + } |
| 4302 | + if ($leftChanged) { |
| 4303 | + $implied[] = [$rightKey, $fact->right, $changedExpressions[$leftKey]->getType()]; |
| 4304 | + } |
| 4305 | + if ($rightChanged) { |
| 4306 | + $implied[] = [$leftKey, $fact->left, $changedExpressions[$rightKey]->getType()]; |
| 4307 | + } |
| 4308 | + } else { |
| 4309 | + // normalized to `$smaller < $larger` / `$smaller <= $larger`; |
| 4310 | + // a false verdict of integers is the inverse comparison |
| 4311 | + $orEqual = $fact instanceof BinaryOp\SmallerOrEqual || $fact instanceof BinaryOp\GreaterOrEqual; |
| 4312 | + [$smaller, $larger, $smallerKey, $largerKey] = $isSmaller |
| 4313 | + ? [$fact->left, $fact->right, $leftKey, $rightKey] |
| 4314 | + : [$fact->right, $fact->left, $rightKey, $leftKey]; |
| 4315 | + if (!$holds) { |
| 4316 | + [$smaller, $larger, $smallerKey, $largerKey] = [$larger, $smaller, $largerKey, $smallerKey]; |
| 4317 | + $orEqual = !$orEqual; |
| 4318 | + } |
| 4319 | + if (array_key_exists($smallerKey, $changedExpressions)) { |
| 4320 | + $smallerType = $changedExpressions[$smallerKey]->getType(); |
| 4321 | + if ($smallerType->isInteger()->yes()) { |
| 4322 | + $implied[] = [$largerKey, $larger, $orEqual ? $smallerType->getGreaterOrEqualType($this->services->phpVersion) : $smallerType->getGreaterType($this->services->phpVersion)]; |
| 4323 | + } |
| 4324 | + } |
| 4325 | + if (array_key_exists($largerKey, $changedExpressions)) { |
| 4326 | + $largerType = $changedExpressions[$largerKey]->getType(); |
| 4327 | + if ($largerType->isInteger()->yes()) { |
| 4328 | + $implied[] = [$smallerKey, $smaller, $orEqual ? $largerType->getSmallerOrEqualType($this->services->phpVersion) : $largerType->getSmallerType($this->services->phpVersion)]; |
| 4329 | + } |
| 4330 | + } |
| 4331 | + } |
| 4332 | + |
| 4333 | + foreach ($implied as [$impliedKey, $impliedExpr, $impliedType]) { |
| 4334 | + $derivedTypes[$impliedKey] = array_key_exists($impliedKey, $derivedTypes) |
| 4335 | + ? [$impliedExpr, TypeCombinator::intersect($derivedTypes[$impliedKey][1], $impliedType)] |
| 4336 | + : [$impliedExpr, $impliedType]; |
| 4337 | + } |
| 4338 | + } |
| 4339 | + |
| 4340 | + return $derivedTypes; |
| 4341 | + } |
| 4342 | + |
4197 | 4343 | /** |
4198 | 4344 | * Matches already-registered conditional expressions against the just-specified |
4199 | 4345 | * expression type holders and applies the matching consequences. |
|
0 commit comments