Repository navigation
Expand file tree
/
Copy pathAssertTag.php
More file actions
143 lines (118 loc) · 3.47 KB
/
Copy pathAssertTag.php
File metadata and controls
143 lines (118 loc) · 3.47 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
<?php declare(strict_types = 1);
namespace PHPStan\PhpDoc\Tag;
use PHPStan\ShouldNotHappenException;
use PHPStan\Turbo\ReferencedByTurboExtension;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use function array_map;
use function count;
/** @api */
#[ReferencedByTurboExtension(key: 'assertTag')]
final class AssertTag implements TypedTag
{
public const NULL = '';
public const IF_TRUE = 'true';
public const IF_FALSE = 'false';
private ?Type $originalType = null;
/**
* @param self::NULL|self::IF_TRUE|self::IF_FALSE $if
*/
public function __construct(private string $if, private Type $type, private AssertTagParameter $parameter, private bool $negated, private bool $equality, private bool $isExplicit)
{
}
/**
* @return self::NULL|self::IF_TRUE|self::IF_FALSE
*/
public function getIf(): string
{
return $this->if;
}
public function getType(): Type
{
return $this->type;
}
public function getOriginalType(): Type
{
return $this->originalType ??= $this->type;
}
public function getParameter(): AssertTagParameter
{
return $this->parameter;
}
public function isNegated(): bool
{
return $this->negated;
}
public function isEquality(): bool
{
return $this->equality;
}
/**
* @return static
*/
public function withType(Type $type): TypedTag
{
$tag = new self($this->if, $type, $this->parameter, $this->negated, $this->equality, $this->isExplicit);
$tag->originalType = $this->getOriginalType();
return $tag;
}
public function withParameter(AssertTagParameter $parameter): self
{
$tag = new self($this->if, $this->type, $parameter, $this->negated, $this->equality, $this->isExplicit);
$tag->originalType = $this->getOriginalType();
return $tag;
}
public function negate(): self
{
if ($this->isEquality()) {
throw new ShouldNotHappenException();
}
$tag = new self($this->if, $this->type, $this->parameter, !$this->negated, $this->equality, $this->isExplicit);
$tag->originalType = $this->getOriginalType();
return $tag;
}
/**
* What the other branch of a conditional assert says: the tags hold
* together, so when they don't, at least one of them is false.
* `-if-true A $x` with `-if-true B $y` means that a false result says
* $x is not A *or* $y is not B, which narrows neither of them.
*
* @param list<self> $tags
* @return list<self>
*/
public static function negateConjunction(array $tags): array
{
if (count($tags) <= 1) {
return array_map(static fn (self $tag): self => $tag->negate(), $tags);
}
$subject = $tags[0]->parameter->describe();
$types = [];
$originalTypes = [];
foreach ($tags as $tag) {
if ($tag->negated !== $tags[0]->negated || $tag->parameter->describe() !== $subject) {
return [];
}
$types[] = $tag->type;
$originalTypes[] = $tag->getOriginalType();
}
// not (A and B) is "not A&B"; not (not A and not B) is "A|B"
if ($tags[0]->negated) {
$type = TypeCombinator::union(...$types);
$originalType = TypeCombinator::union(...$originalTypes);
} else {
$type = TypeCombinator::intersect(...$types);
$originalType = TypeCombinator::intersect(...$originalTypes);
}
$tag = new self($tags[0]->if, $type, $tags[0]->parameter, !$tags[0]->negated, false, $tags[0]->isExplicit);
$tag->originalType = $originalType;
return [$tag];
}
public function isExplicit(): bool
{
return $this->isExplicit;
}
public function toImplicit(): self
{
return new self($this->if, $this->type, $this->parameter, $this->negated, $this->equality, false);
}
}