forked from PHP-CS-Fixer/PHP-CS-Fixer
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathNoUnneededFinalMethodFixer.php
215 lines (178 loc) · 6.25 KB
/
NoUnneededFinalMethodFixer.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
<?php
declare(strict_types=1);
/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <[email protected]>
* Dariusz Rumiński <[email protected]>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace PhpCsFixer\Fixer\ClassNotation;
use PhpCsFixer\AbstractFixer;
use PhpCsFixer\Fixer\ConfigurableFixerInterface;
use PhpCsFixer\Fixer\ConfigurableFixerTrait;
use PhpCsFixer\FixerConfiguration\FixerConfigurationResolver;
use PhpCsFixer\FixerConfiguration\FixerConfigurationResolverInterface;
use PhpCsFixer\FixerConfiguration\FixerOptionBuilder;
use PhpCsFixer\FixerDefinition\CodeSample;
use PhpCsFixer\FixerDefinition\FixerDefinition;
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
use PhpCsFixer\Tokenizer\Token;
use PhpCsFixer\Tokenizer\Tokens;
use PhpCsFixer\Tokenizer\TokensAnalyzer;
/**
* @author Filippo Tessarotto <[email protected]>
*
* @implements ConfigurableFixerInterface<_AutogeneratedInputConfiguration, _AutogeneratedComputedConfiguration>
*
* @phpstan-type _AutogeneratedInputConfiguration array{
* private_methods?: bool
* }
* @phpstan-type _AutogeneratedComputedConfiguration array{
* private_methods: bool
* }
*/
final class NoUnneededFinalMethodFixer extends AbstractFixer implements ConfigurableFixerInterface
{
/** @use ConfigurableFixerTrait<_AutogeneratedInputConfiguration, _AutogeneratedComputedConfiguration> */
use ConfigurableFixerTrait;
public function getDefinition(): FixerDefinitionInterface
{
return new FixerDefinition(
'Removes `final` from methods where possible.',
[
new CodeSample(
'<?php
final class Foo
{
final public function foo1() {}
final protected function bar() {}
final private function baz() {}
}
class Bar
{
final private function bar1() {}
}
'
),
new CodeSample(
'<?php
final class Foo
{
final private function baz() {}
}
class Bar
{
final private function bar1() {}
}
',
['private_methods' => false]
),
],
null,
'Risky when child class overrides a `private` method.'
);
}
public function isCandidate(Tokens $tokens): bool
{
if (!$tokens->isAllTokenKindsFound([T_FINAL, T_FUNCTION])) {
return false;
}
if (\defined('T_ENUM') && $tokens->isTokenKindFound(T_ENUM)) { // @TODO: drop condition when PHP 8.1+ is required
return true;
}
return $tokens->isTokenKindFound(T_CLASS);
}
public function isRisky(): bool
{
return true;
}
protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
{
foreach ($this->getMethods($tokens) as $element) {
$index = $element['method_final_index'];
if ($element['method_of_enum'] || $element['class_is_final']) {
$this->clearFinal($tokens, $index);
continue;
}
if (!$element['method_is_private'] || false === $this->configuration['private_methods'] || $element['method_is_constructor']) {
continue;
}
$this->clearFinal($tokens, $index);
}
}
protected function createConfigurationDefinition(): FixerConfigurationResolverInterface
{
return new FixerConfigurationResolver([
(new FixerOptionBuilder('private_methods', 'Private methods of non-`final` classes must not be declared `final`.'))
->setAllowedTypes(['bool'])
->setDefault(true)
->getOption(),
]);
}
/**
* @return \Generator<array{
* classIndex: int,
* token: Token,
* type: string,
* class_is_final?: bool,
* method_final_index: int|null,
* method_is_constructor?: bool,
* method_is_private: bool,
* method_of_enum: bool
* }>
*/
private function getMethods(Tokens $tokens): \Generator
{
$tokensAnalyzer = new TokensAnalyzer($tokens);
$modifierKinds = [T_PUBLIC, T_PROTECTED, T_PRIVATE, T_FINAL, T_ABSTRACT, T_STATIC];
$enums = [];
$classesAreFinal = [];
foreach ($tokensAnalyzer->getClassyElements() as $index => $element) {
if ('method' !== $element['type']) {
continue; // not a method
}
$classIndex = $element['classIndex'];
if (!\array_key_exists($classIndex, $enums)) {
$enums[$classIndex] = \defined('T_ENUM') && $tokens[$classIndex]->isGivenKind(T_ENUM); // @TODO: drop condition when PHP 8.1+ is required
}
$element['method_final_index'] = null;
$element['method_is_private'] = false;
$previous = $index;
do {
$previous = $tokens->getPrevMeaningfulToken($previous);
if ($tokens[$previous]->isGivenKind(T_PRIVATE)) {
$element['method_is_private'] = true;
} elseif ($tokens[$previous]->isGivenKind(T_FINAL)) {
$element['method_final_index'] = $previous;
}
} while ($tokens[$previous]->isGivenKind($modifierKinds));
if ($enums[$classIndex]) {
$element['method_of_enum'] = true;
yield $element;
continue;
}
if (!\array_key_exists($classIndex, $classesAreFinal)) {
$modifiers = $tokensAnalyzer->getClassyModifiers($classIndex);
$classesAreFinal[$classIndex] = isset($modifiers['final']);
}
$element['method_of_enum'] = false;
$element['class_is_final'] = $classesAreFinal[$classIndex];
$element['method_is_constructor'] = '__construct' === strtolower($tokens[$tokens->getNextMeaningfulToken($index)]->getContent());
yield $element;
}
}
private function clearFinal(Tokens $tokens, ?int $index): void
{
if (null === $index) {
return;
}
$tokens->clearAt($index);
++$index;
if ($tokens[$index]->isWhitespace()) {
$tokens->clearAt($index);
}
}
}