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 |
||
12 | class Cluster extends Point implements IteratorAggregate, Countable |
||
13 | { |
||
14 | /** |
||
15 | * @var Space |
||
16 | */ |
||
17 | protected $space; |
||
18 | |||
19 | /** |
||
20 | * @var SplObjectStorage|Point[] |
||
21 | */ |
||
22 | protected $points; |
||
23 | |||
24 | /** |
||
25 | * @param Space $space |
||
26 | * @param array $coordinates |
||
27 | */ |
||
28 | public function __construct(Space $space, array $coordinates) |
||
34 | |||
35 | /** |
||
36 | * @return array |
||
37 | */ |
||
38 | public function getPoints() |
||
47 | |||
48 | /** |
||
49 | * @return array |
||
50 | */ |
||
51 | public function toArray() |
||
58 | |||
59 | /** |
||
60 | * @param Point $point |
||
61 | * |
||
62 | * @return Point |
||
63 | */ |
||
64 | public function attach(Point $point) |
||
74 | |||
75 | /** |
||
76 | * @param Point $point |
||
77 | * |
||
78 | * @return Point |
||
79 | */ |
||
80 | public function detach(Point $point) |
||
86 | |||
87 | /** |
||
88 | * @param SplObjectStorage $points |
||
89 | */ |
||
90 | public function attachAll(SplObjectStorage $points) |
||
94 | |||
95 | /** |
||
96 | * @param SplObjectStorage $points |
||
97 | */ |
||
98 | public function detachAll(SplObjectStorage $points) |
||
102 | |||
103 | public function updateCentroid() |
||
121 | |||
122 | /** |
||
123 | * @return Point[]|SplObjectStorage |
||
124 | */ |
||
125 | public function getIterator() |
||
129 | |||
130 | /** |
||
131 | * @return mixed |
||
132 | */ |
||
133 | public function count() |
||
137 | } |
||
138 |
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.