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 | /** |
||
24 | * Static method instance(). Used in dump() function. |
||
25 | */ |
||
26 | use LoggerTrait, BenchmarkTrait; |
||
27 | |||
28 | /** |
||
29 | * Declaring to IoC that class is singleton. |
||
30 | */ |
||
31 | const SINGLETON = self::class; |
||
32 | |||
33 | /** |
||
34 | * Options for dump() function to specify output. |
||
35 | */ |
||
36 | const OUTPUT_ECHO = 0; |
||
37 | const OUTPUT_RETURN = 1; |
||
38 | const OUTPUT_LOG = 2; |
||
39 | const OUTPUT_LOG_NICE = 3; |
||
40 | |||
41 | /** |
||
42 | * Deepest level to be dumped. |
||
43 | * |
||
44 | * @var int |
||
45 | */ |
||
46 | private $maxLevel = 10; |
||
47 | |||
48 | /** |
||
49 | * @invisible |
||
50 | * @var Style |
||
51 | */ |
||
52 | private $style = null; |
||
53 | |||
54 | /** |
||
55 | * @param int $maxLevel |
||
56 | * @param Style $styler Light styler to be used by default. |
||
57 | * @param LoggerInterface $logger |
||
58 | */ |
||
59 | public function __construct( |
||
68 | |||
69 | /** |
||
70 | * Set dump styler. |
||
71 | * |
||
72 | * @param Style $style |
||
73 | * @return $this |
||
74 | */ |
||
75 | public function setStyle(Style $style) |
||
81 | |||
82 | /** |
||
83 | * Dump specified value. |
||
84 | * |
||
85 | * @param mixed $value |
||
86 | * @param int $output |
||
87 | * @return null|string |
||
88 | */ |
||
89 | public function dump($value, $output = self::OUTPUT_ECHO) |
||
127 | |||
128 | /** |
||
129 | * Variable dumper. This is the oldest spiral function, it was originally written in 2007. :) |
||
130 | * |
||
131 | * @param mixed $value |
||
132 | * @param string $name Variable name, internal. |
||
133 | * @param int $level Dumping level, internal. |
||
134 | * @param bool $hideHeader Hide array/object header, internal. |
||
135 | * @return string |
||
136 | */ |
||
137 | private function dumpValue($value, $name = '', $level = 0, $hideHeader = false) |
||
200 | |||
201 | /** |
||
202 | * @param array $array |
||
203 | * @param int $level |
||
204 | * @param bool $hideHeader |
||
205 | * @return string |
||
206 | */ |
||
207 | private function dumpArray(array $array, $level, $hideHeader = false) |
||
240 | |||
241 | /** |
||
242 | * @param object $object |
||
243 | * @param int $level |
||
244 | * @param bool $hideHeader |
||
245 | * @param string $class |
||
246 | * @return string |
||
247 | */ |
||
248 | private function dumpObject($object, $level, $hideHeader = false, $class = '') |
||
285 | |||
286 | /** |
||
287 | * @param object $object |
||
288 | * @param \ReflectionProperty $property |
||
289 | * @param int $level |
||
290 | * @return string |
||
291 | */ |
||
292 | private function dumpProperty($object, \ReflectionProperty $property, $level) |
||
322 | |||
323 | /** |
||
324 | * Property access level label. |
||
325 | * |
||
326 | * @param \ReflectionProperty $property |
||
327 | * @return string |
||
328 | */ |
||
329 | private function getAccess(\ReflectionProperty $property) |
||
339 | } |
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.