-
Notifications
You must be signed in to change notification settings - Fork 428
/
IsAFunctionTypeSpecifyingHelper.php
62 lines (54 loc) · 1.45 KB
/
IsAFunctionTypeSpecifyingHelper.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 PHPStan\Type\ClassStringType;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\Generic\GenericClassStringType;
use PHPStan\Type\IntersectionType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\ObjectWithoutClassType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use PHPStan\Type\TypeTraverser;
use PHPStan\Type\UnionType;
final class IsAFunctionTypeSpecifyingHelper
{
public function determineType(
Type $classType,
bool $allowString,
): Type
{
return TypeTraverser::map(
$classType,
static function (Type $type, callable $traverse) use ($allowString): Type {
if ($type instanceof UnionType || $type instanceof IntersectionType) {
return $traverse($type);
}
if ($type instanceof ConstantStringType) {
if ($allowString) {
return TypeCombinator::union(
new ObjectType($type->getValue()),
new GenericClassStringType(new ObjectType($type->getValue())),
);
}
return new ObjectType($type->getValue());
}
if ($type instanceof GenericClassStringType) {
if ($allowString) {
return TypeCombinator::union(
$type->getGenericType(),
$type,
);
}
return $type->getGenericType();
}
if ($allowString) {
return TypeCombinator::union(
new ObjectWithoutClassType(),
new ClassStringType(),
);
}
return new ObjectWithoutClassType();
},
);
}
}