1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Gskema\TypeSniff\Core\Type; |
4
|
|
|
|
5
|
|
|
use Gskema\TypeSniff\Core\Type\Common\ArrayType; |
6
|
|
|
use Gskema\TypeSniff\Core\Type\Common\UndefinedType; |
7
|
|
|
use Gskema\TypeSniff\Core\Type\Declaration\NullableType; |
8
|
|
|
use Gskema\TypeSniff\Core\Type\DocBlock\CompoundType; |
9
|
|
|
use Gskema\TypeSniff\Core\Type\DocBlock\TypedArrayType; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @see TypeHelperTest |
13
|
|
|
*/ |
14
|
|
|
class TypeHelper |
15
|
|
|
{ |
16
|
7 |
|
public static function containsType(?TypeInterface $type, string $typeClassName): bool |
17
|
|
|
{ |
18
|
7 |
|
return is_a($type, $typeClassName) |
19
|
7 |
|
|| ($type instanceof CompoundType && $type->containsType($typeClassName)) |
20
|
7 |
|
|| ($type instanceof NullableType && $type->containsType($typeClassName)); |
21
|
|
|
} |
22
|
|
|
|
23
|
10 |
|
public static function getFakeTypedArrayType(?TypeInterface $type): ?TypedArrayType |
24
|
|
|
{ |
25
|
10 |
|
if (null === $type) { |
26
|
1 |
|
return null; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** @var TypedArrayType[] $typedArrayTypes */ |
30
|
9 |
|
$typedArrayTypes = []; |
31
|
9 |
|
if ($type instanceof CompoundType) { |
32
|
6 |
|
$typedArrayTypes = $type->getType(TypedArrayType::class); |
33
|
9 |
|
} elseif ($type instanceof TypedArrayType) { |
34
|
7 |
|
$typedArrayTypes = [$type]; |
35
|
|
|
} |
36
|
|
|
|
37
|
9 |
|
foreach ($typedArrayTypes as $typedArrayType) { |
38
|
7 |
|
if ($typedArrayType->getType() instanceof ArrayType |
39
|
7 |
|
|| $typedArrayType->getType() instanceof UndefinedType |
40
|
|
|
) { |
41
|
7 |
|
return $typedArrayType; // e.g. array[][] or [][] |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
|
45
|
8 |
|
return null; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param TypeInterface[] $types |
50
|
|
|
* |
51
|
|
|
* @return string|null |
52
|
|
|
*/ |
53
|
5 |
|
public static function listRawTypes(array $types): ?string |
54
|
|
|
{ |
55
|
5 |
|
$rawTypes = []; |
56
|
5 |
|
foreach ($types as $type) { |
57
|
5 |
|
$rawTypes[] = $type->toString(); |
58
|
|
|
} |
59
|
|
|
|
60
|
5 |
|
return $rawTypes ? implode(', ', $rawTypes) : null; |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|