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 |
||
26 | class NodeCollection extends Collection implements NodeCollectionInterface |
||
27 | { |
||
28 | /** |
||
29 | * @param mixed $value |
||
30 | * |
||
31 | * @return $this |
||
32 | */ |
||
33 | 11 | public function add($value) |
|
40 | |||
41 | /** |
||
42 | * @param callable $fn |
||
43 | * |
||
44 | * @return $this |
||
45 | */ |
||
46 | 2 | public function apply(callable $fn) |
|
57 | |||
58 | /** |
||
59 | * On clone, clone all flows too |
||
60 | */ |
||
61 | 1 | public function __clone() |
|
67 | |||
68 | /** |
||
69 | * @return string |
||
70 | */ |
||
71 | 1 | public function __toString() |
|
75 | |||
76 | /** |
||
77 | * @inheritdoc |
||
78 | */ |
||
79 | 1 | public function getClone() |
|
83 | |||
84 | /** |
||
85 | * Retrieve the first element that matches the optional callback |
||
86 | * |
||
87 | * @param callable|null $fn |
||
88 | * @param mixed|null $default |
||
89 | * |
||
90 | * @return mixed|null |
||
91 | */ |
||
92 | 3 | View Code Duplication | public function first(callable $fn = null, $default = null) |
106 | |||
107 | /** |
||
108 | * Receive the last element that matches the optional callback |
||
109 | * |
||
110 | * @param callable|null $fn |
||
111 | * @param mixed|null $default |
||
112 | * |
||
113 | * @return mixed|null |
||
114 | */ |
||
115 | 3 | View Code Duplication | public function last(callable $fn = null, $default = null) |
129 | } |
||
130 |
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.