|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PHPStan\Type\Php; |
| 4 | + |
| 5 | +use PhpParser\Node\Expr\FuncCall; |
| 6 | +use PHPStan\Analyser\Scope; |
| 7 | +use PHPStan\Analyser\SpecifiedTypes; |
| 8 | +use PHPStan\Analyser\TypeSpecifier; |
| 9 | +use PHPStan\Analyser\TypeSpecifierAwareExtension; |
| 10 | +use PHPStan\Analyser\TypeSpecifierContext; |
| 11 | +use PHPStan\DependencyInjection\AutowiredService; |
| 12 | +use PHPStan\Reflection\FunctionReflection; |
| 13 | +use PHPStan\ShouldNotHappenException; |
| 14 | +use PHPStan\Type\Accessory\AccessoryArrayListType; |
| 15 | +use PHPStan\Type\Accessory\NonEmptyArrayType; |
| 16 | +use PHPStan\Type\ArrayType; |
| 17 | +use PHPStan\Type\FunctionTypeSpecifyingExtension; |
| 18 | +use PHPStan\Type\IntegerRangeType; |
| 19 | +use PHPStan\Type\MixedType; |
| 20 | +use PHPStan\Type\TypeCombinator; |
| 21 | +use function strtolower; |
| 22 | + |
| 23 | +/** |
| 24 | + * An array that array_is_list() rejects holds something: the empty array is a |
| 25 | + * list. So `!array_is_list($a)` makes an array `$a` non-empty, also where its |
| 26 | + * type says it is never a list, as `array<string, int>` - PHPStan treats an |
| 27 | + * array whose keys are no list keys as never a list, which the empty one is. |
| 28 | + */ |
| 29 | +#[AutowiredService] |
| 30 | +final class ArrayIsListFunctionTypeSpecifyingExtension implements FunctionTypeSpecifyingExtension, TypeSpecifierAwareExtension |
| 31 | +{ |
| 32 | + |
| 33 | + private TypeSpecifier $typeSpecifier; |
| 34 | + |
| 35 | + public function isFunctionSupported(FunctionReflection $functionReflection, FuncCall $node, TypeSpecifierContext $context): bool |
| 36 | + { |
| 37 | + return strtolower($functionReflection->getName()) === 'array_is_list' |
| 38 | + && !$context->null(); |
| 39 | + } |
| 40 | + |
| 41 | + public function specifyTypes(FunctionReflection $functionReflection, FuncCall $node, Scope $scope, TypeSpecifierContext $context): SpecifiedTypes |
| 42 | + { |
| 43 | + $args = $node->getArgs(); |
| 44 | + if (!isset($args[0])) { |
| 45 | + return new SpecifiedTypes(); |
| 46 | + } |
| 47 | + if ($context->null()) { |
| 48 | + throw new ShouldNotHappenException(); |
| 49 | + } |
| 50 | + |
| 51 | + $listType = TypeCombinator::intersect( |
| 52 | + new ArrayType(IntegerRangeType::fromInterval(0, null), new MixedType()), |
| 53 | + new AccessoryArrayListType(), |
| 54 | + ); |
| 55 | + $specifiedTypes = $this->typeSpecifier->create($args[0]->value, $listType, $context, $scope); |
| 56 | + $valueType = $scope->getType($args[0]->value); |
| 57 | + if ($context->true() || !$valueType->isArray()->yes()) { |
| 58 | + return $specifiedTypes; |
| 59 | + } |
| 60 | + |
| 61 | + return $specifiedTypes->unionWith( |
| 62 | + $this->typeSpecifier->create($args[0]->value, TypeCombinator::intersect($valueType, new NonEmptyArrayType()), TypeSpecifierContext::createTrue(), $scope), |
| 63 | + ); |
| 64 | + } |
| 65 | + |
| 66 | + public function setTypeSpecifier(TypeSpecifier $typeSpecifier): void |
| 67 | + { |
| 68 | + $this->typeSpecifier = $typeSpecifier; |
| 69 | + } |
| 70 | + |
| 71 | +} |
0 commit comments