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 |
||
9 | class Marker_Model extends \WPLib_Model_Base { |
||
10 | |||
11 | /** |
||
12 | * @var string |
||
13 | */ |
||
14 | protected $_address; |
||
15 | |||
16 | /** |
||
17 | * @var Geocoder |
||
18 | */ |
||
19 | protected $_geocoder; |
||
20 | |||
21 | /** |
||
22 | * @var Info_Window |
||
23 | */ |
||
24 | protected $_info_window; |
||
25 | |||
26 | /** |
||
27 | * @var Marker_Label |
||
28 | */ |
||
29 | protected $_label; |
||
30 | |||
31 | /** |
||
32 | * @var double |
||
33 | */ |
||
34 | protected $_latitude = null; |
||
35 | |||
36 | /** |
||
37 | * @var double |
||
38 | */ |
||
39 | protected $_longitude; |
||
40 | |||
41 | /** |
||
42 | * @var Location|\WP_Error |
||
43 | */ |
||
44 | protected $_location; |
||
45 | |||
46 | /** |
||
47 | * @var string |
||
48 | */ |
||
49 | protected $_title; |
||
50 | |||
51 | /** |
||
52 | * Marker_Model constructor. |
||
53 | * @param array $args |
||
54 | */ |
||
55 | 1 | function __construct( $args = array() ) { |
|
75 | |||
76 | /** |
||
77 | * @return Info_Window |
||
78 | */ |
||
79 | function info_window() { |
||
84 | |||
85 | /** |
||
86 | * @return Marker_Label |
||
87 | */ |
||
88 | 1 | function label() { |
|
97 | |||
98 | /** |
||
99 | * @return double |
||
100 | */ |
||
101 | 2 | View Code Duplication | function latitude() { |
108 | |||
109 | /** |
||
110 | * @return Location|\WP_Error |
||
111 | */ |
||
112 | 2 | function location() { |
|
118 | |||
119 | /** |
||
120 | * @return double |
||
121 | */ |
||
122 | 2 | View Code Duplication | function longitude() { |
129 | |||
130 | /** |
||
131 | * Get the position of this marker. An array with key/value pairs of lat and lng. |
||
132 | * |
||
133 | * @return array |
||
134 | */ |
||
135 | 2 | function position() { |
|
140 | |||
141 | /** |
||
142 | * @return string |
||
143 | */ |
||
144 | 2 | function title() { |
|
147 | |||
148 | /** |
||
149 | * @param array $args |
||
150 | * @return array |
||
151 | */ |
||
152 | 1 | function marker_args( $args = array() ) { |
|
165 | |||
166 | /** |
||
167 | * @return Geocoder |
||
168 | */ |
||
169 | 3 | private function _geocoder() { |
|
176 | |||
177 | } |
||
178 |
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.