Skip to content

Commit b1e689a

Browse files
authored
Fix types for Analyzers (#276)
1 parent 95b5322 commit b1e689a

File tree

7 files changed

+101
-40
lines changed

7 files changed

+101
-40
lines changed

dev-tools/phpstan.neon

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,6 @@ parameters:
77
- '#^Method PhpCsFixerCustomFixers\\Fixer\\[a-zA-Z]+::configure\(\) has parameter \$configuration with no value type specified in iterable type array\.$#'
88
- message: '#Cannot call method [a-zA-Z]+\(\) on PhpCsFixer\\Tokenizer\\Token\|null\.$#'
99
paths:
10-
- ../dev-tools/src/Fixer/PriorityInternalFixer.php
11-
- ../src/Analyzer/ArrayAnalyzer.php
12-
- ../src/Analyzer/DataProviderAnalyzer.php
13-
- ../src/Analyzer/ReferenceAnalyzer.php
14-
- ../src/Analyzer/SwitchAnalyzer.php
1510
- ../src/Fixer/CommentSurroundedBySpacesFixer.php
1611
- ../src/Fixer/CommentedOutFunctionFixer.php
1712
- ../src/Fixer/DataProviderNameFixer.php

dev-tools/psalm.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
<MixedArgument errorLevel='suppress' />
2929
<MixedArgumentTypeCoercion errorLevel='suppress' />
3030
<MixedArrayAccess errorLevel='suppress' />
31-
<MixedArrayOffset errorLevel='suppress' />
3231
<MixedAssignment errorLevel='suppress' />
3332
<MixedInferredReturnType errorLevel='suppress' />
3433
<MixedMethodCall errorLevel='suppress' />

dev-tools/src/Fixer/PriorityInternalFixer.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,10 @@ public function fix(\SplFileInfo $file, Tokens $tokens): void
5252
/** @var int $classNameIndex */
5353
$classNameIndex = $tokens->getPrevMeaningfulToken($sequencesStartIndex);
5454

55-
$className = $tokens[$classNameIndex]->getContent();
55+
/** @var Token $classNameToken */
56+
$classNameToken = $tokens[$classNameIndex];
57+
58+
$className = $classNameToken->getContent();
5659

5760
/** @var int $startIndex */
5861
$startIndex = $tokens->getNextTokenOfKind($sequencesStartIndex, ['{']);
@@ -73,7 +76,10 @@ public function fix(\SplFileInfo $file, Tokens $tokens): void
7376
$commentsToInsert = $this->getCommentsToInsert($className);
7477

7578
for ($index = $startIndex; $index < $endIndex; $index++) {
76-
if (!$tokens[$index]->isGivenKind(T_COMMENT)) {
79+
/** @var Token $token */
80+
$token = $tokens[$index];
81+
82+
if (!$token->isGivenKind(T_COMMENT)) {
7783
continue;
7884
}
7985

@@ -102,7 +108,11 @@ public function fix(\SplFileInfo $file, Tokens $tokens): void
102108
$nextIndex = $tokens->getNextTokenOfKind($startIndex, [[T_RETURN]]);
103109

104110
$priorityStartIndex = $nextIndex + 2;
105-
if ($tokens[$priorityStartIndex]->isGivenKind(T_VARIABLE)) {
111+
112+
/** @var Token $priorityStartToken */
113+
$priorityStartToken = $tokens[$priorityStartIndex];
114+
115+
if ($priorityStartToken->isGivenKind(T_VARIABLE)) {
106116
return;
107117
}
108118

src/Analyzer/ArrayAnalyzer.php

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace PhpCsFixerCustomFixers\Analyzer;
66

77
use PhpCsFixer\Tokenizer\CT;
8+
use PhpCsFixer\Tokenizer\Token;
89
use PhpCsFixer\Tokenizer\Tokens;
910
use PhpCsFixerCustomFixers\Analyzer\Analysis\ArrayElementAnalysis;
1011

@@ -18,7 +19,10 @@ final class ArrayAnalyzer
1819
*/
1920
public function getElements(Tokens $tokens, int $index): array
2021
{
21-
if ($tokens[$index]->isGivenKind(CT::T_ARRAY_SQUARE_BRACE_OPEN)) {
22+
/** @var Token $token */
23+
$token = $tokens[$index];
24+
25+
if ($token->isGivenKind(CT::T_ARRAY_SQUARE_BRACE_OPEN)) {
2226
/** @var int $arrayContentStartIndex */
2327
$arrayContentStartIndex = $tokens->getNextMeaningfulToken($index);
2428

@@ -28,7 +32,7 @@ public function getElements(Tokens $tokens, int $index): array
2832
return $this->getElementsForArrayContent($tokens, $arrayContentStartIndex, $arrayContentEndIndex);
2933
}
3034

31-
if ($tokens[$index]->isGivenKind(T_ARRAY)) {
35+
if ($token->isGivenKind(T_ARRAY)) {
3236
/** @var int $arrayOpenBraceIndex */
3337
$arrayOpenBraceIndex = $tokens->getNextTokenOfKind($index, ['(']);
3438

@@ -53,7 +57,10 @@ private function getElementsForArrayContent(Tokens $tokens, int $startIndex, int
5357

5458
$index = $startIndex;
5559
while ($endIndex >= $index = $this->nextCandidateIndex($tokens, $index)) {
56-
if (!$tokens[$index]->equals(',')) {
60+
/** @var Token $token */
61+
$token = $tokens[$index];
62+
63+
if (!$token->equals(',')) {
5764
continue;
5865
}
5966

@@ -77,7 +84,10 @@ private function createArrayElementAnalysis(Tokens $tokens, int $startIndex, int
7784
{
7885
$index = $startIndex;
7986
while ($endIndex > $index = $this->nextCandidateIndex($tokens, $index)) {
80-
if (!$tokens[$index]->isGivenKind(T_DOUBLE_ARROW)) {
87+
/** @var Token $token */
88+
$token = $tokens[$index];
89+
90+
if (!$token->isGivenKind(T_DOUBLE_ARROW)) {
8191
continue;
8292
}
8393

@@ -95,23 +105,26 @@ private function createArrayElementAnalysis(Tokens $tokens, int $startIndex, int
95105

96106
private function nextCandidateIndex(Tokens $tokens, int $index): int
97107
{
98-
if ($tokens[$index]->equals('{')) {
99-
$index = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_CURLY_BRACE, $index);
108+
/** @var Token $token */
109+
$token = $tokens[$index];
110+
111+
if ($token->equals('{')) {
112+
return $tokens->findBlockEnd(Tokens::BLOCK_TYPE_CURLY_BRACE, $index) + 1;
100113
}
101114

102-
if ($tokens[$index]->equals('(')) {
103-
$index = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $index);
115+
if ($token->equals('(')) {
116+
return $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $index) + 1;
104117
}
105118

106-
if ($tokens[$index]->isGivenKind(CT::T_ARRAY_SQUARE_BRACE_OPEN)) {
107-
$index = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_ARRAY_SQUARE_BRACE, $index);
119+
if ($token->isGivenKind(CT::T_ARRAY_SQUARE_BRACE_OPEN)) {
120+
return $tokens->findBlockEnd(Tokens::BLOCK_TYPE_ARRAY_SQUARE_BRACE, $index) + 1;
108121
}
109122

110-
if ($tokens[$index]->isGivenKind(T_ARRAY)) {
123+
if ($token->isGivenKind(T_ARRAY)) {
111124
/** @var int $arrayOpenBraceIndex */
112125
$arrayOpenBraceIndex = $tokens->getNextTokenOfKind($index, ['(']);
113126

114-
$index = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $arrayOpenBraceIndex);
127+
return $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $arrayOpenBraceIndex) + 1;
115128
}
116129

117130
return $index + 1;

src/Analyzer/DataProviderAnalyzer.php

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace PhpCsFixerCustomFixers\Analyzer;
66

77
use PhpCsFixer\Preg;
8+
use PhpCsFixer\Tokenizer\Token;
89
use PhpCsFixer\Tokenizer\Tokens;
910
use PhpCsFixerCustomFixers\Analyzer\Analysis\DataProviderAnalysis;
1011

@@ -29,11 +30,14 @@ public function getDataProviders(Tokens $tokens, int $startIndex, int $endIndex)
2930
[[T_ABSTRACT], [T_COMMENT], [T_FINAL], [T_FUNCTION], [T_PRIVATE], [T_PROTECTED], [T_PUBLIC], [T_STATIC], [T_WHITESPACE]]
3031
);
3132

32-
if (!$tokens[$docCommentIndex]->isGivenKind(T_DOC_COMMENT)) {
33+
/** @var Token $docCommentToken */
34+
$docCommentToken = $tokens[$docCommentIndex];
35+
36+
if (!$docCommentToken->isGivenKind(T_DOC_COMMENT)) {
3337
continue;
3438
}
3539

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

3842
/** @var string[] $matches */
3943
$matches = $matches[1];
@@ -67,17 +71,24 @@ private function getMethods(Tokens $tokens, int $startIndex, int $endIndex): arr
6771
{
6872
$functions = [];
6973
for ($index = $startIndex; $index < $endIndex; $index++) {
70-
if (!$tokens[$index]->isGivenKind(T_FUNCTION)) {
74+
/** @var Token $token */
75+
$token = $tokens[$index];
76+
77+
if (!$token->isGivenKind(T_FUNCTION)) {
7178
continue;
7279
}
7380

7481
/** @var int $functionNameIndex */
7582
$functionNameIndex = $tokens->getNextNonWhitespace($index);
76-
if (!$tokens[$functionNameIndex]->isGivenKind(T_STRING)) {
83+
84+
/** @var Token $functionNameToken */
85+
$functionNameToken = $tokens[$functionNameIndex];
86+
87+
if (!$functionNameToken->isGivenKind(T_STRING)) {
7788
continue;
7889
}
7990

80-
$functions[$tokens[$functionNameIndex]->getContent()] = $functionNameIndex;
91+
$functions[$functionNameToken->getContent()] = $functionNameIndex;
8192
}
8293

8394
return $functions;

src/Analyzer/ReferenceAnalyzer.php

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace PhpCsFixerCustomFixers\Analyzer;
66

77
use PhpCsFixer\Tokenizer\CT;
8+
use PhpCsFixer\Tokenizer\Token;
89
use PhpCsFixer\Tokenizer\Tokens;
910

1011
/**
@@ -14,24 +15,34 @@ final class ReferenceAnalyzer
1415
{
1516
public function isReference(Tokens $tokens, int $index): bool
1617
{
17-
if ($tokens[$index]->isGivenKind(CT::T_RETURN_REF)) {
18+
/** @var Token $token */
19+
$token = $tokens[$index];
20+
21+
if ($token->isGivenKind(CT::T_RETURN_REF)) {
1822
return true;
1923
}
2024

21-
if (!$tokens[$index]->equals('&')) {
25+
if (!$token->equals('&')) {
2226
return false;
2327
}
2428

2529
/** @var int $index */
2630
$index = $tokens->getPrevMeaningfulToken($index);
27-
if ($tokens[$index]->equalsAny(['=', [T_AS], [T_CALLABLE], [T_DOUBLE_ARROW], [CT::T_ARRAY_TYPEHINT]])) {
31+
32+
/** @var Token $token */
33+
$token = $tokens[$index];
34+
35+
if ($token->equalsAny(['=', [T_AS], [T_CALLABLE], [T_DOUBLE_ARROW], [CT::T_ARRAY_TYPEHINT]])) {
2836
return true;
2937
}
3038

31-
if ($tokens[$index]->isGivenKind(T_STRING)) {
39+
if ($token->isGivenKind(T_STRING)) {
3240
$index = $tokens->getPrevMeaningfulToken($index);
41+
42+
/** @var Token $token */
43+
$token = $tokens[$index];
3344
}
3445

35-
return $tokens[$index]->equalsAny(['(', ',', [T_NS_SEPARATOR], [CT::T_NULLABLE_TYPE]]);
46+
return $token->equalsAny(['(', ',', [T_NS_SEPARATOR], [CT::T_NULLABLE_TYPE]]);
3647
}
3748
}

src/Analyzer/SwitchAnalyzer.php

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace PhpCsFixerCustomFixers\Analyzer;
66

7+
use PhpCsFixer\Tokenizer\Token;
78
use PhpCsFixer\Tokenizer\Tokens;
89
use PhpCsFixerCustomFixers\Analyzer\Analysis\CaseAnalysis;
910
use PhpCsFixerCustomFixers\Analyzer\Analysis\SwitchAnalysis;
@@ -15,7 +16,10 @@ final class SwitchAnalyzer
1516
{
1617
public function getSwitchAnalysis(Tokens $tokens, int $switchIndex): SwitchAnalysis
1718
{
18-
if (!$tokens[$switchIndex]->isGivenKind(T_SWITCH)) {
19+
/** @var Token $switchToken */
20+
$switchToken = $tokens[$switchIndex];
21+
22+
if (!$switchToken->isGivenKind(T_SWITCH)) {
1923
throw new \InvalidArgumentException(\sprintf('Index %d is not "switch".', $switchIndex));
2024
}
2125

@@ -27,15 +31,19 @@ public function getSwitchAnalysis(Tokens $tokens, int $switchIndex): SwitchAnaly
2731
$index = $casesStartIndex;
2832
while ($index < $casesEndIndex) {
2933
$index++;
30-
if ($tokens[$index]->isGivenKind(T_SWITCH)) {
34+
35+
/** @var Token $token */
36+
$token = $tokens[$index];
37+
38+
if ($token->isGivenKind(T_SWITCH)) {
3139
$index = (new self())->getSwitchAnalysis($tokens, $index)->getCasesEnd();
3240
continue;
3341
}
34-
if ($tokens[$index]->equals('?')) {
42+
if ($token->equals('?')) {
3543
$ternaryOperatorDepth++;
3644
continue;
3745
}
38-
if (!$tokens[$index]->equals(':')) {
46+
if (!$token->equals(':')) {
3947
continue;
4048
}
4149
if ($ternaryOperatorDepth > 0) {
@@ -62,7 +70,10 @@ private function getCasesStart(Tokens $tokens, int $switchIndex): int
6270

6371
private function getCasesEnd(Tokens $tokens, int $casesStartIndex): int
6472
{
65-
if ($tokens[$casesStartIndex]->equals('{')) {
73+
/** @var Token $casesStartToken */
74+
$casesStartToken = $tokens[$casesStartIndex];
75+
76+
if ($casesStartToken->equals('{')) {
6677
return $tokens->findBlockEnd(Tokens::BLOCK_TYPE_CURLY_BRACE, $casesStartIndex);
6778
}
6879

@@ -72,23 +83,34 @@ private function getCasesEnd(Tokens $tokens, int $casesStartIndex): int
7283
/** @var int $index */
7384
$index = $tokens->getNextMeaningfulToken($index);
7485

75-
if ($tokens[$index]->isGivenKind(T_ENDSWITCH)) {
86+
/** @var Token $token */
87+
$token = $tokens[$index];
88+
89+
if ($token->isGivenKind(T_ENDSWITCH)) {
7690
$depth--;
7791
continue;
7892
}
7993

80-
if (!$tokens[$index]->isGivenKind(T_SWITCH)) {
94+
if (!$token->isGivenKind(T_SWITCH)) {
8195
continue;
8296
}
97+
8398
$index = $this->getCasesStart($tokens, $index);
84-
if ($tokens[$index]->equals(':')) {
99+
100+
/** @var Token $token */
101+
$token = $tokens[$index];
102+
103+
if ($token->equals(':')) {
85104
$depth++;
86105
}
87106
}
88107

89108
/** @var int $afterEndswitchIndex */
90109
$afterEndswitchIndex = $tokens->getNextMeaningfulToken($index);
91110

92-
return $tokens[$afterEndswitchIndex]->equals(';') ? $afterEndswitchIndex : $index;
111+
/** @var Token $afterEndswitchToken */
112+
$afterEndswitchToken = $tokens[$afterEndswitchIndex];
113+
114+
return $afterEndswitchToken->equals(';') ? $afterEndswitchIndex : $index;
93115
}
94116
}

0 commit comments

Comments
 (0)