Repository navigation
Expand file tree
/
Copy pathFixClassCaseSensitivityVarDocblockRector.php
More file actions
180 lines (150 loc) · 5.89 KB
/
Copy pathFixClassCaseSensitivityVarDocblockRector.php
File metadata and controls
180 lines (150 loc) · 5.89 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
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<?php
declare(strict_types=1);
namespace Rector\CodeQuality\Rector\Property;
use PhpParser\Node;
use PhpParser\Node\Identifier;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Property;
use PHPStan\Analyser\Scope;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\Comments\NodeDocBlock\DocBlockUpdater;
use Rector\Naming\Naming\UseImportsResolver;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\Rector\AbstractRector;
use Rector\Reflection\ClassReflectionProvider;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\Tests\CodeQuality\Rector\Property\FixClassCaseSensitivityVarDocblockRector\FixClassCaseSensitivityVarDocblockRectorTest
*/
final class FixClassCaseSensitivityVarDocblockRector extends AbstractRector
{
public function __construct(
private readonly PhpDocInfoFactory $phpDocInfoFactory,
private readonly DocBlockUpdater $docBlockUpdater,
private readonly ClassReflectionProvider $classReflectionProvider,
private readonly UseImportsResolver $useImportsResolver,
) {
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Fix a misspelled class name casing in a @var docblock to match the real class name',
[
new CodeSample(
<<<'CODE_SAMPLE'
/** @var AutomailingService */
$service = $this->getService();
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
/** @var AutoMailingService */
$service = $this->getService();
CODE_SAMPLE
),
]
);
}
public function getNodeTypes(): array
{
return [Property::class, Expression::class];
}
/**
* @param Property|Expression $node
*/
public function refactor(Node $node): ?Node
{
$phpDocInfo = $this->phpDocInfoFactory->createFromNode($node);
if (! $phpDocInfo instanceof PhpDocInfo) {
return null;
}
$hasChanged = false;
foreach ($phpDocInfo->getPhpDocNode()->getVarTagValues() as $varTagValueNode) {
$correctedTypeNode = $this->correctClassNameCasing($varTagValueNode->type, $node);
if ($correctedTypeNode instanceof IdentifierTypeNode) {
$varTagValueNode->type = $correctedTypeNode;
$hasChanged = true;
}
}
if (! $hasChanged) {
return null;
}
$this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($node);
return $node;
}
private function correctClassNameCasing(TypeNode $typeNode, Node $node): ?IdentifierTypeNode
{
if (! $typeNode instanceof IdentifierTypeNode) {
return null;
}
$writtenName = $typeNode->name;
$existingClassName = $this->resolveExistingClassName($writtenName, $node);
if ($existingClassName === null) {
return null;
}
$realClassName = $this->classReflectionProvider->getClass($existingClassName)
->getName();
$hasLeadingSlash = str_starts_with($writtenName, '\\');
$writtenParts = explode('\\', ltrim($writtenName, '\\'));
$realParts = array_slice(explode('\\', $realClassName), -count($writtenParts));
// not a pure casing difference, e.g. an alias or partial name
if (strtolower(implode('\\', $writtenParts)) !== strtolower(implode('\\', $realParts))) {
return null;
}
$correctedName = ($hasLeadingSlash ? '\\' : '') . implode('\\', $realParts);
if ($correctedName === $writtenName) {
return null;
}
return new IdentifierTypeNode($correctedName);
}
/**
* Resolve the existing class name for a written, possibly miss-cased, identifier.
* Lookups in PHPStan reflection are case-insensitive, so the namespace must match,
* while the class name casing may differ.
*/
private function resolveExistingClassName(string $writtenName, Node $node): ?string
{
$bareName = ltrim($writtenName, '\\');
// already fully qualified
if (str_starts_with($writtenName, '\\')) {
return $this->classReflectionProvider->hasClass($bareName) ? $bareName : null;
}
$parts = explode('\\', $bareName);
$firstPart = $parts[0];
// resolve via use imports, matching the imported short name case-insensitively
foreach ($this->useImportsResolver->resolve() as $use) {
$prefix = $this->useImportsResolver->resolvePrefix($use);
foreach ($use->uses as $useItem) {
if ($useItem->alias instanceof Identifier) {
continue;
}
if (strtolower($useItem->name->getLast()) !== strtolower($firstPart)) {
continue;
}
$candidate = $prefix . $useItem->name->toString();
if (count($parts) > 1) {
$candidate .= '\\' . implode('\\', array_slice($parts, 1));
}
if ($this->classReflectionProvider->hasClass($candidate)) {
return $candidate;
}
}
}
// same namespace
$scope = $node->getAttribute(AttributeKey::SCOPE);
if ($scope instanceof Scope) {
$namespace = $scope->getNamespace();
if ($namespace !== null && $this->classReflectionProvider->hasClass($namespace . '\\' . $bareName)) {
return $namespace . '\\' . $bareName;
}
}
// global namespace
if ($this->classReflectionProvider->hasClass($bareName)) {
return $bareName;
}
return null;
}
}