-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathFinalRule.php
162 lines (137 loc) · 4.51 KB
/
FinalRule.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<?php
declare(strict_types=1);
/**
* Copyright (c) 2018-2025 Andreas Möller
*
* For the full copyright and license information, please view
* the LICENSE.md file that was distributed with this source code.
*
* @see https://github.com/ergebnis/phpstan-rules
*/
namespace Ergebnis\PHPStan\Rules\Classes;
use Doctrine\ORM;
use Ergebnis\PHPStan\Rules\ErrorIdentifier;
use PhpParser\Comment;
use PhpParser\Node;
use PHPStan\Analyser;
use PHPStan\Rules;
/**
* @implements Rules\Rule<Node\Stmt\Class_>
*/
final class FinalRule implements Rules\Rule
{
/**
* @var list<string>
*/
private static array $whitelistedAnnotations = [
'Entity',
'ORM\Entity',
'ORM\Mapping\Entity',
];
/**
* @var list<class-string>
*/
private static array $whitelistedAttributes = [
ORM\Mapping\Entity::class,
];
private bool $allowAbstractClasses;
/**
* @var list<string>
*/
private array $classesNotRequiredToBeAbstractOrFinal;
private string $errorMessageTemplate = 'Class %s is not final.';
/**
* @param list<class-string> $classesNotRequiredToBeAbstractOrFinal
*/
public function __construct(
bool $allowAbstractClasses,
array $classesNotRequiredToBeAbstractOrFinal
) {
$this->allowAbstractClasses = $allowAbstractClasses;
$this->classesNotRequiredToBeAbstractOrFinal = \array_map(static function (string $classNotRequiredToBeAbstractOrFinal): string {
return $classNotRequiredToBeAbstractOrFinal;
}, $classesNotRequiredToBeAbstractOrFinal);
if ($allowAbstractClasses) {
$this->errorMessageTemplate = 'Class %s is neither abstract nor final.';
}
}
public function getNodeType(): string
{
return Node\Stmt\Class_::class;
}
public function processNode(
Node $node,
Analyser\Scope $scope
): array {
if (!isset($node->namespacedName)) {
return [];
}
if (\in_array($node->namespacedName->toString(), $this->classesNotRequiredToBeAbstractOrFinal, true)) {
return [];
}
if (
$this->allowAbstractClasses
&& $node->isAbstract()
) {
return [];
}
if ($node->isFinal()) {
return [];
}
if ($this->hasWhitelistedAnnotation($node)) {
return [];
}
if ($this->hasWhitelistedAttribute($node)) {
return [];
}
$message = \sprintf(
$this->errorMessageTemplate,
$node->namespacedName->toString(),
);
return [
Rules\RuleErrorBuilder::message($message)
->identifier(ErrorIdentifier::final()->toString())
->build(),
];
}
/**
* This method is inspired by the work on PhpCsFixer\Fixer\ClassNotation\FinalClassFixer and
* PhpCsFixer\Fixer\ClassNotation\FinalInternalClassFixer contributed by Dariusz Rumiński, Filippo Tessarotto, and
* Spacepossum for friendsofphp/php-cs-fixer.
*
* @see https://github.com/FriendsOfPHP/PHP-CS-Fixer/blob/2.15/src/Fixer/ClassNotation/FinalClassFixer.php
* @see https://github.com/FriendsOfPHP/PHP-CS-Fixer/blob/2.15/src/Fixer/ClassNotation/FinalInternalClassFixer.php
* @see https://github.com/keradus
* @see https://github.com/SpacePossum
* @see https://github.com/Slamdunk
*/
private function hasWhitelistedAnnotation(Node\Stmt\Class_ $node): bool
{
$docComment = $node->getDocComment();
if (!$docComment instanceof Comment\Doc) {
return false;
}
$reformattedComment = $docComment->getReformattedText();
if (\is_int(\preg_match_all('/@(\S+)(?=\s|$)/', $reformattedComment, $matches))) {
foreach ($matches[1] as $annotation) {
foreach (self::$whitelistedAnnotations as $whitelistedAnnotation) {
if (0 === \mb_strpos($annotation, $whitelistedAnnotation)) {
return true;
}
}
}
}
return false;
}
private function hasWhitelistedAttribute(Node\Stmt\Class_ $node): bool
{
foreach ($node->attrGroups as $attributeGroup) {
foreach ($attributeGroup->attrs as $attribute) {
if (\in_array($attribute->name->toString(), self::$whitelistedAttributes, true)) {
return true;
}
}
}
return false;
}
}