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 |
||
28 | class Polygon implements GeometryInterface |
||
29 | { |
||
30 | /** |
||
31 | * @var array |
||
32 | */ |
||
33 | protected $points = []; |
||
34 | |||
35 | /** |
||
36 | * @param Coordinate $point |
||
37 | */ |
||
38 | public function addPoint(Coordinate $point) |
||
42 | |||
43 | /** |
||
44 | * @return array |
||
45 | */ |
||
46 | public function getPoints() |
||
50 | |||
51 | /** |
||
52 | * Return all polygon point's latitudes. |
||
53 | * |
||
54 | * @return float |
||
55 | */ |
||
56 | public function getLats() |
||
66 | |||
67 | /** |
||
68 | * Return all polygon point's longitudes. |
||
69 | * |
||
70 | * @return float |
||
71 | */ |
||
72 | public function getLngs() |
||
82 | |||
83 | /** |
||
84 | * @return int |
||
85 | */ |
||
86 | public function getNumberOfPoints() |
||
90 | |||
91 | /** |
||
92 | * @param FormatterInterface $formatter |
||
93 | * |
||
94 | * @return mixed |
||
95 | */ |
||
96 | public function format(FormatterInterface $formatter) |
||
100 | |||
101 | /** |
||
102 | * @return array |
||
103 | */ |
||
104 | public function getSegments() |
||
125 | |||
126 | /** |
||
127 | * Determine if given geometry is contained inside the polygon. This is |
||
128 | * assumed to be true, if each point of the geometry is inside the polygon. |
||
129 | * |
||
130 | * Edge cases: |
||
131 | * |
||
132 | * - it's not detected when a line between two points is outside the polygon |
||
133 | * - @see contains() for more restrictions |
||
134 | * |
||
135 | * @param GeometryInterface $geometry |
||
136 | * |
||
137 | * @return boolean |
||
138 | */ |
||
139 | public function containsGeometry(GeometryInterface $geometry) |
||
150 | |||
151 | /** |
||
152 | * Determine if given point is contained inside the polygon. Uses the PNPOLY |
||
153 | * algorithm by W. Randolph Franklin. Therfore some edge cases may not give the |
||
154 | * expected results, e. g. if the point resides on the polygon boundary. |
||
155 | * |
||
156 | * @see http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html |
||
157 | * |
||
158 | * For special cases this calculation leads to wrong results: |
||
159 | * |
||
160 | * - if the polygons spans over the longitude boundaries at 180/-180 degrees |
||
161 | * |
||
162 | * @param Coordinate $point |
||
163 | * |
||
164 | * @return boolean |
||
165 | */ |
||
166 | public function contains(Coordinate $point) |
||
188 | |||
189 | /** |
||
190 | * Calculates the polygon perimeter. |
||
191 | * |
||
192 | * @param DistanceInterface $calculator instance of distance calculation class |
||
193 | * |
||
194 | * @return float |
||
195 | */ |
||
196 | View Code Duplication | public function getPerimeter(DistanceInterface $calculator) |
|
210 | |||
211 | /** |
||
212 | * Calculates the polygon area. |
||
213 | * |
||
214 | * @return float |
||
215 | * |
||
216 | * @fixme This calculation gives wrong results, please don't use it! |
||
217 | */ |
||
218 | public function getArea() |
||
235 | } |
||
236 |
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.