forked from phpstan/phpstan-src
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOffsetAccessType.php
More file actions
117 lines (94 loc) · 2.45 KB
/
Copy pathOffsetAccessType.php
File metadata and controls
117 lines (94 loc) · 2.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
<?php declare(strict_types = 1);
namespace PHPStan\Type;
use PHPStan\PhpDocParser\Ast\Type\OffsetAccessTypeNode;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\PhpDocParser\Printer\Printer;
use PHPStan\Type\Generic\TemplateTypeVariance;
use PHPStan\Type\Traits\LateResolvableTypeTrait;
use PHPStan\Type\Traits\NonGeneralizableTypeTrait;
use function array_merge;
/** @api */
final class OffsetAccessType implements CompoundType, LateResolvableType
{
use LateResolvableTypeTrait;
use NonGeneralizableTypeTrait;
public function __construct(
private Type $type,
private Type $offset,
)
{
}
public function getReferencedClasses(): array
{
return array_merge(
$this->type->getReferencedClasses(),
$this->offset->getReferencedClasses(),
);
}
public function getObjectClassNames(): array
{
return [];
}
public function getObjectClassReflections(): array
{
return [];
}
public function getReferencedTemplateTypes(TemplateTypeVariance $positionVariance): array
{
return array_merge(
$this->type->getReferencedTemplateTypes($positionVariance),
$this->offset->getReferencedTemplateTypes($positionVariance),
);
}
public function equals(Type $type): bool
{
return $type instanceof self
&& $this->type->equals($type->type)
&& $this->offset->equals($type->offset);
}
public function describe(VerbosityLevel $level): string
{
$printer = new Printer();
return $printer->print($this->toPhpDocNode());
}
public function isResolvable(): bool
{
return !TypeUtils::containsTemplateType($this->type)
&& !TypeUtils::containsTemplateType($this->offset);
}
protected function getResult(): Type
{
return $this->type->getOffsetValueType($this->offset);
}
/**
* @param callable(Type): Type $cb
*/
public function traverse(callable $cb): Type
{
$type = $cb($this->type);
$offset = $cb($this->offset);
if ($this->type === $type && $this->offset === $offset) {
return $this;
}
return new self($type, $offset);
}
public function traverseSimultaneously(Type $right, callable $cb): Type
{
if (!$right instanceof self) {
return $this;
}
$type = $cb($this->type, $right->type);
$offset = $cb($this->offset, $right->offset);
if ($this->type === $type && $this->offset === $offset) {
return $this;
}
return new self($type, $offset);
}
public function toPhpDocNode(): TypeNode
{
return new OffsetAccessTypeNode(
$this->type->toPhpDocNode(),
$this->offset->toPhpDocNode(),
);
}
}