99use function implode ;
1010use function sprintf ;
1111
12- /** @api */
12+ /**
13+ * @api
14+ * @template-covariant T of RuleError
15+ */
1316class RuleErrorBuilder
1417{
1518
@@ -86,11 +89,18 @@ public static function getRuleErrorTypes(): array
8689 ];
8790 }
8891
92+ /**
93+ * @return self<RuleError>
94+ */
8995 public static function message (string $ message ): self
9096 {
9197 return new self ($ message );
9298 }
9399
100+ /**
101+ * @phpstan-this-out self<T&LineRuleError>
102+ * @return self<T&LineRuleError>
103+ */
94104 public function line (int $ line ): self
95105 {
96106 $ this ->properties ['line ' ] = $ line ;
@@ -99,6 +109,10 @@ public function line(int $line): self
99109 return $ this ;
100110 }
101111
112+ /**
113+ * @phpstan-this-out self<T&FileRuleError>
114+ * @return self<T&FileRuleError>
115+ */
102116 public function file (string $ file ): self
103117 {
104118 $ this ->properties ['file ' ] = $ file ;
@@ -107,6 +121,10 @@ public function file(string $file): self
107121 return $ this ;
108122 }
109123
124+ /**
125+ * @phpstan-this-out self<T&TipRuleError>
126+ * @return self<T&TipRuleError>
127+ */
110128 public function tip (string $ tip ): self
111129 {
112130 $ this ->tips = [$ tip ];
@@ -115,6 +133,10 @@ public function tip(string $tip): self
115133 return $ this ;
116134 }
117135
136+ /**
137+ * @phpstan-this-out self<T&TipRuleError>
138+ * @return self<T&TipRuleError>
139+ */
118140 public function addTip (string $ tip ): self
119141 {
120142 $ this ->tips [] = $ tip ;
@@ -123,13 +145,19 @@ public function addTip(string $tip): self
123145 return $ this ;
124146 }
125147
148+ /**
149+ * @phpstan-this-out self<T&TipRuleError>
150+ * @return self<T&TipRuleError>
151+ */
126152 public function discoveringSymbolsTip (): self
127153 {
128154 return $ this ->tip ('Learn more at https://phpstan.org/user-guide/discovering-symbols ' );
129155 }
130156
131157 /**
132158 * @param list<string> $reasons
159+ * @phpstan-this-out self<T&TipRuleError>
160+ * @return self<T&TipRuleError>
133161 */
134162 public function acceptsReasonsTip (array $ reasons ): self
135163 {
@@ -140,6 +168,10 @@ public function acceptsReasonsTip(array $reasons): self
140168 return $ this ;
141169 }
142170
171+ /**
172+ * @phpstan-this-out self<T&IdentifierRuleError>
173+ * @return self<T&IdentifierRuleError>
174+ */
143175 public function identifier (string $ identifier ): self
144176 {
145177 $ this ->properties ['identifier ' ] = $ identifier ;
@@ -150,6 +182,8 @@ public function identifier(string $identifier): self
150182
151183 /**
152184 * @param mixed[] $metadata
185+ * @phpstan-this-out self<T&MetadataRuleError>
186+ * @return self<T&MetadataRuleError>
153187 */
154188 public function metadata (array $ metadata ): self
155189 {
@@ -159,16 +193,23 @@ public function metadata(array $metadata): self
159193 return $ this ;
160194 }
161195
196+ /**
197+ * @phpstan-this-out self<T&NonIgnorableRuleError>
198+ * @return self<T&NonIgnorableRuleError>
199+ */
162200 public function nonIgnorable (): self
163201 {
164202 $ this ->type |= self ::TYPE_NON_IGNORABLE ;
165203
166204 return $ this ;
167205 }
168206
207+ /**
208+ * @return T
209+ */
169210 public function build (): RuleError
170211 {
171- /** @var class-string<RuleError > $className */
212+ /** @var class-string<T > $className */
172213 $ className = sprintf ('PHPStan \\Rules \\RuleErrors \\RuleError%d ' , $ this ->type );
173214 if (!class_exists ($ className )) {
174215 throw new ShouldNotHappenException (sprintf ('Class %s does not exist. ' , $ className ));
0 commit comments