/
PhpdocToPropertyTypeFixer.php
230 lines (193 loc) · 6.59 KB
/
PhpdocToPropertyTypeFixer.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
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
<?php
/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace PhpCsFixer\Fixer\FunctionNotation;
use PhpCsFixer\AbstractPhpdocToTypeDeclarationFixer;
use PhpCsFixer\DocBlock\Annotation;
use PhpCsFixer\FixerDefinition\FixerDefinition;
use PhpCsFixer\FixerDefinition\VersionSpecification;
use PhpCsFixer\FixerDefinition\VersionSpecificCodeSample;
use PhpCsFixer\Tokenizer\Token;
use PhpCsFixer\Tokenizer\Tokens;
final class PhpdocToPropertyTypeFixer extends AbstractPhpdocToTypeDeclarationFixer
{
/**
* {@inheritdoc}
*/
public function getDefinition()
{
return new FixerDefinition(
'EXPERIMENTAL: Takes `@var` annotation of non-mixed types and adjusts accordingly the property signature. Requires PHP >= 7.4.',
[
new VersionSpecificCodeSample(
'<?php
class Foo {
/** @var int */
private $foo;
/** @var \Traversable */
private $bar;
}
',
new VersionSpecification(70400)
),
new VersionSpecificCodeSample(
'<?php
class Foo {
/** @var int */
private $foo;
/** @var \Traversable */
private $bar;
}
',
new VersionSpecification(70400),
['scalar_types' => false]
),
],
null,
'[1] This rule is EXPERIMENTAL and is not covered with backward compatibility promise. [2] `@var` annotation is mandatory for the fixer to make changes, signatures of properties without it (no docblock, inheritdocs) will not be fixed. [3] Manual actions are required if inherited signatures are not properly documented. [4] `@inheritdocs` support is under construction.'
);
}
/**
* {@inheritdoc}
*/
public function isCandidate(Tokens $tokens)
{
return \PHP_VERSION_ID >= 70400 && $tokens->isTokenKindFound(T_DOC_COMMENT);
}
/**
* {@inheritdoc}
*
* Must run before PhpdocAlignFixer.
* Must run after CommentToPhpdocFixer, PhpdocIndentFixer, PhpdocScalarFixer, PhpdocToCommentFixer, PhpdocTypesFixer.
*/
public function getPriority()
{
return parent::getPriority();
}
/**
* {@inheritdoc}
*/
protected function applyFix(\SplFileInfo $file, Tokens $tokens)
{
for ($index = $tokens->count() - 1; 0 < $index; --$index) {
if ($tokens[$index]->isGivenKind([T_CLASS, T_TRAIT])) {
$this->fixClass($tokens, $index);
}
}
}
/**
* @param int $index
*/
private function fixClass(Tokens $tokens, $index)
{
$index = $tokens->getNextTokenOfKind($index, ['{']);
$classEndIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_CURLY_BRACE, $index);
for (; $index < $classEndIndex; ++$index) {
if ($tokens[$index]->isGivenKind(T_FUNCTION)) {
$index = $tokens->getNextTokenOfKind($index, ['{', ';']);
if ($tokens[$index]->equals('{')) {
$index = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_CURLY_BRACE, $index);
}
continue;
}
if (!$tokens[$index]->isGivenKind(T_DOC_COMMENT)) {
continue;
}
$docCommentIndex = $index;
$propertyIndexes = $this->findNextUntypedPropertiesDeclaration($tokens, $docCommentIndex);
if ([] === $propertyIndexes) {
continue;
}
$typeInfo = $this->resolveAppliableType(
$propertyIndexes,
$this->getAnnotationsFromDocComment('var', $tokens, $docCommentIndex)
);
if (null === $typeInfo) {
continue;
}
list($propertyType, $isNullable) = $typeInfo;
if (\in_array($propertyType, ['void', 'callable'], true)) {
continue;
}
$newTokens = array_merge(
$this->createTypeDeclarationTokens($propertyType, $isNullable),
[new Token([T_WHITESPACE, ' '])]
);
$tokens->insertAt(current($propertyIndexes), $newTokens);
$index = max($propertyIndexes) + \count($newTokens) + 1;
$classEndIndex += \count($newTokens);
}
}
/**
* @param int $index
*
* @return array<string, int>
*/
private function findNextUntypedPropertiesDeclaration(Tokens $tokens, $index)
{
do {
$index = $tokens->getNextMeaningfulToken($index);
} while ($tokens[$index]->isGivenKind([
T_PRIVATE,
T_PROTECTED,
T_PUBLIC,
T_STATIC,
T_VAR,
]));
if (!$tokens[$index]->isGivenKind(T_VARIABLE)) {
return [];
}
$properties = [];
while (!$tokens[$index]->equals(';')) {
if ($tokens[$index]->isGivenKind(T_VARIABLE)) {
$properties[$tokens[$index]->getContent()] = $index;
}
$index = $tokens->getNextMeaningfulToken($index);
}
return $properties;
}
/**
* @param array<string, int> $propertyIndexes
* @param Annotation[] $annotations
*/
private function resolveAppliableType(array $propertyIndexes, array $annotations)
{
$propertyTypes = [];
foreach ($annotations as $annotation) {
$propertyName = $annotation->getVariableName();
if (null === $propertyName) {
if (1 !== \count($propertyIndexes)) {
continue;
}
$propertyName = key($propertyIndexes);
}
if (!isset($propertyIndexes[$propertyName])) {
continue;
}
$typeInfo = $this->getCommonTypeFromAnnotation($annotation);
if (!isset($propertyTypes[$propertyName])) {
$propertyTypes[$propertyName] = [];
} elseif ($typeInfo !== $propertyTypes[$propertyName]) {
return null;
}
$propertyTypes[$propertyName] = $typeInfo;
}
if (\count($propertyTypes) !== \count($propertyIndexes)) {
return null;
}
$type = array_shift($propertyTypes);
foreach ($propertyTypes as $propertyType) {
if ($propertyType !== $type) {
return null;
}
}
return $type;
}
}