Complex classes like TypeToken often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use TypeToken, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
44 | final class TypeToken |
||
45 | { |
||
46 | public const STRING = 'string'; |
||
47 | public const INTEGER = 'integer'; |
||
48 | public const FLOAT = 'float'; |
||
49 | public const BOOLEAN = 'boolean'; |
||
50 | public const HASH = 'array'; |
||
51 | public const OBJECT = 'object'; |
||
52 | public const NULL = 'null'; |
||
53 | public const RESOURCE = 'resource'; |
||
54 | public const WILDCARD = '?'; |
||
55 | |||
56 | /** |
||
57 | * The full initial type |
||
58 | * |
||
59 | * @var string |
||
60 | */ |
||
61 | public $fullTypeString; |
||
62 | |||
63 | /** |
||
64 | * The core php type (string, int, etc) or class if object |
||
65 | * |
||
66 | * @var string |
||
67 | */ |
||
68 | public $rawType; |
||
69 | |||
70 | /** |
||
71 | * The core php type (string, int, object, etc) |
||
72 | * |
||
73 | * @var string |
||
74 | */ |
||
75 | public $phpType; |
||
76 | |||
77 | /** |
||
78 | * An array of parent classes and interfaces that a class implements |
||
79 | * |
||
80 | * @var array |
||
81 | */ |
||
82 | public $parents = []; |
||
83 | |||
84 | /** |
||
85 | * Generic types, if they exist |
||
86 | * |
||
87 | * @var array |
||
88 | */ |
||
89 | public $genericTypes = []; |
||
90 | |||
91 | /** |
||
92 | * An array of cached types |
||
93 | * |
||
94 | * @var TypeToken[] |
||
95 | */ |
||
96 | public static $types = []; |
||
97 | |||
98 | /** |
||
99 | * Constructor |
||
100 | * |
||
101 | * @param string $type |
||
102 | */ |
||
103 | 44 | public function __construct(string $type) |
|
109 | |||
110 | /** |
||
111 | * Singleton factory for creating types |
||
112 | * |
||
113 | * @param string $type |
||
114 | * @return TypeToken |
||
115 | */ |
||
116 | 16 | public static function create(string $type): TypeToken |
|
124 | |||
125 | /** |
||
126 | * Create a new instance from a variable |
||
127 | * |
||
128 | * @param mixed $variable |
||
129 | * @return TypeToken |
||
130 | */ |
||
131 | 9 | public static function createFromVariable($variable): TypeToken |
|
137 | |||
138 | /** |
||
139 | * Returns the class if an object, or the type as a string |
||
140 | * |
||
141 | * @return string |
||
142 | */ |
||
143 | 8 | public function getRawType(): string |
|
147 | |||
148 | /** |
||
149 | * Returns the core php type |
||
150 | * |
||
151 | * @return string |
||
152 | */ |
||
153 | 2 | public function getPhpType(): string |
|
157 | |||
158 | /** |
||
159 | * Returns an array of generic types |
||
160 | * |
||
161 | * @return TypeToken[] |
||
162 | */ |
||
163 | 35 | public function getGenerics(): array |
|
167 | |||
168 | /** |
||
169 | * Returns true if the type matches the class, parent, full type, or one of the interfaces |
||
170 | * |
||
171 | * @param string $type |
||
172 | * @return bool |
||
173 | */ |
||
174 | 4 | public function isA(string $type): bool |
|
190 | |||
191 | /** |
||
192 | * Returns true if this is a string |
||
193 | * |
||
194 | * @return bool |
||
195 | */ |
||
196 | 1 | public function isString(): bool |
|
200 | |||
201 | /** |
||
202 | * Returns true if this is an integer |
||
203 | * |
||
204 | * @return bool |
||
205 | */ |
||
206 | 2 | public function isInteger(): bool |
|
210 | |||
211 | /** |
||
212 | * Returns true if this is a float |
||
213 | * |
||
214 | * @return bool |
||
215 | */ |
||
216 | 2 | public function isFloat(): bool |
|
220 | |||
221 | /** |
||
222 | * Returns true if this is a boolean |
||
223 | * |
||
224 | * @return bool |
||
225 | */ |
||
226 | 2 | public function isBoolean(): bool |
|
230 | |||
231 | /** |
||
232 | * Returns true if the type is a scalar type |
||
233 | * |
||
234 | * @return bool |
||
235 | */ |
||
236 | 6 | public function isScalar(): bool |
|
244 | |||
245 | /** |
||
246 | * Returns true if this is an array |
||
247 | * |
||
248 | * @return bool |
||
249 | */ |
||
250 | 4 | public function isArray(): bool |
|
254 | |||
255 | /** |
||
256 | * Returns true if this is an object |
||
257 | * |
||
258 | * @return bool |
||
259 | */ |
||
260 | 43 | public function isObject(): bool |
|
264 | |||
265 | /** |
||
266 | * Returns true if this is null |
||
267 | * |
||
268 | * @return bool |
||
269 | */ |
||
270 | 2 | public function isNull(): bool |
|
274 | |||
275 | /** |
||
276 | * Returns true if this is a resource |
||
277 | * |
||
278 | * @return bool |
||
279 | */ |
||
280 | 1 | public function isResource(): bool |
|
284 | |||
285 | /** |
||
286 | * Returns true if the type could be anything |
||
287 | * |
||
288 | * @return bool |
||
289 | */ |
||
290 | 1 | public function isWildcard(): bool |
|
294 | |||
295 | /** |
||
296 | * Return the initial type including generics |
||
297 | * |
||
298 | * @return string |
||
299 | */ |
||
300 | 15 | public function __toString(): string |
|
304 | |||
305 | /** |
||
306 | * Recursively parse type. If generics are found, this will create |
||
307 | * new PhpTypes. |
||
308 | * |
||
309 | * @param string $type |
||
310 | * @return void |
||
311 | * @throws \Tebru\PhpType\Exception\MalformedTypeException If the type cannot be processed |
||
312 | */ |
||
313 | 44 | private function parseType(string $type): void |
|
366 | |||
367 | /** |
||
368 | * Create a type enum and set the class if necessary |
||
369 | * |
||
370 | * @param string $rawType |
||
371 | * @return void |
||
372 | */ |
||
373 | 43 | private function setTypes(string $rawType): void |
|
398 | |||
399 | /** |
||
400 | * Get a normalized core php type |
||
401 | * |
||
402 | * @param string $type |
||
403 | * @return string |
||
404 | */ |
||
405 | 43 | private function getNormalizedType(string $type): string |
|
432 | } |
||
433 |