| 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: | |
| 18: | |
| 19: | |
| 20: | |
| 21: | |
| 22: | |
| 23: | |
| 24: | |
| 25: | |
| 26: | |
| 27: | |
| 28: | |
| 29: | |
| 30: | |
| 31: | |
| 32: | |
| 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: | |
| 42: | |
| 43: | private function __construct(private array $asserts) |
| 44: | { |
| 45: | } |
| 46: | |
| 47: | |
| 48: | public function getAll(): array |
| 49: | { |
| 50: | return $this->asserts; |
| 51: | } |
| 52: | |
| 53: | |
| 54: | |
| 55: | |
| 56: | |
| 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: | |
| 65: | |
| 66: | |
| 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: | |
| 85: | |
| 86: | |
| 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: | |
| 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: | |
| 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: | |
| 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: | |
| 203: | |
| 204: | public static function createFromAssertTags(array $assertTags): self |
| 205: | { |
| 206: | return self::create($assertTags); |
| 207: | } |
| 208: | |
| 209: | } |
| 210: | |