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 |
||
29 | class Geometry |
||
30 | { |
||
31 | /** |
||
32 | * The Laravel application. |
||
33 | * |
||
34 | * @var Application|null |
||
35 | */ |
||
36 | protected $app; |
||
37 | |||
38 | /** |
||
39 | * Instance of geoPHP. |
||
40 | * |
||
41 | * @var geoPHP |
||
42 | */ |
||
43 | protected $geoPhp; |
||
44 | |||
45 | /** |
||
46 | * Instance of TypeMapper. |
||
47 | * |
||
48 | * @var TypeMapper |
||
49 | */ |
||
50 | protected $mapper; |
||
51 | |||
52 | /** |
||
53 | * Geometry constructor. |
||
54 | * |
||
55 | * @param geoPHP $geoPhp |
||
56 | * @param TypeMapper $mapper |
||
57 | * @param Application|null $app |
||
58 | */ |
||
59 | 14 | public function __construct(geoPHP $geoPhp, TypeMapper $mapper, $app = null) |
|
65 | |||
66 | /** |
||
67 | * Magic method to allow methods that are not specifically defined. |
||
68 | * |
||
69 | * Allow parseStudlyCaseOfType i.e. parseWkt or parseGeoJson to be called & mapped to the load method. |
||
70 | * |
||
71 | * @param string $name Name of the undefined method |
||
72 | * @param array $arguments |
||
73 | * |
||
74 | * @return bool|\GeometryCollection|mixed |
||
75 | * @throws RuntimeException |
||
76 | */ |
||
77 | 10 | public function __call($name, $arguments) |
|
86 | |||
87 | /** |
||
88 | * Build the name to the proxy geometry class. |
||
89 | * |
||
90 | * @param $geometry |
||
91 | * |
||
92 | * @return string |
||
93 | * @throws InvalidArgumentException|RuntimeException |
||
94 | */ |
||
95 | 11 | public function buildGeometryClassName($geometry) |
|
109 | |||
110 | /** |
||
111 | * Call geoPHP to load the data. |
||
112 | * |
||
113 | * @param string|object $data |
||
114 | * @param string|null $type |
||
115 | * |
||
116 | * @return bool|\GeometryCollection|mixed |
||
117 | * @throws Exception |
||
118 | */ |
||
119 | 10 | protected function loadGeometry($data, $type) |
|
127 | |||
128 | /** |
||
129 | * Pass the data to geoPHP to convert to the correct geometry type. |
||
130 | * |
||
131 | * @param string $data |
||
132 | * @param string $type |
||
133 | * |
||
134 | * @return bool|\GeometryCollection|mixed |
||
135 | * @throws Exception|InvalidArgumentException |
||
136 | */ |
||
137 | 10 | public function parse($data, $type = null) |
|
154 | } |
||
155 |
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.