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 Polyline implements GeometryInterface |
||
27 | { |
||
28 | /** |
||
29 | * @var Coordinate[] |
||
30 | */ |
||
31 | protected $points = []; |
||
32 | |||
33 | /** |
||
34 | * @param Coordinate $point |
||
35 | * |
||
36 | * @return void |
||
37 | */ |
||
38 | public function addPoint(Coordinate $point) |
||
42 | |||
43 | /** |
||
44 | * @return Coordinate[] |
||
45 | */ |
||
46 | public function getPoints(): array |
||
50 | |||
51 | /** |
||
52 | * @return int |
||
53 | */ |
||
54 | public function getNumberOfPoints(): int |
||
58 | |||
59 | /** |
||
60 | * @param FormatterInterface $formatter |
||
61 | * |
||
62 | * @return mixed |
||
63 | */ |
||
64 | public function format(FormatterInterface $formatter) |
||
68 | |||
69 | /** |
||
70 | * @return Line[] |
||
71 | */ |
||
72 | public function getSegments(): array |
||
87 | |||
88 | /** |
||
89 | * Calculates the length of the polyline. |
||
90 | * |
||
91 | * @param DistanceInterface $calculator instance of distance calculation class |
||
92 | * |
||
93 | * @return float |
||
94 | */ |
||
95 | View Code Duplication | public function getLength(DistanceInterface $calculator): float |
|
109 | |||
110 | /** |
||
111 | * Create a new polyline with reversed order of points, i. e. reversed |
||
112 | * polyline direction. |
||
113 | * |
||
114 | * @return Polyline |
||
115 | */ |
||
116 | View Code Duplication | public function getReverse(): Polyline |
|
126 | |||
127 | /** |
||
128 | * @return Bounds |
||
129 | */ |
||
130 | public function getBounds(): Bounds |
||
149 | } |
||
150 |
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.