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 | * Declaring to IoC that class is singleton. |
||
| 27 | */ |
||
| 28 | const SINGLETON = self::class; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Options for dump() function to specify output. |
||
| 32 | */ |
||
| 33 | const OUTPUT_ECHO = 0; |
||
| 34 | const OUTPUT_RETURN = 1; |
||
| 35 | const OUTPUT_LOG = 2; |
||
| 36 | const OUTPUT_LOG_NICE = 3; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Deepest level to be dumped. |
||
| 40 | * |
||
| 41 | * @var int |
||
| 42 | */ |
||
| 43 | private $maxLevel = 10; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @invisible |
||
| 47 | * @var Style |
||
| 48 | */ |
||
| 49 | private $style = null; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @param int $maxLevel |
||
| 53 | * @param Style $styler Light styler to be used by default. |
||
| 54 | * @param LoggerInterface $logger |
||
| 55 | */ |
||
| 56 | public function __construct( |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Set dump styler. |
||
| 68 | * |
||
| 69 | * @param Style $style |
||
| 70 | * @return $this |
||
| 71 | */ |
||
| 72 | public function setStyle(Style $style) |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Dump specified value. |
||
| 81 | * |
||
| 82 | * @param mixed $value |
||
| 83 | * @param int $output |
||
| 84 | * @return null|string |
||
| 85 | */ |
||
| 86 | public function dump($value, $output = self::OUTPUT_ECHO) |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Variable dumper. This is the oldest spiral function originally written in 2007. :) |
||
| 127 | * |
||
| 128 | * @param mixed $value |
||
| 129 | * @param string $name Variable name, internal. |
||
| 130 | * @param int $level Dumping level, internal. |
||
| 131 | * @param bool $hideHeader Hide array/object header, internal. |
||
| 132 | * @return string |
||
| 133 | */ |
||
| 134 | private function dumpValue($value, $name = '', $level = 0, $hideHeader = false) |
||
| 197 | |||
| 198 | /** |
||
| 199 | * @param array $array |
||
| 200 | * @param int $level |
||
| 201 | * @param bool $hideHeader |
||
| 202 | * @return string |
||
| 203 | */ |
||
| 204 | private function dumpArray(array $array, $level, $hideHeader = false) |
||
| 237 | |||
| 238 | /** |
||
| 239 | * @param object $object |
||
| 240 | * @param int $level |
||
| 241 | * @param bool $hideHeader |
||
| 242 | * @param string $class |
||
| 243 | * @return string |
||
| 244 | */ |
||
| 245 | private function dumpObject($object, $level, $hideHeader = false, $class = '') |
||
| 287 | |||
| 288 | /** |
||
| 289 | * @param object $object |
||
| 290 | * @param \ReflectionProperty $property |
||
| 291 | * @param int $level |
||
| 292 | * @return string |
||
| 293 | */ |
||
| 294 | private function dumpProperty($object, \ReflectionProperty $property, $level) |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Property access level label. |
||
| 327 | * |
||
| 328 | * @param \ReflectionProperty $property |
||
| 329 | * @return string |
||
| 330 | */ |
||
| 331 | private function getAccess(\ReflectionProperty $property) |
||
| 341 | } |
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.