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 |
||
18 | class OSRef extends TransverseMercator |
||
19 | { |
||
20 | const GRID_LETTERS = 'VWXYZQRSTULMNOPFGHJKABCDE'; |
||
21 | |||
22 | 3 | public function getReferenceEllipsoid() |
|
26 | |||
27 | 5 | public function getScaleFactor() |
|
31 | |||
32 | 5 | public function getOriginNorthing() |
|
36 | |||
37 | 5 | public function getOriginEasting() |
|
41 | |||
42 | 5 | public function getOriginLatitude() |
|
46 | |||
47 | 5 | 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 | 17 | 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 | * |
||
71 | * @return OSRef |
||
72 | */ |
||
73 | 2 | public static function fromSixFigureReference($ref) |
|
89 | |||
90 | /** |
||
91 | * Convert this grid reference into a grid reference string of a |
||
92 | * given length (2, 4, 6, 8 or 10) including the two-character |
||
93 | * designation for the 100km square. e.g. TG514131. |
||
94 | * |
||
95 | * @return string |
||
96 | */ |
||
97 | 7 | private function toGridReference($length) |
|
119 | |||
120 | /** |
||
121 | * Convert this grid reference into a string using a standard two-figure |
||
122 | * grid reference including the two-character designation for the 100km |
||
123 | * square. e.g. TG51 (10km square). |
||
124 | * |
||
125 | * @return string |
||
126 | */ |
||
127 | 1 | public function toTwoFigureReference() |
|
131 | |||
132 | /** |
||
133 | * Convert this grid reference into a string using a standard four-figure |
||
134 | * grid reference including the two-character designation for the 100km |
||
135 | * square. e.g. TG5113 (1km square). |
||
136 | * |
||
137 | * @return string |
||
138 | */ |
||
139 | 1 | public function toFourFigureReference() |
|
143 | |||
144 | /** |
||
145 | * Convert this grid reference into a string using a standard six-figure |
||
146 | * grid reference including the two-character designation for the 100km |
||
147 | * square. e.g. TG514131 (100m square). |
||
148 | * |
||
149 | * @return string |
||
150 | */ |
||
151 | 3 | public function toSixFigureReference() |
|
155 | |||
156 | /** |
||
157 | * Convert this grid reference into a string using a standard eight-figure |
||
158 | * grid reference including the two-character designation for the 100km |
||
159 | * square. e.g. TG51431312 (10m square). |
||
160 | * |
||
161 | * @return string |
||
162 | */ |
||
163 | 1 | public function toEightFigureReference() |
|
167 | |||
168 | /** |
||
169 | * Convert this grid reference into a string using a standard ten-figure |
||
170 | * grid reference including the two-character designation for the 100km |
||
171 | * square. e.g. TG5143113121 (1m square). |
||
172 | * |
||
173 | * @return string |
||
174 | */ |
||
175 | 1 | public function toTenFigureReference() |
|
179 | |||
180 | /** |
||
181 | * Convert this grid reference into a latitude and longitude. |
||
182 | * |
||
183 | * @return LatLng |
||
184 | */ |
||
185 | 3 | View Code Duplication | public function toLatLng() |
196 | |||
197 | /** |
||
198 | * String version of coordinate. |
||
199 | * |
||
200 | * @return string |
||
201 | */ |
||
202 | 5 | public function __toString() |
|
206 | } |
||
207 |
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.