Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 |
||
| 21 | class Dumper extends Component implements SingletonInterface, LoggerAwareInterface |
||
| 22 | { |
||
| 23 | use LoggerTrait, BenchmarkTrait; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Options for dump() function to specify output. |
||
| 27 | */ |
||
| 28 | const OUTPUT_ECHO = 0; |
||
| 29 | const OUTPUT_RETURN = 1; |
||
| 30 | const OUTPUT_LOG = 2; |
||
| 31 | const OUTPUT_LOG_NICE = 3; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Deepest level to be dumped. |
||
| 35 | * |
||
| 36 | * @var int |
||
| 37 | */ |
||
| 38 | private $maxLevel = 10; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @invisible |
||
| 42 | * @var Style |
||
| 43 | */ |
||
| 44 | private $style = null; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @param int $maxLevel |
||
| 48 | * @param Style $styler Light styler to be used by default. |
||
| 49 | * @param LoggerInterface $logger |
||
| 50 | */ |
||
| 51 | public function __construct( |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Set dump styler. |
||
| 63 | * |
||
| 64 | * @param Style $style |
||
| 65 | * @return $this |
||
| 66 | */ |
||
| 67 | public function setStyle(Style $style) |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Dump specified value. |
||
| 76 | * |
||
| 77 | * @param mixed $value |
||
| 78 | * @param int $output |
||
| 79 | * @return null|string |
||
| 80 | */ |
||
| 81 | public function dump($value, $output = self::OUTPUT_ECHO) |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Variable dumper. This is the oldest spiral function originally written in 2007. :) |
||
| 113 | * |
||
| 114 | * @param mixed $value |
||
| 115 | * @param string $name Variable name, internal. |
||
| 116 | * @param int $level Dumping level, internal. |
||
| 117 | * @param bool $hideHeader Hide array/object header, internal. |
||
| 118 | * @return string |
||
| 119 | */ |
||
| 120 | private function dumpValue($value, $name = '', $level = 0, $hideHeader = false) |
||
| 183 | |||
| 184 | /** |
||
| 185 | * @param array $array |
||
| 186 | * @param int $level |
||
| 187 | * @param bool $hideHeader |
||
| 188 | * @return string |
||
| 189 | */ |
||
| 190 | private function dumpArray(array $array, $level, $hideHeader = false) |
||
| 223 | |||
| 224 | /** |
||
| 225 | * @param object $object |
||
| 226 | * @param int $level |
||
| 227 | * @param bool $hideHeader |
||
| 228 | * @param string $class |
||
| 229 | * @return string |
||
| 230 | */ |
||
| 231 | private function dumpObject($object, $level, $hideHeader = false, $class = '') |
||
| 278 | |||
| 279 | /** |
||
| 280 | * @param object $object |
||
| 281 | * @param \ReflectionProperty $property |
||
| 282 | * @param int $level |
||
| 283 | * @return string |
||
| 284 | */ |
||
| 285 | private function dumpProperty($object, \ReflectionProperty $property, $level) |
||
| 315 | |||
| 316 | /** |
||
| 317 | * Property access level label. |
||
| 318 | * |
||
| 319 | * @param \ReflectionProperty $property |
||
| 320 | * @return string |
||
| 321 | */ |
||
| 322 | private function getAccess(\ReflectionProperty $property) |
||
| 332 | } |
The break statement is not necessary if it is preceded for example by a return statement:
If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.