-
Notifications
You must be signed in to change notification settings - Fork 574
Expand file tree
/
Copy pathTemplateConstantArrayType.php
More file actions
76 lines (61 loc) · 1.93 KB
/
TemplateConstantArrayType.php
File metadata and controls
76 lines (61 loc) · 1.93 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
<?php declare(strict_types = 1);
namespace PHPStan\Type\Generic;
use PHPStan\Type\Constant\ConstantArrayType;
use PHPStan\Type\Traits\UndecidedComparisonCompoundTypeTrait;
use PHPStan\Type\Type;
/** @api */
final class TemplateConstantArrayType extends ConstantArrayType implements TemplateType
{
/** @use TemplateTypeTrait<ConstantArrayType> */
use TemplateTypeTrait;
use UndecidedComparisonCompoundTypeTrait;
/**
* @param non-empty-string $name
*/
public function __construct(
TemplateTypeScope $scope,
TemplateTypeStrategy $templateTypeStrategy,
TemplateTypeVariance $templateTypeVariance,
string $name,
ConstantArrayType $bound,
?Type $default,
)
{
parent::__construct($bound->getKeyTypes(), $bound->getValueTypes(), $bound->getNextAutoIndexes(), $bound->getOptionalKeys(), $bound->isList());
$this->scope = $scope;
$this->strategy = $templateTypeStrategy;
$this->variance = $templateTypeVariance;
$this->name = $name;
$this->bound = $bound;
$this->default = $default;
}
public function setOffsetValueType(?Type $offsetType, Type $valueType, bool $unionValues = true): Type
{
if ($this->isOffsetWithinBound($offsetType, $valueType)) {
return $this;
}
return parent::setOffsetValueType($offsetType, $valueType, $unionValues);
}
public function setExistingOffsetValueType(Type $offsetType, Type $valueType): Type
{
if ($this->isOffsetWithinBound($offsetType, $valueType)) {
return $this;
}
return parent::setExistingOffsetValueType($offsetType, $valueType);
}
private function isOffsetWithinBound(?Type $offsetType, Type $valueType): bool
{
if ($offsetType === null) {
return false;
}
$boundKeyTypes = $this->bound->getKeyTypes();
$boundValueTypes = $this->bound->getValueTypes();
foreach ($boundKeyTypes as $i => $boundKeyType) {
if (!$offsetType->equals($boundKeyType)) {
continue;
}
return $boundValueTypes[$i]->accepts($valueType, true)->yes();
}
return false;
}
}