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 |
||
11 | View Code Duplication | class Paths implements Arrayable { |
|
|
|||
12 | |||
13 | use ExtensionPart; |
||
14 | |||
15 | /** @var Map */ |
||
16 | private $paths; |
||
17 | |||
18 | 9 | public function __construct($contents = null) { |
|
21 | |||
22 | 9 | private function parse($contents) { |
|
36 | |||
37 | 6 | public function toArray() { |
|
42 | |||
43 | 1 | public function size() { |
|
46 | |||
47 | /** |
||
48 | * Returns whether a path with the given name exists |
||
49 | * |
||
50 | * @param string $path |
||
51 | * @return boolean |
||
52 | */ |
||
53 | 1 | public function has($path) { |
|
56 | |||
57 | /** |
||
58 | * Returns whether the given path exists |
||
59 | * |
||
60 | * @param Path $path |
||
61 | * @return boolean |
||
62 | */ |
||
63 | 1 | public function contains(Path $path) { |
|
66 | |||
67 | /** |
||
68 | * Returns the path info for the given path |
||
69 | * |
||
70 | * @param string $path |
||
71 | * @return Path |
||
72 | */ |
||
73 | 2 | public function get($path) { |
|
79 | |||
80 | /** |
||
81 | * Sets the path |
||
82 | * |
||
83 | * @param Path $path |
||
84 | * @return $this |
||
85 | */ |
||
86 | 1 | public function add(Path $path) { |
|
90 | |||
91 | /** |
||
92 | * Removes the given path |
||
93 | * |
||
94 | * @param string $path |
||
95 | */ |
||
96 | 1 | public function remove($path) { |
|
100 | } |
||
101 |
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.