forked from PHP-CS-Fixer/PHP-CS-Fixer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProtectedToPrivateFixer.php
170 lines (138 loc) · 5.27 KB
/
ProtectedToPrivateFixer.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
<?php
declare(strict_types=1);
/*
* 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\ClassNotation;
use PhpCsFixer\AbstractFixer;
use PhpCsFixer\FixerDefinition\CodeSample;
use PhpCsFixer\FixerDefinition\FixerDefinition;
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
use PhpCsFixer\Tokenizer\CT;
use PhpCsFixer\Tokenizer\Token;
use PhpCsFixer\Tokenizer\Tokens;
use PhpCsFixer\Tokenizer\TokensAnalyzer;
/**
* @author Filippo Tessarotto <zoeslam@gmail.com>
*/
final class ProtectedToPrivateFixer extends AbstractFixer
{
private TokensAnalyzer $tokensAnalyzer;
public function getDefinition(): FixerDefinitionInterface
{
return new FixerDefinition(
'Converts `protected` variables and methods to `private` where possible.',
[
new CodeSample(
'<?php
final class Sample
{
protected $a;
protected function test()
{
}
}
'
),
]
);
}
/**
* {@inheritdoc}
*
* Must run before OrderedClassElementsFixer.
* Must run after FinalClassFixer, FinalInternalClassFixer.
*/
public function getPriority(): int
{
return 66;
}
public function isCandidate(Tokens $tokens): bool
{
if (\defined('T_ENUM') && $tokens->isAllTokenKindsFound([T_ENUM, T_PROTECTED])) { // @TODO: drop condition when PHP 8.1+ is required
return true;
}
return $tokens->isAllTokenKindsFound([T_CLASS, T_FINAL, T_PROTECTED]);
}
protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
{
$this->tokensAnalyzer = new TokensAnalyzer($tokens);
$modifierKinds = [T_PUBLIC, T_PROTECTED, T_PRIVATE, T_FINAL, T_ABSTRACT, T_NS_SEPARATOR, T_STRING, CT::T_NULLABLE_TYPE, CT::T_ARRAY_TYPEHINT, T_STATIC, CT::T_TYPE_ALTERNATION, CT::T_TYPE_INTERSECTION];
if (\defined('T_READONLY')) { // @TODO: drop condition when PHP 8.1+ is required
$modifierKinds[] = T_READONLY;
}
$classesCandidate = [];
$classElementTypes = ['method' => true, 'property' => true, 'const' => true];
foreach ($this->tokensAnalyzer->getClassyElements() as $index => $element) {
$classIndex = $element['classIndex'];
if (!\array_key_exists($classIndex, $classesCandidate)) {
$classesCandidate[$classIndex] = $this->isClassCandidate($tokens, $classIndex);
}
if (false === $classesCandidate[$classIndex]) {
continue;
}
if (!isset($classElementTypes[$element['type']])) {
continue;
}
$previous = $index;
$isProtected = false;
$isFinal = false;
do {
$previous = $tokens->getPrevMeaningfulToken($previous);
if ($tokens[$previous]->isGivenKind(T_PROTECTED)) {
$isProtected = $previous;
} elseif ($tokens[$previous]->isGivenKind(T_FINAL)) {
$isFinal = $previous;
}
} while ($tokens[$previous]->isGivenKind($modifierKinds));
if (false === $isProtected) {
continue;
}
if ($isFinal && 'const' === $element['type']) {
continue; // Final constants cannot be private
}
$element['protected_index'] = $isProtected;
$tokens[$element['protected_index']] = new Token([T_PRIVATE, 'private']);
}
}
/**
* Consider symbol as candidate for fixing if it's:
* - an Enum (PHP8.1+)
* - a class, which:
* - is not anonymous
* - is not final
* - does not use traits
* - does not extend other class.
*/
private function isClassCandidate(Tokens $tokens, int $classIndex): bool
{
if (\defined('T_ENUM') && $tokens[$classIndex]->isGivenKind(T_ENUM)) { // @TODO: drop condition when PHP 8.1+ is required
return true;
}
if (!$tokens[$classIndex]->isGivenKind(T_CLASS) || $this->tokensAnalyzer->isAnonymousClass($classIndex)) {
return false;
}
$modifiers = $this->tokensAnalyzer->getClassyModifiers($classIndex);
if (!isset($modifiers['final'])) {
return false;
}
$classNameIndex = $tokens->getNextMeaningfulToken($classIndex); // move to class name as anonymous class is never "final"
$classExtendsIndex = $tokens->getNextMeaningfulToken($classNameIndex); // move to possible "extends"
if ($tokens[$classExtendsIndex]->isGivenKind(T_EXTENDS)) {
return false;
}
if (!$tokens->isTokenKindFound(CT::T_USE_TRAIT)) {
return true; // cheap test
}
$classOpenIndex = $tokens->getNextTokenOfKind($classNameIndex, ['{']);
$classCloseIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_CURLY_BRACE, $classOpenIndex);
$useIndex = $tokens->getNextTokenOfKind($classOpenIndex, [[CT::T_USE_TRAIT]]);
return null === $useIndex || $useIndex > $classCloseIndex;
}
}