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 |
||
11 | class TruliaWidgets { |
||
12 | |||
13 | /** |
||
14 | * __construct function. |
||
15 | * |
||
16 | * @access public |
||
17 | * @return void |
||
18 | */ |
||
19 | public function __construct() { |
||
21 | |||
22 | |||
23 | /** |
||
24 | * trulia_iframe_css function. |
||
25 | * |
||
26 | * @access public |
||
27 | * @return void |
||
28 | */ |
||
29 | public function trulia_iframe_css() { |
||
43 | |||
44 | /** |
||
45 | * Trulia Iframe ID Names. |
||
46 | * |
||
47 | * @access public |
||
48 | * @param string $iframe_id (default: '') |
||
49 | * @return void |
||
50 | */ |
||
51 | public function trulia_iframe_id( $iframe_id = '' ) { |
||
58 | |||
59 | /** |
||
60 | * Trulia Iframe Class Names. |
||
61 | * |
||
62 | * @access public |
||
63 | * @param string $widget_name (default: '') |
||
64 | * @return void |
||
65 | */ |
||
66 | View Code Duplication | public function trulia_iframe_class( $widget_name = '' ) { |
|
75 | |||
76 | /** |
||
77 | * get_community_advice_widget function. |
||
78 | * |
||
79 | * @access public |
||
80 | * @param string $iframe_id (default: '') |
||
81 | * @param mixed $display_answers |
||
82 | * @param mixed $title |
||
83 | * @param string $email (default: '') |
||
84 | * @return void |
||
85 | */ |
||
86 | public function get_community_advice_widget( $iframe_id = '', $display_answers, $title, $email = '' ) { |
||
89 | |||
90 | /** |
||
91 | * get_rent_buy_calc_widget function. |
||
92 | * |
||
93 | * @access public |
||
94 | * @param string $iframe_id (default: '') |
||
95 | * @param mixed $location |
||
96 | * @param mixed $width |
||
97 | * @param mixed $height |
||
98 | * @param string $email (default: '') |
||
99 | * @return void |
||
100 | */ |
||
101 | public function get_rent_buy_calc_widget( $iframe_id = '', $location, $width, $height, $email = '' ) { |
||
104 | |||
105 | /** |
||
106 | * get_rental_market_widget function. |
||
107 | * |
||
108 | * @access public |
||
109 | * @param string $iframe_id (default: '') |
||
110 | * @param mixed $location |
||
111 | * @param mixed $width |
||
112 | * @param mixed $height |
||
113 | * @param string $email (default: '') |
||
114 | * @return void |
||
115 | */ |
||
116 | public function get_rental_market_widget( $iframe_id = '', $location, $width, $height, $email = '' ) { |
||
121 | |||
122 | |||
123 | /** |
||
124 | * get_map_search_widget function. |
||
125 | * |
||
126 | * @access public |
||
127 | * @param string $iframe_id (default: '') |
||
128 | * @param mixed $location |
||
129 | * @param mixed $width |
||
130 | * @param mixed $height |
||
131 | * @param string $email (default: '') |
||
132 | * @return void |
||
133 | */ |
||
134 | public function get_map_search_widget( $iframe_id = '', $location, $width, $height, $email = '' ) { |
||
137 | |||
138 | /** |
||
139 | * get_home_gallery_widget function. |
||
140 | * |
||
141 | * @access public |
||
142 | * @param string $iframe_id (default: '') |
||
143 | * @param mixed $location |
||
144 | * @param mixed $width |
||
145 | * @param mixed $height |
||
146 | * @param string $email (default: '') |
||
147 | * @return void |
||
148 | */ |
||
149 | public function get_home_gallery_widget( $iframe_id = '', $location, $width, $height, $email = '' ) { |
||
152 | |||
153 | /** |
||
154 | * get_home_showcase_widget function. |
||
155 | * |
||
156 | * @access public |
||
157 | * @param string $iframe_id (default: '') |
||
158 | * @param mixed $location |
||
159 | * @param mixed $width |
||
160 | * @param mixed $height |
||
161 | * @param string $email (default: '') |
||
162 | * @return void |
||
163 | */ |
||
164 | public function get_home_showcase_widget( $iframe_id = '', $location, $width, $height, $email = '' ) { |
||
167 | |||
168 | /** |
||
169 | * get_homefinder_widget function. |
||
170 | * |
||
171 | * @access public |
||
172 | * @param string $iframe_id (default: '') |
||
173 | * @param mixed $location |
||
174 | * @param mixed $width |
||
175 | * @param mixed $height |
||
176 | * @param string $email (default: '') |
||
177 | * @return void |
||
178 | */ |
||
179 | public function get_homefinder_widget( $iframe_id = '', $location, $width, $height, $email = '' ) { |
||
182 | |||
183 | /** |
||
184 | * get_market_monitor_widget function. |
||
185 | * |
||
186 | * @access public |
||
187 | * @param string $iframe_id (default: '') |
||
188 | * @param mixed $location |
||
189 | * @param mixed $width |
||
190 | * @param mixed $height |
||
191 | * @param string $email (default: '') |
||
192 | * @return void |
||
193 | */ |
||
194 | public function get_market_monitor_widget( $iframe_id = '', $location, $width, $height, $email = '' ) { |
||
198 | |||
199 | /** |
||
200 | * get_affordability_calc_widget function. (https://www.trulia.com/tools/affordability-calculator/) |
||
201 | * |
||
202 | * @access public |
||
203 | * @param mixed $color |
||
204 | * @param mixed $title |
||
205 | * @param mixed $email |
||
206 | * @param mixed $width |
||
207 | * @param mixed $height |
||
208 | * @return void |
||
209 | */ |
||
210 | public function get_affordability_calc_widget( $iframe_id = '', $color, $title, $width, $height, $email = '' ) { |
||
215 | |||
216 | } |
||
217 | } |
||
218 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.