|
20 | 20 | use PHPStan\Type\TypeTraverser; |
21 | 21 | use PHPStan\Type\VerbosityLevel; |
22 | 22 | use function array_key_exists; |
| 23 | +use function array_merge; |
| 24 | +use function implode; |
23 | 25 | use function in_array; |
24 | 26 | use function sprintf; |
25 | 27 |
|
@@ -180,64 +182,70 @@ public function check(ClassReflection $reflection, ClassLike $node): array |
180 | 182 | return $traverse($type); |
181 | 183 | }); |
182 | 184 |
|
183 | | - if ($this->absentTypeChecks && !$foundError) { |
184 | | - if ($this->checkMissingTypehints) { |
185 | | - foreach ($this->missingTypehintCheck->getIterableTypesWithMissingValueTypehint($resolvedType) as $iterableType) { |
186 | | - $iterableTypeDescription = $iterableType->describe(VerbosityLevel::typeOnly()); |
187 | | - $errors[] = RuleErrorBuilder::message(sprintf( |
188 | | - '%s %s has type alias %s with no value type specified in iterable type %s.', |
189 | | - $reflection->getClassTypeDescription(), |
190 | | - $reflection->getDisplayName(), |
191 | | - $aliasName, |
192 | | - $iterableTypeDescription, |
193 | | - )) |
194 | | - ->tip(MissingTypehintCheck::MISSING_ITERABLE_VALUE_TYPE_TIP) |
195 | | - ->identifier('missingType.iterableValue') |
196 | | - ->build(); |
197 | | - } |
198 | | - |
199 | | - foreach ($this->missingTypehintCheck->getNonGenericObjectTypesWithGenericClass($resolvedType) as [$name, $genericTypeNames]) { |
200 | | - $errors[] = RuleErrorBuilder::message(sprintf( |
201 | | - '%s %s has type alias %s with generic %s but does not specify its types: %s', |
202 | | - $reflection->getClassTypeDescription(), |
203 | | - $reflection->getDisplayName(), |
204 | | - $aliasName, |
205 | | - $name, |
206 | | - implode(', ', $genericTypeNames), |
207 | | - )) |
208 | | - ->identifier('missingType.generics') |
209 | | - ->build(); |
210 | | - } |
211 | | - |
212 | | - foreach ($this->missingTypehintCheck->getCallablesWithMissingSignature($resolvedType) as $callableType) { |
213 | | - $errors[] = RuleErrorBuilder::message(sprintf( |
214 | | - '%s %s has type alias %s with no signature specified for %s.', |
215 | | - $reflection->getClassTypeDescription(), |
216 | | - $reflection->getDisplayName(), |
217 | | - $aliasName, |
218 | | - $callableType->describe(VerbosityLevel::typeOnly()), |
219 | | - ))->identifier('missingType.callable')->build(); |
220 | | - } |
| 185 | + if ($foundError) { |
| 186 | + continue; |
| 187 | + } |
| 188 | + |
| 189 | + if (!$this->absentTypeChecks) { |
| 190 | + continue; |
| 191 | + } |
| 192 | + |
| 193 | + if ($this->checkMissingTypehints) { |
| 194 | + foreach ($this->missingTypehintCheck->getIterableTypesWithMissingValueTypehint($resolvedType) as $iterableType) { |
| 195 | + $iterableTypeDescription = $iterableType->describe(VerbosityLevel::typeOnly()); |
| 196 | + $errors[] = RuleErrorBuilder::message(sprintf( |
| 197 | + '%s %s has type alias %s with no value type specified in iterable type %s.', |
| 198 | + $reflection->getClassTypeDescription(), |
| 199 | + $reflection->getDisplayName(), |
| 200 | + $aliasName, |
| 201 | + $iterableTypeDescription, |
| 202 | + )) |
| 203 | + ->tip(MissingTypehintCheck::MISSING_ITERABLE_VALUE_TYPE_TIP) |
| 204 | + ->identifier('missingType.iterableValue') |
| 205 | + ->build(); |
221 | 206 | } |
222 | 207 |
|
223 | | - foreach ($resolvedType->getReferencedClasses() as $class) { |
224 | | - if (!$this->reflectionProvider->hasClass($class)) { |
225 | | - $errors[] = RuleErrorBuilder::message(sprintf('Type alias %s contains unknown class %s.', $aliasName, $class)) |
226 | | - ->identifier('class.notFound') |
227 | | - ->discoveringSymbolsTip() |
228 | | - ->build(); |
229 | | - } elseif ($this->reflectionProvider->getClass($class)->isTrait()) { |
230 | | - $errors[] = RuleErrorBuilder::message(sprintf('Type alias %s contains invalid type %s.', $aliasName, $class)) |
231 | | - ->identifier('typeAlias.trait') |
232 | | - ->build(); |
233 | | - } else { |
234 | | - $errors = array_merge( |
235 | | - $errors, |
236 | | - $this->classCheck->checkClassNames([ |
237 | | - new ClassNameNodePair($class, $node), |
238 | | - ], $this->checkClassCaseSensitivity), |
239 | | - ); |
240 | | - } |
| 208 | + foreach ($this->missingTypehintCheck->getNonGenericObjectTypesWithGenericClass($resolvedType) as [$name, $genericTypeNames]) { |
| 209 | + $errors[] = RuleErrorBuilder::message(sprintf( |
| 210 | + '%s %s has type alias %s with generic %s but does not specify its types: %s', |
| 211 | + $reflection->getClassTypeDescription(), |
| 212 | + $reflection->getDisplayName(), |
| 213 | + $aliasName, |
| 214 | + $name, |
| 215 | + implode(', ', $genericTypeNames), |
| 216 | + )) |
| 217 | + ->identifier('missingType.generics') |
| 218 | + ->build(); |
| 219 | + } |
| 220 | + |
| 221 | + foreach ($this->missingTypehintCheck->getCallablesWithMissingSignature($resolvedType) as $callableType) { |
| 222 | + $errors[] = RuleErrorBuilder::message(sprintf( |
| 223 | + '%s %s has type alias %s with no signature specified for %s.', |
| 224 | + $reflection->getClassTypeDescription(), |
| 225 | + $reflection->getDisplayName(), |
| 226 | + $aliasName, |
| 227 | + $callableType->describe(VerbosityLevel::typeOnly()), |
| 228 | + ))->identifier('missingType.callable')->build(); |
| 229 | + } |
| 230 | + } |
| 231 | + |
| 232 | + foreach ($resolvedType->getReferencedClasses() as $class) { |
| 233 | + if (!$this->reflectionProvider->hasClass($class)) { |
| 234 | + $errors[] = RuleErrorBuilder::message(sprintf('Type alias %s contains unknown class %s.', $aliasName, $class)) |
| 235 | + ->identifier('class.notFound') |
| 236 | + ->discoveringSymbolsTip() |
| 237 | + ->build(); |
| 238 | + } elseif ($this->reflectionProvider->getClass($class)->isTrait()) { |
| 239 | + $errors[] = RuleErrorBuilder::message(sprintf('Type alias %s contains invalid type %s.', $aliasName, $class)) |
| 240 | + ->identifier('typeAlias.trait') |
| 241 | + ->build(); |
| 242 | + } else { |
| 243 | + $errors = array_merge( |
| 244 | + $errors, |
| 245 | + $this->classCheck->checkClassNames([ |
| 246 | + new ClassNameNodePair($class, $node), |
| 247 | + ], $this->checkClassCaseSensitivity), |
| 248 | + ); |
241 | 249 | } |
242 | 250 | } |
243 | 251 | } |
|
0 commit comments