/
ModernizeTypesCastingFixer.php
160 lines (132 loc) · 5.85 KB
/
ModernizeTypesCastingFixer.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
<?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\CastNotation;
use PhpCsFixer\AbstractFunctionReferenceFixer;
use PhpCsFixer\FixerDefinition\CodeSample;
use PhpCsFixer\FixerDefinition\FixerDefinition;
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
use PhpCsFixer\Tokenizer\Analyzer\ArgumentsAnalyzer;
use PhpCsFixer\Tokenizer\Token;
use PhpCsFixer\Tokenizer\Tokens;
/**
* @author Vladimir Reznichenko <kalessil@gmail.com>
*/
final class ModernizeTypesCastingFixer extends AbstractFunctionReferenceFixer
{
/**
* {@inheritdoc}
*/
public function getDefinition(): FixerDefinitionInterface
{
return new FixerDefinition(
'Replaces `intval`, `floatval`, `doubleval`, `strval` and `boolval` function calls with according type casting operator.',
[
new CodeSample(
'<?php
$a = intval($b);
$a = floatval($b);
$a = doubleval($b);
$a = strval ($b);
$a = boolval($b);
'
),
],
null,
'Risky if any of the functions `intval`, `floatval`, `doubleval`, `strval` or `boolval` are overridden.'
);
}
/**
* {@inheritdoc}
*/
protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
{
// replacement patterns
static $replacement = [
'intval' => [T_INT_CAST, '(int)'],
'floatval' => [T_DOUBLE_CAST, '(float)'],
'doubleval' => [T_DOUBLE_CAST, '(float)'],
'strval' => [T_STRING_CAST, '(string)'],
'boolval' => [T_BOOL_CAST, '(bool)'],
];
$argumentsAnalyzer = new ArgumentsAnalyzer();
foreach ($replacement as $functionIdentity => $newToken) {
$currIndex = 0;
do {
// try getting function reference and translate boundaries for humans
$boundaries = $this->find($functionIdentity, $tokens, $currIndex, $tokens->count() - 1);
if (null === $boundaries) {
// next function search, as current one not found
continue 2;
}
[$functionName, $openParenthesis, $closeParenthesis] = $boundaries;
// analysing cursor shift
$currIndex = $openParenthesis;
// indicator that the function is overridden
if (1 !== $argumentsAnalyzer->countArguments($tokens, $openParenthesis, $closeParenthesis)) {
continue;
}
$paramContentEnd = $closeParenthesis;
$commaCandidate = $tokens->getPrevMeaningfulToken($paramContentEnd);
if ($tokens[$commaCandidate]->equals(',')) {
$tokens->removeTrailingWhitespace($commaCandidate);
$tokens->clearAt($commaCandidate);
$paramContentEnd = $commaCandidate;
}
// check if something complex passed as an argument and preserve parentheses then
$countParamTokens = 0;
for ($paramContentIndex = $openParenthesis + 1; $paramContentIndex < $paramContentEnd; ++$paramContentIndex) {
// not a space, means some sensible token
if (!$tokens[$paramContentIndex]->isGivenKind(T_WHITESPACE)) {
++$countParamTokens;
}
}
$preserveParentheses = $countParamTokens > 1;
$afterCloseParenthesisIndex = $tokens->getNextMeaningfulToken($closeParenthesis);
$afterCloseParenthesisToken = $tokens[$afterCloseParenthesisIndex];
$wrapInParentheses = $afterCloseParenthesisToken->equalsAny(['[', '{']) || $afterCloseParenthesisToken->isGivenKind(T_POW);
// analyse namespace specification (root one or none) and decide what to do
$prevTokenIndex = $tokens->getPrevMeaningfulToken($functionName);
if ($tokens[$prevTokenIndex]->isGivenKind(T_NS_SEPARATOR)) {
// get rid of root namespace when it used
$tokens->removeTrailingWhitespace($prevTokenIndex);
$tokens->clearAt($prevTokenIndex);
}
// perform transformation
$replacementSequence = [
new Token($newToken),
new Token([T_WHITESPACE, ' ']),
];
if ($wrapInParentheses) {
array_unshift($replacementSequence, new Token('('));
}
if (!$preserveParentheses) {
// closing parenthesis removed with leading spaces
$tokens->removeLeadingWhitespace($closeParenthesis);
$tokens->clearAt($closeParenthesis);
// opening parenthesis removed with trailing spaces
$tokens->removeLeadingWhitespace($openParenthesis);
$tokens->removeTrailingWhitespace($openParenthesis);
$tokens->clearAt($openParenthesis);
} else {
// we'll need to provide a space after a casting operator
$tokens->removeTrailingWhitespace($functionName);
}
if ($wrapInParentheses) {
$tokens->insertAt($closeParenthesis, new Token(')'));
}
$tokens->overrideRange($functionName, $functionName, $replacementSequence);
// nested transformations support
$currIndex = $functionName;
} while (null !== $currIndex);
}
}
}