-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClassNameMustBeFirstInConstructMethodDocumentationRule.php
75 lines (65 loc) · 2.05 KB
/
ClassNameMustBeFirstInConstructMethodDocumentationRule.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
75
<?php
declare(strict_types=1);
namespace Oneserv\PHPStan\Rules\Methods;
use Oneserv\PHPStan\Services\ClassHelper;
use Oneserv\PHPStan\Services\DocCommentHelper;
use PhpParser\Node;
use PhpParser\Node\Stmt\ClassMethod;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\ShouldNotHappenException;
/**
* Class ClassNameMustBeFirstInConstructMethodDocumentationRule
*
* @implements Rule<ClassMethod>
* @see \Tests\Oneserv\PHPStan\Rules\Methods\ClassNameMustBeFirstInConstructMethodDocumentationRuleTest
*/
class ClassNameMustBeFirstInConstructMethodDocumentationRule implements Rule
{
/**
* ClassNameMustBeFirstInConstructMethodDocumentationRule constructor.
*
* @param ClassHelper $classHelper
* @param DocCommentHelper $docCommentHelper
*/
public function __construct(
private readonly ClassHelper $classHelper,
private readonly DocCommentHelper $docCommentHelper
) {
}
/**
* @inheritDoc
*/
public function getNodeType(): string
{
return ClassMethod::class;
}
/**
* @inheritDoc
* @throws ShouldNotHappenException
*/
public function processNode(Node $node, Scope $scope): array
{
/** @var ClassMethod $node */
if ($node->name->name !== '__construct') {
return [];
}
$classReflection = $scope->getClassReflection();
if ($classReflection === null) {
throw new ShouldNotHappenException();
}
$className = $this->classHelper->getClassNameFromFqn($classReflection->getName());
$docComment = (string)$node->getDocComment();
$docComment = $this->docCommentHelper->cleanUpDocComment($docComment);
if (!str_starts_with($docComment, '/***' . $className . 'constructor.')) {
return [
sprintf(
'The doc comment of the __construct method of class %s must start with "%s constructor.".',
$className,
$className,
),
];
}
return [];
}
}