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:
1 | <?php |
||
17 | class DumperManager |
||
18 | { |
||
19 | /** |
||
20 | * @var array |
||
21 | */ |
||
22 | private $dumpers; |
||
23 | |||
24 | /** |
||
25 | * Constructor. |
||
26 | * |
||
27 | * @param array $dumpers |
||
28 | */ |
||
29 | public function __construct(array $dumpers = array()) |
||
37 | |||
38 | /** |
||
39 | * Get a dumper. |
||
40 | * |
||
41 | * @param string $name The name of the dumper |
||
42 | * |
||
43 | * @return Dumper |
||
44 | * |
||
45 | * @throws \RuntimeException If no dumper was found |
||
46 | */ |
||
47 | View Code Duplication | public function get($name) |
|
55 | |||
56 | /** |
||
57 | * Sets a dumper. |
||
58 | * |
||
59 | * @param string $name The name |
||
60 | * @param Dumper $dumper The dumper instance |
||
61 | */ |
||
62 | public function set($name, Dumper $dumper) |
||
66 | |||
67 | /** |
||
68 | * Remove a dumper instance from the manager. |
||
69 | * |
||
70 | * @param string $name The name of the dumper |
||
71 | * |
||
72 | * @throws \RuntimeException If no dumper was found |
||
73 | */ |
||
74 | View Code Duplication | public function remove($name) |
|
82 | |||
83 | /** |
||
84 | * Return true if $name exists, false otherwise. |
||
85 | * |
||
86 | * @param $name The name of the dumper |
||
87 | * |
||
88 | * @return bool |
||
89 | */ |
||
90 | public function has($name) |
||
94 | } |
||
95 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.