Complex classes like CachingTypeFactory 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 CachingTypeFactory, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 24 | class CachingTypeFactory extends TypeFactory |
||
| 25 | { |
||
| 26 | protected $cache = []; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Add a Uint8 serializer to the template |
||
| 30 | * |
||
| 31 | * @return Uint8 |
||
| 32 | */ |
||
| 33 | 4 | public function uint8(): Uint8 |
|
| 40 | |||
| 41 | /** |
||
| 42 | * Add a little-endian Uint8 serializer to the template |
||
| 43 | * |
||
| 44 | * @return Uint8 |
||
| 45 | */ |
||
| 46 | 4 | public function uint8le(): Uint8 |
|
| 53 | |||
| 54 | /** |
||
| 55 | * Add a Uint16 serializer to the template |
||
| 56 | * |
||
| 57 | * @return Uint16 |
||
| 58 | */ |
||
| 59 | 4 | public function uint16(): Uint16 |
|
| 66 | |||
| 67 | /** |
||
| 68 | * Add a little-endian Uint16 serializer to the template |
||
| 69 | * |
||
| 70 | * @return Uint16 |
||
| 71 | */ |
||
| 72 | 4 | public function uint16le(): Uint16 |
|
| 79 | |||
| 80 | /** |
||
| 81 | * Add a Uint32 serializer to the template |
||
| 82 | * |
||
| 83 | * @return Uint32 |
||
| 84 | */ |
||
| 85 | 4 | public function uint32(): Uint32 |
|
| 92 | |||
| 93 | /** |
||
| 94 | * Add a little-endian Uint32 serializer to the template |
||
| 95 | * |
||
| 96 | * @return Uint32 |
||
| 97 | */ |
||
| 98 | 4 | public function uint32le(): Uint32 |
|
| 105 | |||
| 106 | /** |
||
| 107 | * Add a Uint64 serializer to the template |
||
| 108 | * |
||
| 109 | * @return Uint64 |
||
| 110 | */ |
||
| 111 | 4 | public function uint64(): Uint64 |
|
| 118 | |||
| 119 | /** |
||
| 120 | * Add a little-endian Uint64 serializer to the template |
||
| 121 | * |
||
| 122 | * @return Uint64 |
||
| 123 | */ |
||
| 124 | 4 | public function uint64le(): Uint64 |
|
| 131 | |||
| 132 | /** |
||
| 133 | * Add a Uint128 serializer to the template |
||
| 134 | * |
||
| 135 | * @return Uint128 |
||
| 136 | */ |
||
| 137 | 4 | public function uint128(): Uint128 |
|
| 144 | |||
| 145 | /** |
||
| 146 | * Add a little-endian Uint128 serializer to the template |
||
| 147 | * |
||
| 148 | * @return Uint128 |
||
| 149 | */ |
||
| 150 | 4 | public function uint128le(): Uint128 |
|
| 157 | |||
| 158 | /** |
||
| 159 | * Add a Uint256 serializer to the template |
||
| 160 | * |
||
| 161 | * @return Uint256 |
||
| 162 | */ |
||
| 163 | 4 | public function uint256(): Uint256 |
|
| 170 | |||
| 171 | /** |
||
| 172 | * Add a little-endian Uint256 serializer to the template |
||
| 173 | * |
||
| 174 | * @return Uint256 |
||
| 175 | */ |
||
| 176 | 4 | public function uint256le(): Uint256 |
|
| 183 | |||
| 184 | /** |
||
| 185 | * Add a int8 serializer to the template |
||
| 186 | * |
||
| 187 | * @return Int8 |
||
| 188 | */ |
||
| 189 | 4 | public function int8(): Int8 |
|
| 196 | |||
| 197 | /** |
||
| 198 | * Add a little-endian Int8 serializer to the template |
||
| 199 | * |
||
| 200 | * @return Int8 |
||
| 201 | */ |
||
| 202 | 4 | public function int8le(): Int8 |
|
| 209 | |||
| 210 | /** |
||
| 211 | * Add a int16 serializer to the template |
||
| 212 | * |
||
| 213 | * @return Int16 |
||
| 214 | */ |
||
| 215 | 4 | public function int16(): Int16 |
|
| 222 | |||
| 223 | /** |
||
| 224 | * Add a little-endian Int16 serializer to the template |
||
| 225 | * |
||
| 226 | * @return Int16 |
||
| 227 | */ |
||
| 228 | 4 | public function int16le(): Int16 |
|
| 235 | |||
| 236 | /** |
||
| 237 | * Add a int32 serializer to the template |
||
| 238 | * |
||
| 239 | * @return Int32 |
||
| 240 | */ |
||
| 241 | 4 | public function int32(): Int32 |
|
| 248 | |||
| 249 | /** |
||
| 250 | * Add a little-endian Int serializer to the template |
||
| 251 | * |
||
| 252 | * @return Int32 |
||
| 253 | */ |
||
| 254 | 4 | public function int32le(): Int32 |
|
| 261 | |||
| 262 | /** |
||
| 263 | * Add a int64 serializer to the template |
||
| 264 | * |
||
| 265 | * @return Int64 |
||
| 266 | */ |
||
| 267 | 4 | public function int64(): Int64 |
|
| 274 | |||
| 275 | /** |
||
| 276 | * Add a little-endian Int64 serializer to the template |
||
| 277 | * |
||
| 278 | * @return Int64 |
||
| 279 | */ |
||
| 280 | 4 | public function int64le(): Int64 |
|
| 287 | |||
| 288 | /** |
||
| 289 | * Add a int128 serializer to the template |
||
| 290 | * |
||
| 291 | * @return Int128 |
||
| 292 | */ |
||
| 293 | 4 | public function int128(): Int128 |
|
| 300 | |||
| 301 | /** |
||
| 302 | * Add a little-endian Int128 serializer to the template |
||
| 303 | * |
||
| 304 | * @return Int128 |
||
| 305 | */ |
||
| 306 | 4 | public function int128le(): Int128 |
|
| 313 | |||
| 314 | /** |
||
| 315 | * Add a int256 serializer to the template |
||
| 316 | * |
||
| 317 | * @return Int256 |
||
| 318 | */ |
||
| 319 | 4 | public function int256(): Int256 |
|
| 326 | |||
| 327 | /** |
||
| 328 | * Add a little-endian Int256 serializer to the template |
||
| 329 | * |
||
| 330 | * @return Int256 |
||
| 331 | */ |
||
| 332 | 4 | public function int256le(): Int256 |
|
| 339 | |||
| 340 | /** |
||
| 341 | * Add a VarInt serializer to the template |
||
| 342 | * |
||
| 343 | * @return VarInt |
||
| 344 | */ |
||
| 345 | 4 | public function varint(): VarInt |
|
| 352 | |||
| 353 | /** |
||
| 354 | * Add a VarString serializer to the template |
||
| 355 | * |
||
| 356 | * @return VarString |
||
| 357 | */ |
||
| 358 | 2 | public function varstring(): VarString |
|
| 365 | |||
| 366 | /** |
||
| 367 | * Add a byte string serializer to the template. This serializer requires a length to |
||
| 368 | * pad/truncate to. |
||
| 369 | * |
||
| 370 | * @param int $length |
||
| 371 | * @return ByteString |
||
| 372 | */ |
||
| 373 | public function bytestring(int $length): ByteString |
||
| 380 | |||
| 381 | /** |
||
| 382 | * Add a little-endian byte string serializer to the template. This serializer requires |
||
| 383 | * a length to pad/truncate to. |
||
| 384 | * |
||
| 385 | * @param int $length |
||
| 386 | * @return ByteString |
||
| 387 | */ |
||
| 388 | public function bytestringle(int $length): ByteString |
||
| 395 | |||
| 396 | /** |
||
| 397 | * Add a vector serializer to the template. A $readHandler must be provided if the |
||
| 398 | * template will be used to deserialize a vector, since it's contents are not known. |
||
| 399 | * |
||
| 400 | * The $readHandler should operate on the parser reference, reading the bytes for each |
||
| 401 | * item in the collection. |
||
| 402 | * |
||
| 403 | * @param callable $readHandler |
||
| 404 | * @return Vector |
||
| 405 | */ |
||
| 406 | 2 | public function vector(callable $readHandler): Vector |
|
| 410 | } |
||
| 411 |