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 | * Constructor |
||
| 93 | * |
||
| 94 | * @param string $type |
||
| 95 | */ |
||
| 96 | 42 | public function __construct(string $type) |
|
| 97 | { |
||
| 98 | 42 | $type = trim($type); |
|
| 99 | 42 | $this->fullTypeString = $type; |
|
| 100 | 42 | $this->parseType($type); |
|
| 101 | 41 | } |
|
| 102 | |||
| 103 | /** |
||
| 104 | * Create a new instance from a variable |
||
| 105 | * |
||
| 106 | * @param mixed $variable |
||
| 107 | * @return TypeToken |
||
| 108 | */ |
||
| 109 | 8 | public static function createFromVariable($variable): TypeToken |
|
| 110 | { |
||
| 111 | 8 | return \is_object($variable) ? new self(\get_class($variable)) : new self(\gettype($variable)); |
|
| 112 | } |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Returns the class if an object, or the type as a string |
||
| 116 | * |
||
| 117 | * @return string |
||
| 118 | */ |
||
| 119 | 8 | public function getRawType(): string |
|
| 123 | |||
| 124 | /** |
||
| 125 | * Returns the core php type |
||
| 126 | * |
||
| 127 | * @return string |
||
| 128 | */ |
||
| 129 | 2 | public function getPhpType(): string |
|
| 133 | |||
| 134 | /** |
||
| 135 | * Returns an array of generic types |
||
| 136 | * |
||
| 137 | * @return TypeToken[] |
||
| 138 | */ |
||
| 139 | 34 | public function getGenerics(): array |
|
| 143 | |||
| 144 | /** |
||
| 145 | * Returns true if the type matches the class, parent, full type, or one of the interfaces |
||
| 146 | * |
||
| 147 | * @param string $type |
||
| 148 | * @return bool |
||
| 149 | */ |
||
| 150 | 4 | public function isA(string $type): bool |
|
| 166 | |||
| 167 | /** |
||
| 168 | * Returns true if this is a string |
||
| 169 | * |
||
| 170 | * @return bool |
||
| 171 | */ |
||
| 172 | 2 | public function isString(): bool |
|
| 176 | |||
| 177 | /** |
||
| 178 | * Returns true if this is an integer |
||
| 179 | * |
||
| 180 | * @return bool |
||
| 181 | */ |
||
| 182 | 3 | public function isInteger(): bool |
|
| 186 | |||
| 187 | /** |
||
| 188 | * Returns true if this is a float |
||
| 189 | * |
||
| 190 | * @return bool |
||
| 191 | */ |
||
| 192 | 2 | public function isFloat(): bool |
|
| 196 | |||
| 197 | /** |
||
| 198 | * Returns true if this is a boolean |
||
| 199 | * |
||
| 200 | * @return bool |
||
| 201 | */ |
||
| 202 | 2 | public function isBoolean(): bool |
|
| 206 | |||
| 207 | /** |
||
| 208 | * Returns true if this is an array |
||
| 209 | * |
||
| 210 | * @return bool |
||
| 211 | */ |
||
| 212 | 6 | public function isArray(): bool |
|
| 216 | |||
| 217 | /** |
||
| 218 | * Returns true if this is an object |
||
| 219 | * |
||
| 220 | * @return bool |
||
| 221 | */ |
||
| 222 | 41 | public function isObject(): bool |
|
| 226 | |||
| 227 | /** |
||
| 228 | * Returns true if this is null |
||
| 229 | * |
||
| 230 | * @return bool |
||
| 231 | */ |
||
| 232 | 2 | public function isNull(): bool |
|
| 236 | |||
| 237 | /** |
||
| 238 | * Returns true if this is a resource |
||
| 239 | * |
||
| 240 | * @return bool |
||
| 241 | */ |
||
| 242 | 1 | public function isResource(): bool |
|
| 246 | |||
| 247 | /** |
||
| 248 | * Returns true if the type could be anything |
||
| 249 | * |
||
| 250 | * @return bool |
||
| 251 | */ |
||
| 252 | 1 | public function isWildcard(): bool |
|
| 256 | |||
| 257 | /** |
||
| 258 | * Return the initial type including generics |
||
| 259 | * |
||
| 260 | * @return string |
||
| 261 | */ |
||
| 262 | 17 | public function __toString(): string |
|
| 266 | |||
| 267 | /** |
||
| 268 | * Recursively parse type. If generics are found, this will create |
||
| 269 | * new PhpTypes. |
||
| 270 | * |
||
| 271 | * @param string $type |
||
| 272 | * @return void |
||
| 273 | * @throws \Tebru\PhpType\Exception\MalformedTypeException If the type cannot be processed |
||
| 274 | */ |
||
| 275 | 42 | private function parseType(string $type): void |
|
| 336 | |||
| 337 | /** |
||
| 338 | * Create a type enum and set the class if necessary |
||
| 339 | * |
||
| 340 | * @param string $rawType |
||
| 341 | * @return void |
||
| 342 | */ |
||
| 343 | 41 | private function setTypes(string $rawType): void |
|
| 368 | |||
| 369 | /** |
||
| 370 | * Get a normalized core php type |
||
| 371 | * |
||
| 372 | * @param string $type |
||
| 373 | * @return string |
||
| 374 | */ |
||
| 375 | 41 | private function getNormalizedType(string $type): string |
|
| 402 | |||
| 403 | /** |
||
| 404 | * Take an array documented like string[] and convert to array<string> |
||
| 405 | * |
||
| 406 | * @param $type |
||
| 407 | * @return string |
||
| 408 | */ |
||
| 409 | 2 | private function convertArrayToGeneric($type): string |
|
| 424 | } |
||
| 425 |