|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PHPStan\Type\Php; |
| 4 | + |
| 5 | +use PhpParser\Node\Arg; |
| 6 | +use PhpParser\Node\Expr\FuncCall; |
| 7 | +use PhpParser\Node\Name; |
| 8 | +use PHPStan\Analyser\Scope; |
| 9 | +use PHPStan\Reflection\FunctionReflection; |
| 10 | +use PHPStan\Reflection\ParametersAcceptorSelector; |
| 11 | +use PHPStan\Reflection\ReflectionProvider; |
| 12 | +use PHPStan\Type\ArrayType; |
| 13 | +use PHPStan\Type\Constant\ConstantArrayType; |
| 14 | +use PHPStan\Type\Constant\ConstantBooleanType; |
| 15 | +use PHPStan\Type\Constant\ConstantIntegerType; |
| 16 | +use PHPStan\Type\DynamicFunctionReturnTypeExtension; |
| 17 | +use PHPStan\Type\IntegerType; |
| 18 | +use PHPStan\Type\StringType; |
| 19 | +use PHPStan\Type\Type; |
| 20 | +use PHPStan\Type\TypeCombinator; |
| 21 | + |
| 22 | +class PregSplitDynamicReturnTypeExtension implements DynamicFunctionReturnTypeExtension |
| 23 | +{ |
| 24 | + |
| 25 | + private ReflectionProvider $reflectionProvider; |
| 26 | + |
| 27 | + public function __construct(ReflectionProvider $reflectionProvider) |
| 28 | + { |
| 29 | + $this->reflectionProvider = $reflectionProvider; |
| 30 | + } |
| 31 | + |
| 32 | + |
| 33 | + public function isFunctionSupported(FunctionReflection $functionReflection): bool |
| 34 | + { |
| 35 | + return strtolower($functionReflection->getName()) === 'preg_split'; |
| 36 | + } |
| 37 | + |
| 38 | + |
| 39 | + public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): Type |
| 40 | + { |
| 41 | + $flagsArg = $functionCall->args[3] ?? null; |
| 42 | + |
| 43 | + if ($this->hasFlag($this->getConstant('PREG_SPLIT_OFFSET_CAPTURE'), $flagsArg, $scope)) { |
| 44 | + $type = new ArrayType( |
| 45 | + new IntegerType(), |
| 46 | + new ConstantArrayType([new ConstantIntegerType(0), new ConstantIntegerType(1)], [new StringType(), new IntegerType()]) |
| 47 | + ); |
| 48 | + return TypeCombinator::union($type, new ConstantBooleanType(false)); |
| 49 | + } |
| 50 | + |
| 51 | + return ParametersAcceptorSelector::selectSingle($functionReflection->getVariants())->getReturnType(); |
| 52 | + } |
| 53 | + |
| 54 | + |
| 55 | + private function hasFlag(int $flag, ?Arg $expression, Scope $scope): bool |
| 56 | + { |
| 57 | + if ($expression === null) { |
| 58 | + return false; |
| 59 | + } |
| 60 | + |
| 61 | + $type = $scope->getType($expression->value); |
| 62 | + return $type instanceof ConstantIntegerType && ($type->getValue() & $flag) === $flag; |
| 63 | + } |
| 64 | + |
| 65 | + |
| 66 | + private function getConstant(string $constantName): int |
| 67 | + { |
| 68 | + $constant = $this->reflectionProvider->getConstant(new Name($constantName), null); |
| 69 | + $valueType = $constant->getValueType(); |
| 70 | + if (!$valueType instanceof ConstantIntegerType) { |
| 71 | + throw new \PHPStan\ShouldNotHappenException(sprintf('Constant %s does not have integer type.', $constantName)); |
| 72 | + } |
| 73 | + |
| 74 | + return $valueType->getValue(); |
| 75 | + } |
| 76 | + |
| 77 | +} |
0 commit comments