forked from phpstan/phpstan-src
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssertions.php
More file actions
189 lines (160 loc) · 4.6 KB
/
Assertions.php
File metadata and controls
189 lines (160 loc) · 4.6 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<?php declare(strict_types = 1);
namespace PHPStan\Reflection;
use PHPStan\PhpDoc\ResolvedPhpDocBlock;
use PHPStan\PhpDoc\Tag\AssertTag;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use function array_filter;
use function array_map;
use function array_merge;
use function count;
use function sprintf;
/**
* Collection of @phpstan-assert annotations on a function or method.
*
* PHPStan supports type assertions via PHPDoc annotations:
* - `@phpstan-assert Type $param` — narrows the parameter type unconditionally
* - `@phpstan-assert-if-true Type $param` — narrows when the method returns true
* - `@phpstan-assert-if-false Type $param` — narrows when the method returns false
*
* This class collects all such assertions and provides methods to retrieve them
* by condition type. It also handles negation: an `@phpstan-assert-if-true` assertion
* is automatically negated and included in the `getAssertsIfFalse()` result.
*
* Returned by ExtendedMethodReflection::getAsserts() and FunctionReflection::getAsserts().
*
* @api
*/
final class Assertions
{
private static ?self $empty = null;
/**
* @param AssertTag[] $asserts
*/
private function __construct(private array $asserts)
{
}
/** @return AssertTag[] */
public function getAll(): array
{
return $this->asserts;
}
/**
* Unconditional assertions — narrow parameter types regardless of the method's return value.
*
* @return AssertTag[]
*/
public function getAsserts(): array
{
return array_filter($this->asserts, static fn (AssertTag $assert) => $assert->getIf() === AssertTag::NULL);
}
/**
* Includes @phpstan-assert-if-true tags and negated @phpstan-assert-if-false tags.
*
* @return AssertTag[]
*/
public function getAssertsIfTrue(): array
{
return array_merge(
array_filter($this->asserts, static fn (AssertTag $assert) => $assert->getIf() === AssertTag::IF_TRUE),
array_map(
static fn (AssertTag $assert) => $assert->negate(),
array_filter($this->asserts, static fn (AssertTag $assert) => $assert->getIf() === AssertTag::IF_FALSE && !$assert->isEquality()),
),
);
}
/**
* Includes @phpstan-assert-if-false tags and negated @phpstan-assert-if-true tags.
*
* @return AssertTag[]
*/
public function getAssertsIfFalse(): array
{
return array_merge(
array_filter($this->asserts, static fn (AssertTag $assert) => $assert->getIf() === AssertTag::IF_FALSE),
array_map(
static fn (AssertTag $assert) => $assert->negate(),
array_filter($this->asserts, static fn (AssertTag $assert) => $assert->getIf() === AssertTag::IF_TRUE && !$assert->isEquality()),
),
);
}
/** @param callable(Type): Type $callable */
public function mapTypes(callable $callable): self
{
$assertTagsCallback = static fn (AssertTag $tag): AssertTag => $tag->withType($callable($tag->getType()));
return self::create(array_map($assertTagsCallback, $this->asserts));
}
/**
* @deprecated use union() or intersect() instead
*/
public function intersectWith(Assertions $other): self
{
return $this->union($other);
}
public function union(Assertions $other): self
{
if ($this === self::$empty) {
return $other;
}
if ($other === self::$empty) {
return $this;
}
return self::create(array_merge($this->getAll(), $other->getAll()));
}
public function intersect(Assertions $other): self
{
if ($this === self::$empty) {
return $other;
}
if ($other === self::$empty) {
return $this;
}
$otherAsserts = $other->getAll();
$thisAsserts = $this->getAll();
$merged = [];
foreach ($thisAsserts as $thisAssert) {
$key = self::getAssertKey($thisAssert);
foreach ($otherAsserts as $otherAssert) {
if (self::getAssertKey($otherAssert) !== $key) {
continue;
}
$merged[] = $thisAssert->withType(TypeCombinator::union($thisAssert->getType(), $otherAssert->getType()));
}
}
return self::create($merged);
}
private static function getAssertKey(AssertTag $assert): string
{
return sprintf(
'%s-%s-%s',
$assert->getParameter()->describe(),
$assert->getIf(),
$assert->isNegated() ? '1' : '0',
);
}
/**
* @param AssertTag[] $asserts
*/
private static function create(array $asserts): self
{
if (count($asserts) === 0) {
return self::createEmpty();
}
return new self($asserts);
}
public static function createEmpty(): self
{
$empty = self::$empty;
if ($empty !== null) {
return $empty;
}
$empty = new self([]);
self::$empty = $empty;
return $empty;
}
public static function createFromResolvedPhpDocBlock(ResolvedPhpDocBlock $phpDocBlock): self
{
$tags = $phpDocBlock->getAssertTags();
return self::create($tags);
}
}