-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClassNameMustBeFirstInClassDocumentationRule.php
74 lines (65 loc) · 1.88 KB
/
ClassNameMustBeFirstInClassDocumentationRule.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<?php
declare(strict_types=1);
namespace Oneserv\PHPStan\Rules\Classes;
use Oneserv\PHPStan\Services\ClassHelper;
use Oneserv\PHPStan\Services\DocCommentHelper;
use PhpParser\Node;
use PhpParser\Node\Stmt\Class_;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\ShouldNotHappenException;
/**
* Class ClassNameMustBeFirstInClassDocumentationRule
*
* @see \Tests\Oneserv\PHPStan\Rules\Classes\ClassNameMustBeFirstInClassDocumentationRuleTest
* @implements Rule<Class_>
*/
class ClassNameMustBeFirstInClassDocumentationRule implements Rule
{
/**
* ClassNameMustBeFirstInClassDocumentationRule constructor.
*
* @param ClassHelper $classHelper
* @param DocCommentHelper $docCommentHelper
*/
public function __construct(
private readonly ClassHelper $classHelper,
private readonly DocCommentHelper $docCommentHelper
) {
}
/**
* @inheritDoc
*/
public function getNodeType(): string
{
return Class_::class;
}
/**
* @inheritDoc
* @throws ShouldNotHappenException
*/
public function processNode(Node $node, Scope $scope): array
{
/** @var Class_ $node */
if (!$this->classHelper->shouldClassBeAnalysed($node)) {
return [];
}
$className = $node->name;
if ($className === null) {
throw new ShouldNotHappenException();
}
$className = $className->name;
$docComment = (string)$node->getDocComment();
$docComment = $this->docCommentHelper->cleanUpDocComment($docComment);
if (!str_starts_with($docComment, "/***Class$className")) {
return [
sprintf(
"The doc comment of class %s must start with \"Class %s\".",
$className,
$className,
),
];
}
return [];
}
}