/
PathinfoFunctionDynamicReturnTypeExtension.php
62 lines (52 loc) · 1.91 KB
/
PathinfoFunctionDynamicReturnTypeExtension.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<?php declare(strict_types = 1);
namespace PHPStan\Type\Php;
use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Type\Constant\ConstantArrayTypeBuilder;
use PHPStan\Type\Constant\ConstantIntegerType;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\DynamicFunctionReturnTypeExtension;
use PHPStan\Type\StringType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use function count;
use const PATHINFO_ALL;
class PathinfoFunctionDynamicReturnTypeExtension implements DynamicFunctionReturnTypeExtension
{
public function isFunctionSupported(FunctionReflection $functionReflection): bool
{
return $functionReflection->getName() === 'pathinfo';
}
public function getTypeFromFunctionCall(
FunctionReflection $functionReflection,
Node\Expr\FuncCall $functionCall,
Scope $scope,
): ?Type
{
$argsCount = count($functionCall->getArgs());
if ($argsCount === 0) {
return null;
}
$pathType = $scope->getType($functionCall->getArgs()[0]->value);
$builder = ConstantArrayTypeBuilder::createEmpty();
$builder->setOffsetValueType(new ConstantStringType('dirname'), new StringType(), !$pathType->isNonEmptyString()->yes());
$builder->setOffsetValueType(new ConstantStringType('basename'), new StringType());
$builder->setOffsetValueType(new ConstantStringType('extension'), new StringType(), true);
$builder->setOffsetValueType(new ConstantStringType('filename'), new StringType());
$arrayType = $builder->getArray();
if ($argsCount === 1) {
return $arrayType;
} elseif ($argsCount >= 2) {
$flagsType = $scope->getType($functionCall->getArgs()[1]->value);
if ($flagsType instanceof ConstantIntegerType) {
if ($flagsType->getValue() === PATHINFO_ALL) {
return $arrayType;
}
} elseif ($flagsType->isInteger()->yes()) {
return TypeCombinator::union($arrayType, new StringType());
}
}
return new StringType();
}
}