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 | private $fullTypeString; |
||
62 | |||
63 | /** |
||
64 | * The core php type (string, int, etc) or class if object |
||
65 | * |
||
66 | * @var string |
||
67 | */ |
||
68 | private $rawType; |
||
69 | |||
70 | /** |
||
71 | * The core php type (string, int, object, etc) |
||
72 | * |
||
73 | * @var string |
||
74 | */ |
||
75 | private $phpType; |
||
76 | |||
77 | /** |
||
78 | * An array of parent classes and interfaces that a class implements |
||
79 | * |
||
80 | * @var array |
||
81 | */ |
||
82 | private $parents = []; |
||
83 | |||
84 | /** |
||
85 | * Generic types, if they exist |
||
86 | * |
||
87 | * @var array |
||
88 | */ |
||
89 | private $genericTypes = []; |
||
90 | |||
91 | /** |
||
92 | * An array of cached types |
||
93 | * |
||
94 | * @var TypeToken[] |
||
95 | */ |
||
96 | private static $types = []; |
||
97 | |||
98 | /** |
||
99 | * Constructor |
||
100 | * |
||
101 | * @param string $type |
||
102 | */ |
||
103 | 38 | 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 | 30 | 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 this is an array |
||
233 | * |
||
234 | * @return bool |
||
235 | */ |
||
236 | 4 | public function isArray(): bool |
|
240 | |||
241 | /** |
||
242 | * Returns true if this is an object |
||
243 | * |
||
244 | * @return bool |
||
245 | */ |
||
246 | 37 | public function isObject(): bool |
|
250 | |||
251 | /** |
||
252 | * Returns true if this is null |
||
253 | * |
||
254 | * @return bool |
||
255 | */ |
||
256 | 2 | public function isNull(): bool |
|
260 | |||
261 | /** |
||
262 | * Returns true if this is a resource |
||
263 | * |
||
264 | * @return bool |
||
265 | */ |
||
266 | 1 | public function isResource(): bool |
|
270 | |||
271 | /** |
||
272 | * Returns true if the type could be anything |
||
273 | * |
||
274 | * @return bool |
||
275 | */ |
||
276 | 1 | public function isWildcard(): bool |
|
280 | |||
281 | /** |
||
282 | * Return the initial type including generics |
||
283 | * |
||
284 | * @return string |
||
285 | */ |
||
286 | 15 | public function __toString(): string |
|
290 | |||
291 | /** |
||
292 | * Recursively parse type. If generics are found, this will create |
||
293 | * new PhpTypes. |
||
294 | * |
||
295 | * @param string $type |
||
296 | * @return void |
||
297 | * @throws \Tebru\PhpType\Exception\MalformedTypeException If the type cannot be processed |
||
298 | */ |
||
299 | 38 | private function parseType(string $type): void |
|
352 | |||
353 | /** |
||
354 | * Create a type enum and set the class if necessary |
||
355 | * |
||
356 | * @param string $rawType |
||
357 | * @return void |
||
358 | */ |
||
359 | 37 | private function setTypes(string $rawType): void |
|
384 | |||
385 | /** |
||
386 | * Get a normalized core php type |
||
387 | * |
||
388 | * @param string $type |
||
389 | * @return string |
||
390 | */ |
||
391 | 37 | private function getNormalizedType(string $type): string |
|
418 | } |
||
419 |