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 |
||
| 15 | abstract class TransverseMercator |
||
| 16 | { |
||
| 17 | |||
| 18 | /** |
||
| 19 | * X |
||
| 20 | * @var float |
||
| 21 | */ |
||
| 22 | protected $x; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Y |
||
| 26 | * @var float |
||
| 27 | */ |
||
| 28 | protected $y; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * h |
||
| 32 | * @var float |
||
| 33 | */ |
||
| 34 | protected $h; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Reference ellipsoid used in this datum |
||
| 38 | * @var RefEll |
||
| 39 | */ |
||
| 40 | protected $refEll; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Cartesian constructor. |
||
| 44 | * @param float $x |
||
| 45 | * @param float $y |
||
| 46 | * @param float $h |
||
| 47 | * @param RefEll $refEll |
||
| 48 | */ |
||
| 49 | 30 | public function __construct($x, $y, $h, RefEll $refEll) |
|
| 56 | |||
| 57 | /** |
||
| 58 | * String version of coordinate. |
||
| 59 | * @return string |
||
| 60 | */ |
||
| 61 | public function __toString() |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @return float |
||
| 68 | */ |
||
| 69 | 2 | public function getX() |
|
| 73 | |||
| 74 | /** |
||
| 75 | * @param float $x |
||
| 76 | */ |
||
| 77 | 30 | public function setX($x) |
|
| 81 | |||
| 82 | /** |
||
| 83 | * @return float |
||
| 84 | */ |
||
| 85 | 2 | public function getY() |
|
| 89 | |||
| 90 | /** |
||
| 91 | * @param float $y |
||
| 92 | */ |
||
| 93 | 30 | public function setY($y) |
|
| 97 | |||
| 98 | /** |
||
| 99 | * @return float |
||
| 100 | */ |
||
| 101 | 1 | public function getH() |
|
| 105 | |||
| 106 | /** |
||
| 107 | * @param float $h |
||
| 108 | */ |
||
| 109 | 30 | public function setH($h) |
|
| 113 | |||
| 114 | /** |
||
| 115 | * @return RefEll |
||
| 116 | */ |
||
| 117 | 1 | public function getRefEll() |
|
| 121 | |||
| 122 | /** |
||
| 123 | * @param RefEll $refEll |
||
| 124 | */ |
||
| 125 | 30 | public function setRefEll(RefEll $refEll) |
|
| 129 | |||
| 130 | |||
| 131 | /** |
||
| 132 | * Reference ellipsoid used by this projection |
||
| 133 | * @return RefEll |
||
| 134 | */ |
||
| 135 | abstract public function getReferenceEllipsoid(); |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Scale factor at central meridian |
||
| 139 | * @return float |
||
| 140 | */ |
||
| 141 | abstract public function getScaleFactor(); |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Northing of true origin |
||
| 145 | * @return float |
||
| 146 | */ |
||
| 147 | abstract public function getOriginNorthing(); |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Easting of true origin |
||
| 151 | * @return float |
||
| 152 | */ |
||
| 153 | abstract public function getOriginEasting(); |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Latitude of true origin |
||
| 157 | * @return float |
||
| 158 | */ |
||
| 159 | abstract public function getOriginLatitude(); |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Longitude of true origin |
||
| 163 | * @return float |
||
| 164 | */ |
||
| 165 | abstract public function getOriginLongitude(); |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Convert this grid reference into a latitude and longitude |
||
| 169 | * Formula for transformation is taken from OS document |
||
| 170 | * "A Guide to Coordinate Systems in Great Britain" |
||
| 171 | * |
||
| 172 | * @param float $N map coordinate (northing) of point to convert |
||
| 173 | * @param float $E map coordinate (easting) of point to convert |
||
| 174 | * @param float $N0 map coordinate (northing) of true origin |
||
| 175 | * @param float $E0 map coordinate (easting) of true origin |
||
| 176 | * @param float $phi0 map coordinate (latitude) of true origin |
||
| 177 | * @param float $lambda0 map coordinate (longitude) of true origin and central meridian |
||
| 178 | * @return LatLng |
||
| 179 | */ |
||
| 180 | 7 | public function convertToLatitudeLongitude($N, $E, $N0, $E0, $phi0, $lambda0) |
|
| 260 | |||
| 261 | /** |
||
| 262 | * Calculate the surface distance between this object and the one |
||
| 263 | * passed in as a parameter. |
||
| 264 | * |
||
| 265 | * @param self $to object to measure the distance to |
||
|
|
|||
| 266 | * @return float |
||
| 267 | */ |
||
| 268 | 1 | View Code Duplication | public function distance(self $to) { |
| 281 | } |
||
| 282 |
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.