c05891c3f946bfbbd0b3f54db17b93e0f6d3890b
[GitHub/WoltLab/WCF.git] /
1 <?php
2
3 declare(strict_types=1);
4
5 namespace CuyZ\Valinor\QA\PHPStan\Extension;
6
7 use PhpParser\Node;
8 use PHPStan\Analyser\Scope;
9 use PHPStan\Node\InClassNode;
10 use PHPStan\Rules\Rule;
11 use PHPStan\Rules\RuleErrorBuilder;
12
13 use function str_starts_with;
14
15 /**
16 * @implements Rule<InClassNode>
17 */
18 final class ApiAndInternalAnnotationCheck implements Rule
19 {
20 public function getNodeType(): string
21 {
22 return InClassNode::class;
23 }
24
25 public function processNode(Node $node, Scope $scope): array
26 {
27 $reflection = $scope->getClassReflection();
28
29 if (! $reflection) {
30 return [];
31 }
32
33 if ($reflection->isAnonymous()) {
34 return [];
35 }
36
37 if (str_starts_with($reflection->getName(), 'CuyZ\Valinor\Tests')
38 || str_starts_with($reflection->getName(), 'SimpleNamespace')
39 ) {
40 return [];
41 }
42
43 if (str_starts_with($reflection->getName(), 'CuyZ\Valinor\QA')) {
44 return [];
45 }
46
47 if (! preg_match('/@(api|internal)\s+/', $reflection->getResolvedPhpDoc()?->getPhpDocString() ?? '')) {
48 return [
49 RuleErrorBuilder::message(
50 'Missing annotation `@api` or `@internal`.'
51 )->build(),
52 ];
53 }
54
55 return [];
56 }
57 }