Complex classes like Dumper 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 Dumper, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 23 | class Dumper extends Component implements SingletonInterface, LoggerAwareInterface |
||
| 24 | { |
||
| 25 | use LoggerAwareTrait; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Options for dump() function to specify output. |
||
| 29 | */ |
||
| 30 | const OUTPUT_ECHO = 0; |
||
| 31 | const OUTPUT_RETURN = 1; |
||
| 32 | const OUTPUT_LOG = 2; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Deepest level to be dumped. |
||
| 36 | * |
||
| 37 | * @var int |
||
| 38 | */ |
||
| 39 | private $maxLevel = 10; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @invisible |
||
| 43 | * |
||
| 44 | * @var Style |
||
| 45 | */ |
||
| 46 | private $style = null; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @param int $maxLevel |
||
| 50 | * @param Style $style Light styler to be used by default. |
||
| 51 | * @param LoggerInterface $logger |
||
| 52 | */ |
||
| 53 | public function __construct( |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Set dump styler. |
||
| 68 | * |
||
| 69 | * @param Style $style |
||
| 70 | * |
||
| 71 | * @return self |
||
| 72 | */ |
||
| 73 | public function setStyle(Style $style): Dumper |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Dump specified value. Dumper will automatically detect CLI mode in OUTPUT_ECHO mode. |
||
| 82 | * |
||
| 83 | * @param mixed $value |
||
| 84 | * @param int $output |
||
| 85 | * |
||
| 86 | * @return string |
||
| 87 | */ |
||
| 88 | public function dump($value, int $output = self::OUTPUT_ECHO): string |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Variable dumper. This is the oldest spiral function originally written in 2007. :). |
||
| 111 | * |
||
| 112 | * @param mixed $value |
||
| 113 | * @param string $name Variable name, internal. |
||
| 114 | * @param int $level Dumping level, internal. |
||
| 115 | * @param bool $hideHeader Hide array/object header, internal. |
||
| 116 | * |
||
| 117 | * @return string |
||
| 118 | */ |
||
| 119 | private function dumpValue( |
||
| 186 | |||
| 187 | /** |
||
| 188 | * @param array $array |
||
| 189 | * @param int $level |
||
| 190 | * @param bool $hideHeader |
||
| 191 | * |
||
| 192 | * @return string |
||
| 193 | */ |
||
| 194 | private function dumpArray(array $array, int $level, bool $hideHeader = false): string |
||
| 227 | |||
| 228 | /** |
||
| 229 | * @param object $object |
||
| 230 | * @param int $level |
||
| 231 | * @param bool $hideHeader |
||
| 232 | * @param string $class |
||
| 233 | * |
||
| 234 | * @return string |
||
| 235 | */ |
||
| 236 | private function dumpObject( |
||
| 286 | |||
| 287 | /** |
||
| 288 | * @param object $object |
||
| 289 | * @param \ReflectionProperty $property |
||
| 290 | * @param int $level |
||
| 291 | * |
||
| 292 | * @return string |
||
| 293 | */ |
||
| 294 | private function dumpProperty($object, \ReflectionProperty $property, int $level): string |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Fetch information about the closure. |
||
| 327 | * |
||
| 328 | * @param \Closure $closure |
||
| 329 | * |
||
| 330 | * @return array |
||
| 331 | */ |
||
| 332 | private function describeClosure(\Closure $closure): array |
||
| 342 | |||
| 343 | /** |
||
| 344 | * Property access level label. |
||
| 345 | * |
||
| 346 | * @param \ReflectionProperty $property |
||
| 347 | * |
||
| 348 | * @return string |
||
| 349 | */ |
||
| 350 | private function getAccess(\ReflectionProperty $property): string |
||
| 360 | } |
||
| 361 |