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 |
||
16 | class Polyline implements GeometryInterface |
||
17 | { |
||
18 | use GetBoundsTrait; |
||
19 | |||
20 | /** |
||
21 | * @var Coordinate[] |
||
22 | */ |
||
23 | protected $points = []; |
||
24 | |||
25 | /** |
||
26 | * @param Coordinate $point |
||
27 | * |
||
28 | * @return void |
||
29 | */ |
||
30 | public function addPoint(Coordinate $point): void |
||
34 | |||
35 | /** |
||
36 | * @param array $points |
||
37 | */ |
||
38 | public function addPoints(array $points): void |
||
44 | |||
45 | /** |
||
46 | * Adds an unique point to the polyline. A maximum allowed distance for |
||
47 | * same point comparison can be provided. Default allowed distance |
||
48 | * deviation is 0.001 meters (1 millimeter). |
||
49 | * |
||
50 | * @param Coordinate $point |
||
51 | * @param float $allowedDistance |
||
52 | * |
||
53 | * @return void |
||
54 | */ |
||
55 | public function addUniquePoint(Coordinate $point, float $allowedDistance = .001): void |
||
63 | |||
64 | /** |
||
65 | * @return Coordinate[] |
||
66 | */ |
||
67 | public function getPoints(): array |
||
71 | |||
72 | /** |
||
73 | * @return int |
||
74 | */ |
||
75 | public function getNumberOfPoints(): int |
||
79 | |||
80 | /** |
||
81 | * @param Coordinate $point |
||
82 | * @param float $allowedDistance |
||
83 | * |
||
84 | * @return bool |
||
85 | */ |
||
86 | public function containsPoint(Coordinate $point, float $allowedDistance = .001): bool |
||
96 | |||
97 | /** |
||
98 | * @param FormatterInterface $formatter |
||
99 | * |
||
100 | * @return string |
||
101 | */ |
||
102 | public function format(FormatterInterface $formatter): string |
||
106 | |||
107 | /** |
||
108 | * @return Line[] |
||
109 | */ |
||
110 | public function getSegments(): array |
||
125 | |||
126 | /** |
||
127 | * Calculates the length of the polyline. |
||
128 | * |
||
129 | * @param DistanceInterface $calculator instance of distance calculation class |
||
130 | * |
||
131 | * @return float |
||
132 | */ |
||
133 | View Code Duplication | public function getLength(DistanceInterface $calculator): float |
|
147 | |||
148 | /** |
||
149 | * Create a new polyline with reversed order of points, i. e. reversed |
||
150 | * polyline direction. |
||
151 | * |
||
152 | * @return Polyline |
||
153 | */ |
||
154 | View Code Duplication | public function getReverse(): Polyline |
|
164 | |||
165 | /** |
||
166 | * Returns the point which is defined by the avarages of all |
||
167 | * latitude/longitude values. |
||
168 | * |
||
169 | * This currently only works for polylines which don't cross the dateline at |
||
170 | * 180/-180 degrees longitude. |
||
171 | * |
||
172 | * @return Coordinate |
||
173 | * |
||
174 | * @throws InvalidGeometryException when the polyline doesn't contain any points. |
||
175 | */ |
||
176 | public function getAveragePoint(): Coordinate |
||
197 | } |
||
198 |
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.