Skip to content

Commit 77a9c8d

Browse files
authored
Remove Token typehints (#610)
1 parent d7cf9dc commit 77a9c8d

38 files changed

+241
-603
lines changed

dev-tools/phpstan.neon

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ parameters:
1717
- ../src
1818
- ../tests
1919
reportMaybesInMethodSignatures: false
20+
stubFiles:
21+
- ./stubs.php
2022

2123
includes:
2224
- ./vendor/symplify/phpstan-rules/config/services/services.neon

dev-tools/src/Fixer/PriorityInternalFixer.php

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,7 @@ public function fix(\SplFileInfo $file, Tokens $tokens): void
6767
/** @var int $classNameIndex */
6868
$classNameIndex = $tokens->getPrevMeaningfulToken($sequencesStartIndex);
6969

70-
/** @var Token $classNameToken */
71-
$classNameToken = $tokens[$classNameIndex];
72-
73-
$className = $classNameToken->getContent();
70+
$className = $tokens[$classNameIndex]->getContent();
7471

7572
/** @var int $startIndex */
7673
$startIndex = $tokens->getNextTokenOfKind($sequencesStartIndex, ['{']);
@@ -83,12 +80,9 @@ public function fix(\SplFileInfo $file, Tokens $tokens): void
8380
/** @var int $sequencesStartIndex */
8481
$sequencesStartIndex = \key($indices);
8582

86-
/** @var Token $commentToken */
87-
$commentToken = $tokens[$sequencesStartIndex - 2];
88-
8983
$commentContent = $this->getCommentContent($className);
9084

91-
if ($commentToken->isGivenKind(\T_DOC_COMMENT)) {
85+
if ($tokens[$sequencesStartIndex - 2]->isGivenKind(\T_DOC_COMMENT)) {
9286
$tokens[$sequencesStartIndex - 2] = new Token([\T_DOC_COMMENT, $commentContent]);
9387
} else {
9488
$tokens->insertAt(
@@ -105,10 +99,7 @@ public function fix(\SplFileInfo $file, Tokens $tokens): void
10599

106100
$priorityStartIndex = $returnIndex + 2;
107101

108-
/** @var Token $priorityStartToken */
109-
$priorityStartToken = $tokens[$priorityStartIndex];
110-
111-
if ($priorityStartToken->isGivenKind(\T_VARIABLE)) {
102+
if ($tokens[$priorityStartIndex]->isGivenKind(\T_VARIABLE)) {
112103
return;
113104
}
114105

dev-tools/stubs.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
/*
4+
* This file is part of PHP CS Fixer: custom fixers.
5+
*
6+
* (c) 2018 Kuba Werłos
7+
*
8+
* For the full copyright and license information, please view
9+
* the LICENSE file that was distributed with this source code.
10+
*/
11+
12+
namespace PhpCsFixer\Tokenizer {
13+
class Token
14+
{
15+
}
16+
17+
/**
18+
* @extends \SplFixedArray<Token>
19+
*
20+
* @method Token offsetGet()
21+
*/
22+
class Tokens extends \SplFixedArray
23+
{
24+
}
25+
}

src/Analyzer/ArrayAnalyzer.php

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
namespace PhpCsFixerCustomFixers\Analyzer;
1515

1616
use PhpCsFixer\Tokenizer\CT;
17-
use PhpCsFixer\Tokenizer\Token;
1817
use PhpCsFixer\Tokenizer\Tokens;
1918
use PhpCsFixerCustomFixers\Analyzer\Analysis\ArrayElementAnalysis;
2019

@@ -28,10 +27,7 @@ final class ArrayAnalyzer
2827
*/
2928
public function getElements(Tokens $tokens, int $index): array
3029
{
31-
/** @var Token $token */
32-
$token = $tokens[$index];
33-
34-
if ($token->isGivenKind(CT::T_ARRAY_SQUARE_BRACE_OPEN)) {
30+
if ($tokens[$index]->isGivenKind(CT::T_ARRAY_SQUARE_BRACE_OPEN)) {
3531
/** @var int $arrayContentStartIndex */
3632
$arrayContentStartIndex = $tokens->getNextMeaningfulToken($index);
3733

@@ -41,7 +37,7 @@ public function getElements(Tokens $tokens, int $index): array
4137
return $this->getElementsForArrayContent($tokens, $arrayContentStartIndex, $arrayContentEndIndex);
4238
}
4339

44-
if ($token->isGivenKind(\T_ARRAY)) {
40+
if ($tokens[$index]->isGivenKind(\T_ARRAY)) {
4541
/** @var int $arrayOpenBraceIndex */
4642
$arrayOpenBraceIndex = $tokens->getNextTokenOfKind($index, ['(']);
4743

@@ -66,10 +62,7 @@ private function getElementsForArrayContent(Tokens $tokens, int $startIndex, int
6662

6763
$index = $startIndex;
6864
while ($endIndex >= $index = $this->nextCandidateIndex($tokens, $index)) {
69-
/** @var Token $token */
70-
$token = $tokens[$index];
71-
72-
if (!$token->equals(',')) {
65+
if (!$tokens[$index]->equals(',')) {
7366
continue;
7467
}
7568

@@ -93,10 +86,7 @@ private function createArrayElementAnalysis(Tokens $tokens, int $startIndex, int
9386
{
9487
$index = $startIndex;
9588
while ($endIndex > $index = $this->nextCandidateIndex($tokens, $index)) {
96-
/** @var Token $token */
97-
$token = $tokens[$index];
98-
99-
if (!$token->isGivenKind(\T_DOUBLE_ARROW)) {
89+
if (!$tokens[$index]->isGivenKind(\T_DOUBLE_ARROW)) {
10090
continue;
10191
}
10292

@@ -114,22 +104,19 @@ private function createArrayElementAnalysis(Tokens $tokens, int $startIndex, int
114104

115105
private function nextCandidateIndex(Tokens $tokens, int $index): int
116106
{
117-
/** @var Token $token */
118-
$token = $tokens[$index];
119-
120-
if ($token->equals('{')) {
107+
if ($tokens[$index]->equals('{')) {
121108
return $tokens->findBlockEnd(Tokens::BLOCK_TYPE_CURLY_BRACE, $index) + 1;
122109
}
123110

124-
if ($token->equals('(')) {
111+
if ($tokens[$index]->equals('(')) {
125112
return $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $index) + 1;
126113
}
127114

128-
if ($token->isGivenKind(CT::T_ARRAY_SQUARE_BRACE_OPEN)) {
115+
if ($tokens[$index]->isGivenKind(CT::T_ARRAY_SQUARE_BRACE_OPEN)) {
129116
return $tokens->findBlockEnd(Tokens::BLOCK_TYPE_ARRAY_SQUARE_BRACE, $index) + 1;
130117
}
131118

132-
if ($token->isGivenKind(\T_ARRAY)) {
119+
if ($tokens[$index]->isGivenKind(\T_ARRAY)) {
133120
/** @var int $arrayOpenBraceIndex */
134121
$arrayOpenBraceIndex = $tokens->getNextTokenOfKind($index, ['(']);
135122

src/Analyzer/DataProviderAnalyzer.php

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
namespace PhpCsFixerCustomFixers\Analyzer;
1515

1616
use PhpCsFixer\Preg;
17-
use PhpCsFixer\Tokenizer\Token;
1817
use PhpCsFixer\Tokenizer\Tokens;
1918
use PhpCsFixerCustomFixers\Analyzer\Analysis\DataProviderAnalysis;
2019

@@ -39,14 +38,11 @@ public function getDataProviders(Tokens $tokens, int $startIndex, int $endIndex)
3938
[[\T_ABSTRACT], [\T_COMMENT], [\T_FINAL], [\T_FUNCTION], [\T_PRIVATE], [\T_PROTECTED], [\T_PUBLIC], [\T_STATIC], [\T_WHITESPACE]]
4039
);
4140

42-
/** @var Token $docCommentToken */
43-
$docCommentToken = $tokens[$docCommentIndex];
44-
45-
if (!$docCommentToken->isGivenKind(\T_DOC_COMMENT)) {
41+
if (!$tokens[$docCommentIndex]->isGivenKind(\T_DOC_COMMENT)) {
4642
continue;
4743
}
4844

49-
Preg::matchAll('/@dataProvider\s+([a-zA-Z0-9._:-\\\\x7f-\xff]+)/', $docCommentToken->getContent(), $matches);
45+
Preg::matchAll('/@dataProvider\s+([a-zA-Z0-9._:-\\\\x7f-\xff]+)/', $tokens[$docCommentIndex]->getContent(), $matches);
5046

5147
/** @var array<string> $matches */
5248
$matches = $matches[1];
@@ -80,24 +76,18 @@ private function getMethods(Tokens $tokens, int $startIndex, int $endIndex): arr
8076
{
8177
$functions = [];
8278
for ($index = $startIndex; $index < $endIndex; $index++) {
83-
/** @var Token $token */
84-
$token = $tokens[$index];
85-
86-
if (!$token->isGivenKind(\T_FUNCTION)) {
79+
if (!$tokens[$index]->isGivenKind(\T_FUNCTION)) {
8780
continue;
8881
}
8982

9083
/** @var int $functionNameIndex */
9184
$functionNameIndex = $tokens->getNextNonWhitespace($index);
9285

93-
/** @var Token $functionNameToken */
94-
$functionNameToken = $tokens[$functionNameIndex];
95-
96-
if (!$functionNameToken->isGivenKind(\T_STRING)) {
86+
if (!$tokens[$functionNameIndex]->isGivenKind(\T_STRING)) {
9787
continue;
9888
}
9989

100-
$functions[$functionNameToken->getContent()] = $functionNameIndex;
90+
$functions[$tokens[$functionNameIndex]->getContent()] = $functionNameIndex;
10191
}
10292

10393
return $functions;

src/Fixer/CommentSurroundedBySpacesFixer.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,7 @@ public function isRisky(): bool
5454
public function fix(\SplFileInfo $file, Tokens $tokens): void
5555
{
5656
for ($index = $tokens->count() - 1; $index > 0; $index--) {
57-
/** @var Token $token */
58-
$token = $tokens[$index];
59-
60-
if (!$token->isGivenKind([\T_COMMENT, \T_DOC_COMMENT])) {
57+
if (!$tokens[$index]->isGivenKind([\T_COMMENT, \T_DOC_COMMENT])) {
6158
continue;
6259
}
6360

@@ -68,10 +65,10 @@ public function fix(\SplFileInfo $file, Tokens $tokens): void
6865
'/^(.+(?<!(?:\/|\*|\h)))(\*+\/)$/',
6966
],
7067
'$1 $2',
71-
$token->getContent()
68+
$tokens[$index]->getContent()
7269
);
7370

74-
if ($newContent === $token->getContent()) {
71+
if ($newContent === $tokens[$index]->getContent()) {
7572
continue;
7673
}
7774

src/Fixer/CommentedOutFunctionFixer.php

Lines changed: 15 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,7 @@ public function fix(\SplFileInfo $file, Tokens $tokens): void
9595
/** @var int $prevIndex */
9696
$prevIndex = $tokens->getPrevMeaningfulToken($index);
9797

98-
/** @var Token $prevToken */
99-
$prevToken = $tokens[$prevIndex];
100-
101-
if ($prevToken->isGivenKind(\T_NS_SEPARATOR)) {
98+
if ($tokens[$prevIndex]->isGivenKind(\T_NS_SEPARATOR)) {
10299
$startIndex = $prevIndex;
103100
}
104101

@@ -114,14 +111,11 @@ public function fix(\SplFileInfo $file, Tokens $tokens): void
114111
/** @var int $semicolonIndex */
115112
$semicolonIndex = $tokens->getNextMeaningfulToken($endIndex);
116113

117-
/** @var Token $semicolonToken */
118-
$semicolonToken = $tokens[$semicolonIndex];
119-
120-
if (!$semicolonToken->equalsAny([';', [\T_CLOSE_TAG]])) {
114+
if (!$tokens[$semicolonIndex]->equalsAny([';', [\T_CLOSE_TAG]])) {
121115
continue;
122116
}
123117

124-
if ($semicolonToken->equals(';')) {
118+
if ($tokens[$semicolonIndex]->equals(';')) {
125119
$endIndex = $semicolonIndex;
126120
}
127121

@@ -131,14 +125,11 @@ public function fix(\SplFileInfo $file, Tokens $tokens): void
131125

132126
private function isFunctionToFix(Tokens $tokens, int $index): bool
133127
{
134-
/** @var Token $token */
135-
$token = $tokens[$index];
136-
137-
if (!$token->isGivenKind(\T_STRING)) {
128+
if (!$tokens[$index]->isGivenKind(\T_STRING)) {
138129
return false;
139130
}
140131

141-
if (!\in_array(\strtolower($token->getContent()), $this->functions, true)) {
132+
if (!\in_array(\strtolower($tokens[$index]->getContent()), $this->functions, true)) {
142133
return false;
143134
}
144135

@@ -150,24 +141,18 @@ private function isPreviousTokenSeparateStatement(Tokens $tokens, int $index): b
150141
/** @var int $prevIndex */
151142
$prevIndex = $tokens->getPrevMeaningfulToken($index);
152143

153-
/** @var Token $prevToken */
154-
$prevToken = $tokens[$prevIndex];
155-
156-
if ($prevToken->equalsAny([';', '{', '}', [\T_OPEN_TAG]])) {
144+
if ($tokens[$prevIndex]->equalsAny([';', '{', '}', [\T_OPEN_TAG]])) {
157145
return true;
158146
}
159147

160148
$switchAnalyzer = new SwitchAnalyzer();
161149

162-
if (!$prevToken->equals(':')) { // can be part of ternary operator or from switch/case
150+
if (!$tokens[$prevIndex]->equals(':')) { // can be part of ternary operator or from switch/case
163151
return false;
164152
}
165153

166154
for ($i = $index; $i > 0; $i--) {
167-
/** @var Token $token */
168-
$token = $tokens[$i];
169-
170-
if (!$token->isGivenKind(\T_SWITCH)) {
155+
if (!$tokens[$i]->isGivenKind(\T_SWITCH)) {
171156
continue;
172157
}
173158
foreach ($switchAnalyzer->getSwitchAnalysis($tokens, $i)->getCases() as $caseAnalysis) {
@@ -201,18 +186,12 @@ private function canUseSingleLineComment(Tokens $tokens, int $startIndex, int $e
201186
return true;
202187
}
203188

204-
/** @var Token $afterEndToken */
205-
$afterEndToken = $tokens[$endIndex + 1];
206-
207-
if (Preg::match('/^\R/', $afterEndToken->getContent()) === 1) {
189+
if (Preg::match('/^\R/', $tokens[$endIndex + 1]->getContent()) === 1) {
208190
return true;
209191
}
210192

211193
for ($index = $startIndex; $index < $endIndex; $index++) {
212-
/** @var Token $token */
213-
$token = $tokens[$index];
214-
215-
if (\strpos($token->getContent(), '*/') !== false) {
194+
if (\strpos($tokens[$index]->getContent(), '*/') !== false) {
216195
return true;
217196
}
218197
}
@@ -224,26 +203,20 @@ private function fixBlockWithSingleLineComments(Tokens $tokens, int $startIndex,
224203
{
225204
$codeToCommentOut = $tokens->generatePartialCode($startIndex, $endIndex);
226205

227-
/** @var Token $beforeStartToken */
228-
$beforeStartToken = $tokens[$startIndex - 1];
229-
230206
$prefix = '//';
231-
if ($beforeStartToken->isWhitespace()) {
207+
if ($tokens[$startIndex - 1]->isWhitespace()) {
232208
$startIndex--;
233209
/** @var string $prefix */
234-
$prefix = Preg::replace('/(^|\R)(\h*$)/D', '$1//$2', $beforeStartToken->getContent());
210+
$prefix = Preg::replace('/(^|\R)(\h*$)/D', '$1//$2', $tokens[$startIndex]->getContent());
235211
}
236212
$codeToCommentOut = $prefix . \str_replace("\n", "\n//", $codeToCommentOut);
237213

238214
if ($tokens->offsetExists($endIndex + 1)) {
239-
/** @var Token $afterEndToken */
240-
$afterEndToken = $tokens[$endIndex + 1];
241-
242-
if (Preg::match('/^\R/', $afterEndToken->getContent()) === 0) {
215+
if (Preg::match('/^\R/', $tokens[$endIndex + 1]->getContent()) === 0) {
243216
$codeToCommentOut .= "\n";
244-
if ($afterEndToken->isWhitespace()) {
217+
if ($tokens[$endIndex + 1]->isWhitespace()) {
245218
$endIndex++;
246-
$codeToCommentOut .= $afterEndToken->getContent();
219+
$codeToCommentOut .= $tokens[$endIndex]->getContent();
247220
}
248221
}
249222
}

src/Fixer/DataProviderNameFixer.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -122,16 +122,10 @@ private function fixNames(Tokens $tokens, int $startIndex, int $endIndex): void
122122

123123
$usageIndex = $dataProviderAnalysis->getUsageIndices()[0];
124124

125-
/** @var Token $usageToken */
126-
$usageToken = $tokens[$usageIndex];
127-
128125
/** @var int $testNameIndex */
129126
$testNameIndex = $tokens->getNextTokenOfKind($usageIndex, [[\T_STRING]]);
130127

131-
/** @var Token $testNameToken */
132-
$testNameToken = $tokens[$testNameIndex];
133-
134-
$dataProviderNewName = $this->getProviderNameForTestName($testNameToken->getContent());
128+
$dataProviderNewName = $this->getProviderNameForTestName($tokens[$testNameIndex]->getContent());
135129
if ($tokens->findSequence([[\T_FUNCTION], [\T_STRING, $dataProviderNewName]], $startIndex, $endIndex, false) !== null) {
136130
continue;
137131
}
@@ -142,7 +136,7 @@ private function fixNames(Tokens $tokens, int $startIndex, int $endIndex): void
142136
$newCommentContent = Preg::replace(
143137
\sprintf('/(@dataProvider\s+)%s/', $dataProviderAnalysis->getName()),
144138
\sprintf('$1%s', $dataProviderNewName),
145-
$usageToken->getContent()
139+
$tokens[$usageIndex]->getContent()
146140
);
147141

148142
$tokens[$usageIndex] = new Token([\T_DOC_COMMENT, $newCommentContent]);

0 commit comments

Comments
 (0)