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) |
||
123 | |||
124 | /** |
||
125 | * Variable dumper. This is the oldest spiral function originally written in 2007. :) |
||
126 | * |
||
127 | * @param mixed $value |
||
128 | * @param string $name Variable name, internal. |
||
129 | * @param int $level Dumping level, internal. |
||
130 | * @param bool $hideHeader Hide array/object header, internal. |
||
131 | * @return string |
||
132 | */ |
||
133 | private function dumpValue($value, $name = '', $level = 0, $hideHeader = false) |
||
196 | |||
197 | /** |
||
198 | * @param array $array |
||
199 | * @param int $level |
||
200 | * @param bool $hideHeader |
||
201 | * @return string |
||
202 | */ |
||
203 | private function dumpArray(array $array, $level, $hideHeader = false) |
||
236 | |||
237 | /** |
||
238 | * @param object $object |
||
239 | * @param int $level |
||
240 | * @param bool $hideHeader |
||
241 | * @param string $class |
||
242 | * @return string |
||
243 | */ |
||
244 | private function dumpObject($object, $level, $hideHeader = false, $class = '') |
||
291 | |||
292 | /** |
||
293 | * @param object $object |
||
294 | * @param \ReflectionProperty $property |
||
295 | * @param int $level |
||
296 | * @return string |
||
297 | */ |
||
298 | private function dumpProperty($object, \ReflectionProperty $property, $level) |
||
328 | |||
329 | /** |
||
330 | * Property access level label. |
||
331 | * |
||
332 | * @param \ReflectionProperty $property |
||
333 | * @return string |
||
334 | */ |
||
335 | private function getAccess(\ReflectionProperty $property) |
||
345 | } |
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.