1: | <?php declare(strict_types = 1); |
2: | |
3: | namespace PHPStan\PhpDoc\Tag; |
4: | |
5: | use PHPStan\ShouldNotHappenException; |
6: | use PHPStan\Type\Type; |
7: | |
8: | |
9: | final class AssertTag implements TypedTag |
10: | { |
11: | |
12: | public const NULL = ''; |
13: | public const IF_TRUE = 'true'; |
14: | public const IF_FALSE = 'false'; |
15: | |
16: | private ?Type $originalType = null; |
17: | |
18: | |
19: | |
20: | |
21: | public function __construct(private string $if, private Type $type, private AssertTagParameter $parameter, private bool $negated, private bool $equality) |
22: | { |
23: | } |
24: | |
25: | |
26: | |
27: | |
28: | public function getIf(): string |
29: | { |
30: | return $this->if; |
31: | } |
32: | |
33: | public function getType(): Type |
34: | { |
35: | return $this->type; |
36: | } |
37: | |
38: | public function getOriginalType(): Type |
39: | { |
40: | return $this->originalType ??= $this->type; |
41: | } |
42: | |
43: | public function getParameter(): AssertTagParameter |
44: | { |
45: | return $this->parameter; |
46: | } |
47: | |
48: | public function isNegated(): bool |
49: | { |
50: | return $this->negated; |
51: | } |
52: | |
53: | public function isEquality(): bool |
54: | { |
55: | return $this->equality; |
56: | } |
57: | |
58: | |
59: | |
60: | |
61: | public function withType(Type $type): TypedTag |
62: | { |
63: | $tag = new self($this->if, $type, $this->parameter, $this->negated, $this->equality); |
64: | $tag->originalType = $this->getOriginalType(); |
65: | return $tag; |
66: | } |
67: | |
68: | public function withParameter(AssertTagParameter $parameter): self |
69: | { |
70: | $tag = new self($this->if, $this->type, $parameter, $this->negated, $this->equality); |
71: | $tag->originalType = $this->getOriginalType(); |
72: | return $tag; |
73: | } |
74: | |
75: | public function negate(): self |
76: | { |
77: | if ($this->isEquality()) { |
78: | throw new ShouldNotHappenException(); |
79: | } |
80: | |
81: | $tag = new self($this->if, $this->type, $this->parameter, !$this->negated, $this->equality); |
82: | $tag->originalType = $this->getOriginalType(); |
83: | return $tag; |
84: | } |
85: | |
86: | } |
87: | |