55namespace PhpCsFixerCustomFixers \Analyzer ;
66
77use PhpCsFixer \Tokenizer \CT ;
8+ use PhpCsFixer \Tokenizer \Token ;
89use PhpCsFixer \Tokenizer \Tokens ;
910use 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 ;
0 commit comments