1: <?php declare(strict_types = 1);
2:
3: namespace PHPStan\Reflection;
4:
5: use PHPStan\PhpDoc\ResolvedPhpDocBlock;
6: use PHPStan\PhpDoc\Tag\AssertTag;
7: use PHPStan\Turbo\ShadowedByTurboExtension;
8: use PHPStan\Type\Type;
9: use PHPStan\Type\TypeCombinator;
10: use function array_filter;
11: use function array_map;
12: use function array_merge;
13: use function count;
14: use function sprintf;
15:
16: /**
17: * Collection of @phpstan-assert annotations on a function or method.
18: *
19: * PHPStan supports type assertions via PHPDoc annotations:
20: * - `@phpstan-assert Type $param` — narrows the parameter type unconditionally
21: * - `@phpstan-assert-if-true Type $param` — narrows when the method returns true
22: * - `@phpstan-assert-if-false Type $param` — narrows when the method returns false
23: *
24: * This class collects all such assertions and provides methods to retrieve them
25: * by condition type. It also handles negation: an `@phpstan-assert-if-true` assertion
26: * is automatically negated and included in the `getAssertsIfFalse()` result
27: * (several of them only when they are all about the same expression, see
28: * AssertTag::negateConjunction()).
29: *
30: * Returned by ExtendedMethodReflection::getAsserts() and FunctionReflection::getAsserts().
31: *
32: * @api
33: */
34: #[ShadowedByTurboExtension(implementation: __DIR__ . '/../../turbo-ext/src/Assertions.cpp')]
35: final class Assertions
36: {
37:
38: private static ?self $empty = null;
39:
40: /**
41: * @param AssertTag[] $asserts
42: */
43: private function __construct(private array $asserts)
44: {
45: }
46:
47: /** @return AssertTag[] */
48: public function getAll(): array
49: {
50: return $this->asserts;
51: }
52:
53: /**
54: * Unconditional assertions — narrow parameter types regardless of the method's return value.
55: *
56: * @return AssertTag[]
57: */
58: public function getAsserts(): array
59: {
60: return array_filter($this->asserts, static fn (AssertTag $assert) => $assert->getIf() === AssertTag::NULL);
61: }
62:
63: /**
64: * Includes @phpstan-assert-if-true tags and the negation of the @phpstan-assert-if-false tags.
65: *
66: * @return AssertTag[]
67: */
68: public function getAssertsIfTrue(): array
69: {
70: $asserts = [];
71: $oppositeAsserts = [];
72: foreach ($this->asserts as $assert) {
73: if ($assert->getIf() === AssertTag::IF_TRUE) {
74: $asserts[] = $assert;
75: } elseif ($assert->getIf() === AssertTag::IF_FALSE && !$assert->isEquality()) {
76: $oppositeAsserts[] = $assert;
77: }
78: }
79:
80: return array_merge($asserts, AssertTag::negateConjunction($oppositeAsserts));
81: }
82:
83: /**
84: * Includes @phpstan-assert-if-false tags and the negation of the @phpstan-assert-if-true tags.
85: *
86: * @return AssertTag[]
87: */
88: public function getAssertsIfFalse(): array
89: {
90: $asserts = [];
91: $oppositeAsserts = [];
92: foreach ($this->asserts as $assert) {
93: if ($assert->getIf() === AssertTag::IF_FALSE) {
94: $asserts[] = $assert;
95: } elseif ($assert->getIf() === AssertTag::IF_TRUE && !$assert->isEquality()) {
96: $oppositeAsserts[] = $assert;
97: }
98: }
99:
100: return array_merge($asserts, AssertTag::negateConjunction($oppositeAsserts));
101: }
102:
103: /** @param callable(Type): Type $callable */
104: public function mapTypes(callable $callable): self
105: {
106: $assertTagsCallback = static fn (AssertTag $tag): AssertTag => $tag->withType($callable($tag->getType()));
107:
108: return self::create(array_map($assertTagsCallback, $this->asserts));
109: }
110:
111: /**
112: * @deprecated use union() or intersect() instead
113: */
114: public function intersectWith(Assertions $other): self
115: {
116: return $this->union($other);
117: }
118:
119: public function union(Assertions $other): self
120: {
121: if ($this === self::$empty) {
122: return $other;
123: }
124: if ($other === self::$empty) {
125: return $this;
126: }
127:
128: return self::create(array_merge($this->getAll(), $other->getAll()));
129: }
130:
131: public function intersect(Assertions $other): self
132: {
133: if ($this === self::$empty) {
134: return $other;
135: }
136: if ($other === self::$empty) {
137: return $this;
138: }
139:
140: $otherAsserts = $other->getAll();
141: $thisAsserts = $this->getAll();
142:
143: $merged = [];
144: foreach ($thisAsserts as $thisAssert) {
145: $key = self::getAssertKey($thisAssert);
146:
147: foreach ($otherAsserts as $otherAssert) {
148: if (self::getAssertKey($otherAssert) !== $key) {
149: continue;
150: }
151:
152: $merged[] = $thisAssert->withType(TypeCombinator::union($thisAssert->getType(), $otherAssert->getType()));
153: }
154: }
155:
156: return self::create($merged);
157: }
158:
159: private static function getAssertKey(AssertTag $assert): string
160: {
161: return sprintf(
162: '%s-%s-%s',
163: $assert->getParameter()->describe(),
164: $assert->getIf(),
165: $assert->isNegated() ? '1' : '0',
166: );
167: }
168:
169: /**
170: * @param AssertTag[] $asserts
171: */
172: private static function create(array $asserts): self
173: {
174: if (count($asserts) === 0) {
175: return self::createEmpty();
176: }
177: return new self($asserts);
178: }
179:
180: public static function createEmpty(): self
181: {
182: $empty = self::$empty;
183:
184: if ($empty !== null) {
185: return $empty;
186: }
187:
188: $empty = new self([]);
189: self::$empty = $empty;
190:
191: return $empty;
192: }
193:
194: public static function createFromResolvedPhpDocBlock(ResolvedPhpDocBlock $phpDocBlock): self
195: {
196: $tags = $phpDocBlock->getAssertTags();
197:
198: return self::create($tags);
199: }
200:
201: /**
202: * @param AssertTag[] $assertTags
203: */
204: public static function createFromAssertTags(array $assertTags): self
205: {
206: return self::create($assertTags);
207: }
208:
209: }
210: