Repository navigation
Expand file tree
/
Copy pathValueOfType.php
More file actions
151 lines (121 loc) · 3.45 KB
/
Copy pathValueOfType.php
File metadata and controls
151 lines (121 loc) · 3.45 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<?php declare(strict_types = 1);
namespace PHPStan\Type;
use PHPStan\PhpDocParser\Ast\Type\GenericTypeNode;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\Turbo\ShadowedByTurboExtension;
use PHPStan\Type\Generic\TemplateType;
use PHPStan\Type\Generic\TemplateTypeVariance;
use PHPStan\Type\Traits\LateResolvableTypeTrait;
use PHPStan\Type\Traits\NonGeneralizableTypeTrait;
use function array_values;
use function count;
use function sprintf;
/** @api */
#[ShadowedByTurboExtension(implementation: __DIR__ . '/../../turbo-ext/src/ValueOfType.cpp')]
final class ValueOfType implements CompoundType, LateResolvableType
{
use LateResolvableTypeTrait;
use NonGeneralizableTypeTrait;
public function __construct(private Type $type)
{
}
public function getReferencedClasses(): array
{
return $this->type->getReferencedClasses();
}
public function getReferencedTemplateTypes(TemplateTypeVariance $positionVariance): array
{
return $this->type->getReferencedTemplateTypes($positionVariance);
}
public function equals(Type $type): bool
{
return $type instanceof self
&& $this->type->equals($type->type);
}
public function describe(VerbosityLevel $level): string
{
return sprintf('value-of<%s>', $this->type->describe($level));
}
public function isResolvable(): bool
{
return !TypeUtils::containsTemplateType($this->type);
}
protected function getResult(): Type
{
if ($this->type->isEnum()->yes()) {
$enumCases = $this->type->getEnumCases();
if (
$enumCases === []
&& $this->type instanceof TemplateType
&& (new ObjectType('BackedEnum'))->isSuperTypeOf($this->type->getBound())->yes()
) {
$backingTypes = [];
foreach ($this->type->getBound()->getObjectClassReflections() as $classReflection) {
$ancestor = $classReflection->getAncestorWithClassName('BackedEnum');
if ($ancestor === null) {
$backingTypes = [];
break;
}
$ancestorTypes = $ancestor->getActiveTemplateTypeMap()->getTypes();
if (count($ancestorTypes) !== 1) {
$backingTypes = [];
break;
}
$backingType = array_values($ancestorTypes)[0];
if ($backingType instanceof ErrorType) {
$backingTypes = [];
break;
}
$backingTypes[] = $backingType;
}
if ($backingTypes !== []) {
return TypeCombinator::union(...$backingTypes);
}
return new UnionType([new IntegerType(), new StringType()]);
}
$valueTypes = [];
foreach ($enumCases as $enumCase) {
$valueType = $enumCase->getBackingValueType();
if ($valueType === null) {
continue;
}
$valueTypes[] = $valueType;
}
if (count($valueTypes) === 0) {
return new NeverType();
}
if (count($valueTypes) === 1) {
return $valueTypes[0];
}
return new UnionType($valueTypes);
}
return $this->type->getIterableValueType();
}
/**
* @param callable(Type): Type $cb
*/
public function traverse(callable $cb): Type
{
$type = $cb($this->type);
if ($this->type === $type) {
return $this;
}
return new self($type);
}
public function traverseSimultaneously(Type $right, callable $cb): Type
{
if (!$right instanceof self) {
return $this;
}
$type = $cb($this->type, $right->type);
if ($this->type === $type) {
return $this;
}
return new self($type);
}
public function toPhpDocNode(): TypeNode
{
return new GenericTypeNode(new IdentifierTypeNode('value-of'), [$this->type->toPhpDocNode()]);
}
}