Repository navigation
Expand file tree
/
Copy pathClassCaseSensitivityCheck.php
More file actions
123 lines (108 loc) · 3.34 KB
/
Copy pathClassCaseSensitivityCheck.php
File metadata and controls
123 lines (108 loc) · 3.34 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
<?php declare(strict_types = 1);
namespace PHPStan\Rules;
use PhpParser\Node\Name;
use PHPStan\DependencyInjection\AutowiredParameter;
use PHPStan\DependencyInjection\AutowiredService;
use PHPStan\Parser\UseAliasVisitor;
use PHPStan\Reflection\ReflectionProvider;
use function count;
use function sprintf;
use function strrpos;
use function strtolower;
use function substr;
#[AutowiredService]
final class ClassCaseSensitivityCheck
{
public function __construct(
private ReflectionProvider $reflectionProvider,
#[AutowiredParameter]
private bool $checkInternalClassCaseSensitivity,
#[AutowiredParameter(ref: '%featureToggles.checkImportedClassNameCase%')]
private bool $checkImportedClassNameCase,
)
{
}
/**
* @param ClassNameNodePair[] $pairs
* @return list<IdentifierRuleError>
*/
public function checkClassNames(array $pairs): array
{
$errors = [];
foreach ($pairs as $pair) {
$className = $this->getClassNameAsWritten($pair);
if (!$this->reflectionProvider->hasClass($className)) {
continue;
}
$classReflection = $this->reflectionProvider->getClass($className);
if (!$this->checkInternalClassCaseSensitivity && $classReflection->isBuiltin()) {
continue; // skip built-in classes
}
$realClassName = $classReflection->getName();
if (strtolower($realClassName) !== strtolower($className)) {
continue; // skip class_alias() where the alias is a completely different name
}
if ($pair->getNode()->getAttribute(UseAliasVisitor::ATTRIBUTE_NAME) === true) {
continue;
}
if ($realClassName === $className) {
continue;
}
$typeName = $classReflection->getClassTypeDescription();
$errors[] = RuleErrorBuilder::message(sprintf(
'%s %s referenced with incorrect case: %s.',
$typeName,
$realClassName,
$className,
))
->identifier(sprintf('%s.nameCase', strtolower($typeName)))
->line($pair->getNode()->getStartLine())
->build();
}
return $errors;
}
/**
* @return non-empty-string
*/
private function getClassNameAsWritten(ClassNameNodePair $pair): string
{
$className = $pair->getClassName();
if (!$this->checkImportedClassNameCase) {
return $className;
}
$node = $pair->getNode();
if (!$node instanceof Name || $node->toString() !== $className) {
return $className;
}
return self::getNameAsWritten($node);
}
/**
* NameResolver replaces a single-part imported name entirely with the use statement's
* spelling, so the case the user wrote survives only in the originalName attribute.
* Multi-part names keep the written case of everything after the first part.
*
* @return non-empty-string
*/
public static function getNameAsWritten(Name $node): string
{
$resolvedName = $node->toString();
$originalName = $node->getAttribute('originalName');
if (
!$originalName instanceof Name
|| $originalName instanceof Name\FullyQualified
|| $originalName instanceof Name\Relative
) {
return $resolvedName;
}
if (count($originalName->getParts()) !== 1) {
return $resolvedName;
}
// the original name takes over the last part of the resolved name,
// everything up to the last separator stays as resolved
$lastSeparatorPos = strrpos($resolvedName, '\\');
if ($lastSeparatorPos === false) {
return $originalName->toString();
}
return substr($resolvedName, 0, $lastSeparatorPos + 1) . $originalName->toString();
}
}