-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
FinalRule.php
133 lines (112 loc) 路 3.82 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
<?php
declare(strict_types=1);
/**
* Copyright (c) 2018 Andreas M枚ller
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*
* @see https://github.com/ergebnis/phpstan-rules
*/
namespace Ergebnis\PHPStan\Rules\Classes;
use PhpParser\Comment;
use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\ShouldNotHappenException;
final class FinalRule implements Rule
{
/**
* @var string[]
*/
private static $whitelistedAnnotations = [
'Entity',
'ORM\Entity',
];
private $allowAbstractClasses;
private $classesNotRequiredToBeAbstractOrFinal;
/**
* @var string
*/
private $errorMessageTemplate = 'Class %s is not final.';
/**
* @param bool $allowAbstractClasses
* @param 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 (true === $allowAbstractClasses) {
$this->errorMessageTemplate = 'Class %s is neither abstract nor final.';
}
}
public function getNodeType(): string
{
return Node\Stmt\Class_::class;
}
public function processNode(Node $node, Scope $scope): array
{
if (!$node instanceof Node\Stmt\Class_) {
throw new ShouldNotHappenException(\sprintf(
'Expected node to be instance of "%s", but got instance of "%s" instead.',
Node\Stmt\Class_::class,
\get_class($node)
));
}
if (!isset($node->namespacedName)
|| \in_array($node->namespacedName->toString(), $this->classesNotRequiredToBeAbstractOrFinal, true)
) {
return [];
}
if (true === $this->allowAbstractClasses && $node->isAbstract()) {
return [];
}
if ($node->isFinal()) {
return [];
}
if ($this->isWhitelisted($node)) {
return [];
}
return [
\sprintf(
$this->errorMessageTemplate,
$node->namespacedName
),
];
}
/**
* 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
*
* @param Node $node
*
* @return bool
*/
private function isWhitelisted(Node $node): bool
{
$docComment = $node->getDocComment();
if (!$docComment instanceof Comment\Doc) {
return false;
}
if (\is_int(\preg_match_all('/@(\S+)(?=\s|$)/', $docComment->getReformattedText(), $matches))) {
foreach ($matches[1] as $annotation) {
foreach (self::$whitelistedAnnotations as $whitelistedAnnotation) {
if (0 === \mb_strpos($annotation, $whitelistedAnnotation)) {
return true;
}
}
}
}
return false;
}
}