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 |
||
10 | View Code Duplication | class Map_Model extends Model_Base { |
|
|
|||
11 | |||
12 | /** |
||
13 | * @var Map |
||
14 | */ |
||
15 | protected $_map; |
||
16 | |||
17 | /** |
||
18 | * @param string $method_name |
||
19 | * @return mixed |
||
20 | */ |
||
21 | function __call( $method_name, $args ) { |
||
42 | |||
43 | // /** |
||
44 | // * @var array |
||
45 | // */ |
||
46 | // protected $_center; |
||
47 | // |
||
48 | // /** |
||
49 | // * The Map element height (default: 400px). |
||
50 | // * @var string |
||
51 | // */ |
||
52 | // protected $_height = '400px'; |
||
53 | // |
||
54 | // /** |
||
55 | // * The string to use for the map HTML element id property |
||
56 | // * |
||
57 | // * @var string |
||
58 | // */ |
||
59 | // protected $_html_id; |
||
60 | // |
||
61 | // /** |
||
62 | // * @var Marker[] |
||
63 | // */ |
||
64 | // protected $_markers = array(); |
||
65 | // |
||
66 | // /** |
||
67 | // * The Map element width (default: 100%). |
||
68 | // * @var string |
||
69 | // */ |
||
70 | // protected $_width = '100%'; |
||
71 | // |
||
72 | // /** |
||
73 | // * @var int |
||
74 | // */ |
||
75 | // protected $_zoom = 5; |
||
76 | // |
||
77 | // /** |
||
78 | // * @param Marker $marker |
||
79 | // */ |
||
80 | // function add_marker( $marker ) { |
||
81 | // |
||
82 | // $this->_markers[] = $marker; |
||
83 | // |
||
84 | // } |
||
85 | // |
||
86 | // /** |
||
87 | // * @param Marker[] $markers |
||
88 | // */ |
||
89 | // function add_markers( $markers ) { |
||
90 | // |
||
91 | // $this->_markers = array_merge( $this->_markers, $markers ); |
||
92 | // |
||
93 | // } |
||
94 | // |
||
95 | // /** |
||
96 | // * @return array |
||
97 | // */ |
||
98 | // function center() { |
||
99 | // |
||
100 | // return $this->_center; |
||
101 | // |
||
102 | // } |
||
103 | // |
||
104 | // /** |
||
105 | // * @return string |
||
106 | // */ |
||
107 | // function height() { |
||
108 | // |
||
109 | // return $this->_height; |
||
110 | // |
||
111 | // } |
||
112 | // |
||
113 | // /** |
||
114 | // * @return string |
||
115 | // */ |
||
116 | // function html_id() { |
||
117 | // |
||
118 | // if ( ! isset( $this->_html_id ) ) { |
||
119 | // $this->_html_id = sprintf( 'map-%1$s', md5( serialize( array( $this->center(), $this->markers() ) ) ) ); |
||
120 | // } |
||
121 | // |
||
122 | // return $this->_html_id; |
||
123 | // |
||
124 | // } |
||
125 | // |
||
126 | // /** |
||
127 | // * @return Marker[] |
||
128 | // */ |
||
129 | // function markers() { |
||
130 | // |
||
131 | // return $this->_markers; |
||
132 | // |
||
133 | // } |
||
134 | // |
||
135 | // /** |
||
136 | // * @return string |
||
137 | // */ |
||
138 | // function width() { |
||
139 | // |
||
140 | // return $this->_width; |
||
141 | // |
||
142 | // } |
||
143 | // |
||
144 | // /** |
||
145 | // * @return int |
||
146 | // */ |
||
147 | // function zoom() { |
||
148 | // |
||
149 | // return $this->_zoom; |
||
150 | // |
||
151 | // } |
||
152 | // |
||
153 | // /** |
||
154 | // * @return array |
||
155 | // */ |
||
156 | // function make_args() { |
||
157 | // |
||
158 | // return array( |
||
159 | // 'center' => $this->center(), |
||
160 | // 'zoom' => (int)$this->zoom() |
||
161 | // ); |
||
162 | // |
||
163 | // } |
||
164 | |||
165 | } |
||
166 |
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.