forked from phpstan/phpstan-src
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDuplicateDeclarationHelper.php
More file actions
126 lines (114 loc) · 3.8 KB
/
DuplicateDeclarationHelper.php
File metadata and controls
126 lines (114 loc) · 3.8 KB
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
<?php declare(strict_types = 1);
namespace PHPStan\Rules\Classes;
use PhpParser\Node;
use PhpParser\Node\Stmt\ClassConst;
use PhpParser\Node\Stmt\ClassLike;
use PhpParser\Node\Stmt\EnumCase;
use PHPStan\DependencyInjection\AutowiredService;
use PHPStan\Rules\IdentifierRuleError;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\ShouldNotHappenException;
use function array_key_exists;
use function is_string;
use function sprintf;
use function strtolower;
#[AutowiredService]
final class DuplicateDeclarationHelper
{
/**
* @param 'class'|'interface'|'trait'|'enum' $identifierType
* @return list<IdentifierRuleError>
*/
public function checkClassLike(ClassLike $classLike, string $displayName, string $identifierType): array
{
$errors = [];
$declaredClassConstantsOrEnumCases = [];
foreach ($classLike->stmts as $stmtNode) {
if ($stmtNode instanceof EnumCase) {
if (array_key_exists($stmtNode->name->name, $declaredClassConstantsOrEnumCases)) {
$errors[] = RuleErrorBuilder::message(sprintf(
'Cannot redeclare enum case %s::%s.',
$displayName,
$stmtNode->name->name,
))->identifier(sprintf('%s.duplicateEnumCase', $identifierType))
->line($stmtNode->getStartLine())
->nonIgnorable()
->build();
} else {
$declaredClassConstantsOrEnumCases[$stmtNode->name->name] = true;
}
} elseif ($stmtNode instanceof ClassConst) {
foreach ($stmtNode->consts as $classConstNode) {
if (array_key_exists($classConstNode->name->name, $declaredClassConstantsOrEnumCases)) {
$errors[] = RuleErrorBuilder::message(sprintf(
'Cannot redeclare constant %s::%s.',
$displayName,
$classConstNode->name->name,
))->identifier(sprintf('%s.duplicateConstant', $identifierType))
->line($classConstNode->getStartLine())
->nonIgnorable()
->build();
} else {
$declaredClassConstantsOrEnumCases[$classConstNode->name->name] = true;
}
}
}
}
$declaredProperties = [];
foreach ($classLike->getProperties() as $propertyDecl) {
foreach ($propertyDecl->props as $property) {
if (array_key_exists($property->name->name, $declaredProperties)) {
$errors[] = RuleErrorBuilder::message(sprintf(
'Cannot redeclare property %s::$%s.',
$displayName,
$property->name->name,
))->identifier(sprintf('%s.duplicateProperty', $identifierType))
->line($property->getStartLine())
->nonIgnorable()
->build();
} else {
$declaredProperties[$property->name->name] = true;
}
}
}
$declaredFunctions = [];
foreach ($classLike->getMethods() as $method) {
if ($method->name->toLowerString() === '__construct') {
foreach ($method->params as $param) {
if ($param->flags === 0) {
continue;
}
if (!$param->var instanceof Node\Expr\Variable || !is_string($param->var->name)) {
throw new ShouldNotHappenException();
}
$propertyName = $param->var->name;
if (array_key_exists($propertyName, $declaredProperties)) {
$errors[] = RuleErrorBuilder::message(sprintf(
'Cannot redeclare property %s::$%s.',
$displayName,
$propertyName,
))->identifier(sprintf('%s.duplicateProperty', $identifierType))
->line($param->getStartLine())
->nonIgnorable()
->build();
} else {
$declaredProperties[$propertyName] = true;
}
}
}
if (array_key_exists(strtolower($method->name->name), $declaredFunctions)) {
$errors[] = RuleErrorBuilder::message(sprintf(
'Cannot redeclare method %s::%s().',
$displayName,
$method->name->name,
))->identifier(sprintf('%s.duplicateMethod', $identifierType))
->line($method->getStartLine())
->nonIgnorable()
->build();
} else {
$declaredFunctions[strtolower($method->name->name)] = true;
}
}
return $errors;
}
}