| 1: | <?php declare(strict_types = 1); |
| 2: | |
| 3: | namespace PHPStan\PhpDoc\Tag; |
| 4: | |
| 5: | use PHPStan\ShouldNotHappenException; |
| 6: | use PHPStan\Turbo\ReferencedByTurboExtension; |
| 7: | use PHPStan\Type\Type; |
| 8: | use PHPStan\Type\TypeCombinator; |
| 9: | use function array_map; |
| 10: | use function count; |
| 11: | |
| 12: | |
| 13: | #[ReferencedByTurboExtension(key: 'assertTag')] |
| 14: | final class AssertTag implements TypedTag |
| 15: | { |
| 16: | |
| 17: | public const NULL = ''; |
| 18: | public const IF_TRUE = 'true'; |
| 19: | public const IF_FALSE = 'false'; |
| 20: | |
| 21: | private ?Type $originalType = null; |
| 22: | |
| 23: | |
| 24: | |
| 25: | |
| 26: | public function __construct(private string $if, private Type $type, private AssertTagParameter $parameter, private bool $negated, private bool $equality, private bool $isExplicit) |
| 27: | { |
| 28: | } |
| 29: | |
| 30: | |
| 31: | |
| 32: | |
| 33: | public function getIf(): string |
| 34: | { |
| 35: | return $this->if; |
| 36: | } |
| 37: | |
| 38: | public function getType(): Type |
| 39: | { |
| 40: | return $this->type; |
| 41: | } |
| 42: | |
| 43: | public function getOriginalType(): Type |
| 44: | { |
| 45: | return $this->originalType ??= $this->type; |
| 46: | } |
| 47: | |
| 48: | public function getParameter(): AssertTagParameter |
| 49: | { |
| 50: | return $this->parameter; |
| 51: | } |
| 52: | |
| 53: | public function isNegated(): bool |
| 54: | { |
| 55: | return $this->negated; |
| 56: | } |
| 57: | |
| 58: | public function isEquality(): bool |
| 59: | { |
| 60: | return $this->equality; |
| 61: | } |
| 62: | |
| 63: | |
| 64: | |
| 65: | |
| 66: | public function withType(Type $type): TypedTag |
| 67: | { |
| 68: | $tag = new self($this->if, $type, $this->parameter, $this->negated, $this->equality, $this->isExplicit); |
| 69: | $tag->originalType = $this->getOriginalType(); |
| 70: | return $tag; |
| 71: | } |
| 72: | |
| 73: | public function withParameter(AssertTagParameter $parameter): self |
| 74: | { |
| 75: | $tag = new self($this->if, $this->type, $parameter, $this->negated, $this->equality, $this->isExplicit); |
| 76: | $tag->originalType = $this->getOriginalType(); |
| 77: | return $tag; |
| 78: | } |
| 79: | |
| 80: | public function negate(): self |
| 81: | { |
| 82: | if ($this->isEquality()) { |
| 83: | throw new ShouldNotHappenException(); |
| 84: | } |
| 85: | |
| 86: | $tag = new self($this->if, $this->type, $this->parameter, !$this->negated, $this->equality, $this->isExplicit); |
| 87: | $tag->originalType = $this->getOriginalType(); |
| 88: | return $tag; |
| 89: | } |
| 90: | |
| 91: | |
| 92: | |
| 93: | |
| 94: | |
| 95: | |
| 96: | |
| 97: | |
| 98: | |
| 99: | |
| 100: | public static function negateConjunction(array $tags): array |
| 101: | { |
| 102: | if (count($tags) <= 1) { |
| 103: | return array_map(static fn (self $tag): self => $tag->negate(), $tags); |
| 104: | } |
| 105: | |
| 106: | $subject = $tags[0]->parameter->describe(); |
| 107: | $types = []; |
| 108: | $originalTypes = []; |
| 109: | foreach ($tags as $tag) { |
| 110: | if ($tag->negated !== $tags[0]->negated || $tag->parameter->describe() !== $subject) { |
| 111: | return []; |
| 112: | } |
| 113: | |
| 114: | $types[] = $tag->type; |
| 115: | $originalTypes[] = $tag->getOriginalType(); |
| 116: | } |
| 117: | |
| 118: | |
| 119: | if ($tags[0]->negated) { |
| 120: | $type = TypeCombinator::union(...$types); |
| 121: | $originalType = TypeCombinator::union(...$originalTypes); |
| 122: | } else { |
| 123: | $type = TypeCombinator::intersect(...$types); |
| 124: | $originalType = TypeCombinator::intersect(...$originalTypes); |
| 125: | } |
| 126: | |
| 127: | $tag = new self($tags[0]->if, $type, $tags[0]->parameter, !$tags[0]->negated, false, $tags[0]->isExplicit); |
| 128: | $tag->originalType = $originalType; |
| 129: | |
| 130: | return [$tag]; |
| 131: | } |
| 132: | |
| 133: | public function isExplicit(): bool |
| 134: | { |
| 135: | return $this->isExplicit; |
| 136: | } |
| 137: | |
| 138: | public function toImplicit(): self |
| 139: | { |
| 140: | return new self($this->if, $this->type, $this->parameter, $this->negated, $this->equality, false); |
| 141: | } |
| 142: | |
| 143: | } |
| 144: | |