-
Notifications
You must be signed in to change notification settings - Fork 568
Expand file tree
/
Copy pathMissingClassConstantTypehintRule.php
More file actions
110 lines (95 loc) · 3.31 KB
/
MissingClassConstantTypehintRule.php
File metadata and controls
110 lines (95 loc) · 3.31 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
<?php declare(strict_types = 1);
namespace PHPStan\Rules\Constants;
use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\DependencyInjection\RegisteredRule;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Rules\IdentifierRuleError;
use PHPStan\Rules\MissingTypehintCheck;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\ShouldNotHappenException;
use PHPStan\Type\VerbosityLevel;
use function array_merge;
use function sprintf;
/**
* @implements Rule<Node\Stmt\ClassConst>
*/
#[RegisteredRule(level: 6)]
final class MissingClassConstantTypehintRule implements Rule
{
public function __construct(private MissingTypehintCheck $missingTypehintCheck)
{
}
public function getNodeType(): string
{
return Node\Stmt\ClassConst::class;
}
public function processNode(Node $node, Scope $scope): array
{
if (!$scope->isInClass()) {
throw new ShouldNotHappenException();
}
$errors = [];
foreach ($node->consts as $const) {
$constantName = $const->name->toString();
$errors = array_merge($errors, $this->processSingleConstant($scope->getClassReflection(), $constantName));
}
return $errors;
}
/**
* @return list<IdentifierRuleError>
*/
private function processSingleConstant(ClassReflection $classReflection, string $constantName): array
{
$constantReflection = $classReflection->getConstant($constantName);
$constantType = $constantReflection->getPhpDocType();
if ($constantType === null) {
return [];
}
$errors = [];
foreach ($this->missingTypehintCheck->getIterableTypesWithMissingValueTypehint($constantType) as $iterableType) {
$iterableTypeDescription = $iterableType->describe(VerbosityLevel::typeOnly());
$errors[] = RuleErrorBuilder::message(sprintf(
'Constant %s::%s type has no value type specified in iterable type %s.',
$constantReflection->getDeclaringClass()->getDisplayName(),
$constantName,
$iterableTypeDescription,
))
->tip(MissingTypehintCheck::MISSING_ITERABLE_VALUE_TYPE_TIP)
->identifier('missingType.iterableValue')
->build();
}
foreach ($this->missingTypehintCheck->getNonGenericObjectTypesWithGenericClass($constantType) as [$name, $genericTypeNames]) {
$errors[] = RuleErrorBuilder::message(sprintf(
'Constant %s::%s with generic %s does not specify its types: %s',
$constantReflection->getDeclaringClass()->getDisplayName(),
$constantName,
$name,
$genericTypeNames,
))
->identifier('missingType.generics')
->build();
}
foreach ($this->missingTypehintCheck->getRawGenericTypeAliasesUsage($constantType) as [$aliasName, $missingParams]) {
$errors[] = RuleErrorBuilder::message(sprintf(
'Constant %s::%s with generic type alias %s does not specify its types: %s',
$constantReflection->getDeclaringClass()->getDisplayName(),
$constantName,
$aliasName,
$missingParams,
))
->identifier('missingType.generics')
->build();
}
foreach ($this->missingTypehintCheck->getCallablesWithMissingSignature($constantType) as $callableType) {
$errors[] = RuleErrorBuilder::message(sprintf(
'Constant %s::%s type has no signature specified for %s.',
$constantReflection->getDeclaringClass()->getDisplayName(),
$constantName,
$callableType->describe(VerbosityLevel::typeOnly()),
))->identifier('missingType.callable')->build();
}
return $errors;
}
}