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 |
||
17 | class OSRef extends TransverseMercator |
||
18 | { |
||
19 | |||
20 | const GRID_LETTERS = "VWXYZQRSTULMNOPFGHJKABCDE"; |
||
21 | |||
22 | 2 | public function getReferenceEllipsoid() |
|
26 | |||
27 | 4 | public function getScaleFactor() |
|
31 | |||
32 | 4 | public function getOriginNorthing() |
|
36 | |||
37 | 4 | public function getOriginEasting() |
|
41 | |||
42 | 4 | public function getOriginLatitude() |
|
46 | |||
47 | 4 | public function getOriginLongitude() |
|
51 | |||
52 | /** |
||
53 | * Create a new object representing a OSGB reference. |
||
54 | * |
||
55 | * @param int $x |
||
56 | * @param int $y |
||
57 | * @param int $z |
||
58 | */ |
||
59 | 12 | public function __construct($x, $y, $z = 0) |
|
63 | |||
64 | /** |
||
65 | * Take a string formatted as a six-figure OS grid reference (e.g. |
||
66 | * "TG514131") and return a reference to an OSRef object that represents |
||
67 | * that grid reference. |
||
68 | * |
||
69 | * @param string $ref |
||
70 | * @return OSRef |
||
71 | */ |
||
72 | 2 | public static function fromSixFigureReference($ref) |
|
88 | |||
89 | /** |
||
90 | * Convert this grid reference into a string using a standard six-figure |
||
91 | * grid reference including the two-character designation for the 100km |
||
92 | * square. e.g. TG514131. |
||
93 | * @return string |
||
94 | */ |
||
95 | 3 | View Code Duplication | public function toSixFigureReference() |
117 | |||
118 | /** |
||
119 | * Convert this grid reference into a string using a standard eight-figure |
||
120 | * grid reference including the two-character designation for the 100km |
||
121 | * square. e.g. TG51411311. |
||
122 | * @return string |
||
123 | */ |
||
124 | 1 | View Code Duplication | public function toEightFigureReference() |
146 | |||
147 | /** |
||
148 | * Convert this grid reference into a string using a standard ten-figure |
||
149 | * grid reference including the two-character designation for the 100km |
||
150 | * square. e.g. TG5141213112. |
||
151 | * @return string |
||
152 | */ |
||
153 | 1 | View Code Duplication | public function toTenFigureReference() |
175 | |||
176 | /** |
||
177 | * Convert this grid reference into a latitude and longitude |
||
178 | * @return LatLng |
||
179 | */ |
||
180 | 2 | View Code Duplication | public function toLatLng() |
191 | |||
192 | /** |
||
193 | * String version of coordinate. |
||
194 | * @return string |
||
195 | */ |
||
196 | 5 | public function __toString() |
|
200 | } |
||
201 |
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.