1: <?php declare(strict_types = 1);
2:
3: namespace PHPStan\Type;
4:
5: use PHPStan\Type\Generic\TemplateTypeVariance;
6: use PHPStan\Type\Traits\LateResolvableTypeTrait;
7: use PHPStan\Type\Traits\NonGeneralizableTypeTrait;
8: use function array_merge;
9: use function sprintf;
10:
11: /** @api */
12: final class OffsetAccessType implements CompoundType, LateResolvableType
13: {
14:
15: use LateResolvableTypeTrait;
16: use NonGeneralizableTypeTrait;
17:
18: public function __construct(
19: private Type $type,
20: private Type $offset,
21: )
22: {
23: }
24:
25: public function getReferencedClasses(): array
26: {
27: return array_merge(
28: $this->type->getReferencedClasses(),
29: $this->offset->getReferencedClasses(),
30: );
31: }
32:
33: public function getReferencedTemplateTypes(TemplateTypeVariance $positionVariance): array
34: {
35: return array_merge(
36: $this->type->getReferencedTemplateTypes($positionVariance),
37: $this->offset->getReferencedTemplateTypes($positionVariance),
38: );
39: }
40:
41: public function equals(Type $type): bool
42: {
43: return $type instanceof self
44: && $this->type->equals($type->type)
45: && $this->offset->equals($type->offset);
46: }
47:
48: public function describe(VerbosityLevel $level): string
49: {
50: return sprintf(
51: '%s[%s]',
52: $this->type->describe($level),
53: $this->offset->describe($level),
54: );
55: }
56:
57: public function isResolvable(): bool
58: {
59: return !TypeUtils::containsTemplateType($this->type)
60: && !TypeUtils::containsTemplateType($this->offset);
61: }
62:
63: protected function getResult(): Type
64: {
65: return $this->type->getOffsetValueType($this->offset);
66: }
67:
68: /**
69: * @param callable(Type): Type $cb
70: */
71: public function traverse(callable $cb): Type
72: {
73: $type = $cb($this->type);
74: $offset = $cb($this->offset);
75:
76: if ($this->type === $type && $this->offset === $offset) {
77: return $this;
78: }
79:
80: return new OffsetAccessType($type, $offset);
81: }
82:
83: /**
84: * @param mixed[] $properties
85: */
86: public static function __set_state(array $properties): Type
87: {
88: return new self(
89: $properties['type'],
90: $properties['offset'],
91: );
92: }
93:
94: }
95: