2121
2222final class TypedClassConstantFixer extends AbstractFixer
2323{
24+ private const TOKEN_TO_TYPE_MAP = [
25+ \T_DNUMBER => 'float ' ,
26+ \T_LNUMBER => 'int ' ,
27+ \T_CONSTANT_ENCAPSED_STRING => 'string ' ,
28+ CT ::T_ARRAY_SQUARE_BRACE_CLOSE => 'array ' ,
29+ ];
30+
2431 public function getDefinition (): FixerDefinitionInterface
2532 {
2633 return new FixerDefinition (
@@ -39,7 +46,7 @@ class Foo {
3946 ),
4047 ],
4148 '' ,
42- 'when constant is not of single type ' ,
49+ 'when constant can be of different types ' ,
4350 );
4451 }
4552
@@ -91,55 +98,50 @@ private static function fixClass(Tokens $tokens, int $openParenthesisIndex, int
9198 continue ;
9299 }
93100
101+ $ type = self ::getTypeOfExpression ($ tokens , $ assignmentIndex );
102+
94103 $ tokens ->insertAt (
95104 $ constantNameIndex ,
96105 [
97- self :: getTypeOfExpression ( $ tokens , $ assignmentIndex ),
106+ new Token ([ $ type === ' array ' ? CT :: T_ARRAY_TYPEHINT : \ T_STRING , $ type ] ),
98107 new Token ([\T_WHITESPACE , ' ' ]),
99108 ],
100109 );
101110 }
102111 }
103112
104- private static function getTypeOfExpression (Tokens $ tokens , int $ assignmentIndex ): Token
113+ private static function getTypeOfExpression (Tokens $ tokens , int $ assignmentIndex ): string
105114 {
106115 $ semicolonIndex = $ tokens ->getNextTokenOfKind ($ assignmentIndex , ['; ' ]);
107116 \assert (\is_int ($ semicolonIndex ));
108117
109118 $ beforeSemicolonIndex = $ tokens ->getPrevMeaningfulToken ($ semicolonIndex );
110119 \assert (\is_int ($ beforeSemicolonIndex ));
111120
112- $ map = [
113- \T_DNUMBER => [\T_STRING , 'float ' ],
114- \T_LNUMBER => [\T_STRING , 'int ' ],
115- \T_CONSTANT_ENCAPSED_STRING => [\T_STRING , 'string ' ],
116- CT ::T_ARRAY_SQUARE_BRACE_CLOSE => [CT ::T_ARRAY_TYPEHINT , 'array ' ],
117- ];
118-
119121 $ tokenId = $ tokens [$ beforeSemicolonIndex ]->getId ();
120122
123+ if (isset (self ::TOKEN_TO_TYPE_MAP [$ tokenId ])) {
124+ return self ::TOKEN_TO_TYPE_MAP [$ tokenId ];
125+ }
126+
121127 if ($ tokens [$ beforeSemicolonIndex ]->isGivenKind (\T_STRING )) {
122128 $ lowercasedContent = \strtolower ($ tokens [$ beforeSemicolonIndex ]->getContent ());
123129 if (\in_array ($ lowercasedContent , ['false ' , 'true ' , 'null ' ], true )) {
124- return new Token ([\ T_STRING , $ lowercasedContent]) ;
130+ return $ lowercasedContent ;
125131 }
126132 }
127133
128- if ($ tokenId === null && $ tokens [$ beforeSemicolonIndex ]->equals (') ' )) {
134+ if ($ tokens [$ beforeSemicolonIndex ]->equals (') ' )) {
129135 $ openParenthesisIndex = $ tokens ->findBlockStart (Tokens::BLOCK_TYPE_PARENTHESIS_BRACE , $ beforeSemicolonIndex );
130136
131137 $ arrayIndex = $ tokens ->getPrevMeaningfulToken ($ openParenthesisIndex );
132138 \assert (\is_int ($ arrayIndex ));
133139
134140 if ($ tokens [$ arrayIndex ]->isGivenKind (\T_ARRAY )) {
135- $ tokenId = CT :: T_ARRAY_SQUARE_BRACE_CLOSE ;
141+ return ' array ' ;
136142 }
137143 }
138144
139- if (isset ($ map [$ tokenId ])) {
140- return new Token ($ map [$ tokenId ]);
141- }
142-
143- return new Token ([\T_STRING , 'mixed ' ]);
145+ return 'mixed ' ;
144146 }
145147}
0 commit comments