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 | public function __construct(Space $space, array $coordinates) |
||
30 | |||
31 | public function getPoints(): array |
||
40 | |||
41 | public function getPointsWithLabels(): array |
||
50 | |||
51 | public function toArray(): array |
||
58 | |||
59 | public function attach(Point $point): Point |
||
69 | |||
70 | public function detach(Point $point): Point |
||
76 | |||
77 | public function attachAll(SplObjectStorage $points): void |
||
81 | |||
82 | public function detachAll(SplObjectStorage $points): void |
||
86 | |||
87 | public function updateCentroid(): void |
||
106 | |||
107 | /** |
||
108 | * @return Point[]|SplObjectStorage |
||
109 | */ |
||
110 | public function getIterator() |
||
114 | |||
115 | /** |
||
116 | * @return mixed |
||
117 | */ |
||
118 | public function count() |
||
122 | |||
123 | public function setCoordinates(array $newCoordinates): void |
||
127 | } |
||
128 |
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.