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 | class HomesWidgets { |
||
16 | |||
17 | /** |
||
18 | * __construct function. |
||
19 | * |
||
20 | * @access public |
||
21 | * @return void |
||
22 | */ |
||
23 | public function __construct() { |
||
25 | |||
26 | /** |
||
27 | * Homes_iframe_css function. |
||
28 | * |
||
29 | * @access public |
||
30 | * @return void |
||
31 | */ |
||
32 | public function homes_iframe_css() { |
||
45 | |||
46 | /** |
||
47 | * Homes iframe ID Names. |
||
48 | * |
||
49 | * @access public |
||
50 | * @param string $iframe_id (default: ''). |
||
51 | * @return string $iframe_id. |
||
52 | */ |
||
53 | public function homes_iframe_id( $iframe_id = '' ) { |
||
60 | |||
61 | /** |
||
62 | * Homes iframe Class Names. |
||
63 | * |
||
64 | * @access public |
||
65 | * @param string $widget_name (default: ''). |
||
66 | * @return string class name. |
||
67 | */ |
||
68 | View Code Duplication | public function homes_iframe_class( $widget_name = '' ) { |
|
77 | |||
78 | /* Homes WIDGETS. */ |
||
79 | |||
80 | /** |
||
81 | * Get Commute Time Widget. |
||
82 | * |
||
83 | * @access public |
||
84 | * @param string $iframe_id (default: ''). |
||
85 | * @param mixed $start_addr Start Address. |
||
86 | * @param mixed $color Button Color. |
||
87 | * @return void |
||
88 | */ |
||
89 | public function get_commute_time_widget( $iframe_id = '', $start_addr, $color ) { |
||
102 | |||
103 | /** |
||
104 | * Get Featured Listings Widget. |
||
105 | * |
||
106 | * @access public |
||
107 | * @param string $iframe_id (default: ''). |
||
108 | * @param mixed $location Location. |
||
109 | * @param mixed $color Text Color. |
||
110 | * @param mixed $status Listing Status. |
||
111 | * @return void |
||
112 | */ |
||
113 | public function get_featured_listings( $iframe_id = '', $location, $color, $status ) { |
||
130 | |||
131 | /** |
||
132 | * Get Home Values Widget. |
||
133 | * |
||
134 | * @access public |
||
135 | * @param string $iframe_id (default: ''). |
||
136 | * @param mixed $location Location. |
||
137 | * @param mixed $firstColor Color 1. |
||
138 | * @param mixed $secondColor Color 2. |
||
139 | * @param mixed $average Average Value. |
||
140 | * @param mixed $median Median Value. |
||
141 | * @return void |
||
142 | */ |
||
143 | public function get_home_values( $iframe_id = '', $location, $firstColor, $secondColor, $average, $median ) { |
||
165 | |||
166 | /** |
||
167 | * Get Search Widget. |
||
168 | * |
||
169 | * @access public |
||
170 | * @param string $iframe_id (default: ''). |
||
171 | * @param mixed $location Location. |
||
172 | * @param mixed $color Color. |
||
173 | * @param bool $sale (default: true) For Sale homes. |
||
174 | * @param bool $rent (default: true) For Rent homes. |
||
175 | * @return void |
||
176 | */ |
||
177 | View Code Duplication | public function get_search( $iframe_id = '', $location, $color, $sale, $rent ) { |
|
199 | |||
200 | /** |
||
201 | * Get Mortgage Calculator Widget. |
||
202 | * |
||
203 | * @access public |
||
204 | * @param string $iframe_id (default: ''). |
||
205 | * @param mixed $color Color. |
||
206 | * @return void |
||
207 | */ |
||
208 | public function get_mortgage_calc( $iframe_id = '', $color ) { |
||
219 | |||
220 | /** |
||
221 | * Get Real Estate (Simple) Search Widget. |
||
222 | * |
||
223 | * @access public |
||
224 | * @param string $iframe_id (default: ''). |
||
225 | * @param mixed $location Location. |
||
226 | * @param mixed $color Color. |
||
227 | * @param bool $sale (default: true) For Sale homes. |
||
228 | * @param bool $rent (default: true) For Rent homes. |
||
229 | * @return void |
||
230 | */ |
||
231 | View Code Duplication | public function get_simple_search( $iframe_id = '', $location, $color, $sale, $rent ) { |
|
252 | |||
253 | /** |
||
254 | * Get Real Estate (Tall) Search Widget. |
||
255 | * |
||
256 | * @access public |
||
257 | * @param string $iframe_id (default: ''). |
||
258 | * @param mixed $location Location. |
||
259 | * @param mixed $color Text Color. |
||
260 | * @param mixed $status Listing Status. |
||
261 | * @return void |
||
262 | */ |
||
263 | public function get_tall_search( $iframe_id = '', $location, $color, $status ) { |
||
273 | } |
||
274 | } |
||
275 |
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.