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:
9: /** @api */
10: #[ReferencedByTurboExtension(key: 'assertTag')]
11: final class AssertTag implements TypedTag
12: {
13:
14: public const NULL = '';
15: public const IF_TRUE = 'true';
16: public const IF_FALSE = 'false';
17:
18: private ?Type $originalType = null;
19:
20: /**
21: * @param self::NULL|self::IF_TRUE|self::IF_FALSE $if
22: */
23: public function __construct(private string $if, private Type $type, private AssertTagParameter $parameter, private bool $negated, private bool $equality, private bool $isExplicit)
24: {
25: }
26:
27: /**
28: * @return self::NULL|self::IF_TRUE|self::IF_FALSE
29: */
30: public function getIf(): string
31: {
32: return $this->if;
33: }
34:
35: public function getType(): Type
36: {
37: return $this->type;
38: }
39:
40: public function getOriginalType(): Type
41: {
42: return $this->originalType ??= $this->type;
43: }
44:
45: public function getParameter(): AssertTagParameter
46: {
47: return $this->parameter;
48: }
49:
50: public function isNegated(): bool
51: {
52: return $this->negated;
53: }
54:
55: public function isEquality(): bool
56: {
57: return $this->equality;
58: }
59:
60: /**
61: * @return static
62: */
63: public function withType(Type $type): TypedTag
64: {
65: $tag = new self($this->if, $type, $this->parameter, $this->negated, $this->equality, $this->isExplicit);
66: $tag->originalType = $this->getOriginalType();
67: return $tag;
68: }
69:
70: public function withParameter(AssertTagParameter $parameter): self
71: {
72: $tag = new self($this->if, $this->type, $parameter, $this->negated, $this->equality, $this->isExplicit);
73: $tag->originalType = $this->getOriginalType();
74: return $tag;
75: }
76:
77: public function negate(): self
78: {
79: if ($this->isEquality()) {
80: throw new ShouldNotHappenException();
81: }
82:
83: $tag = new self($this->if, $this->type, $this->parameter, !$this->negated, $this->equality, $this->isExplicit);
84: $tag->originalType = $this->getOriginalType();
85: return $tag;
86: }
87:
88: public function isExplicit(): bool
89: {
90: return $this->isExplicit;
91: }
92:
93: public function toImplicit(): self
94: {
95: return new self($this->if, $this->type, $this->parameter, $this->negated, $this->equality, false);
96: }
97:
98: }
99: