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 HomeFinderWidgets { |
||
16 | |||
17 | /** |
||
18 | * Widget data to be sent to JS. |
||
19 | * |
||
20 | * @var [Array] |
||
21 | */ |
||
22 | static private $hf_data; |
||
23 | |||
24 | /** |
||
25 | * __construct function. |
||
26 | * |
||
27 | * @access public |
||
28 | * @return void |
||
29 | */ |
||
30 | public function __construct() { |
||
34 | |||
35 | /** |
||
36 | * Enqueue JS on footer and handle multiple widgets. |
||
37 | */ |
||
38 | public function hf_enqueue() { |
||
44 | |||
45 | /** |
||
46 | * HomeFinder ID Names. |
||
47 | * |
||
48 | * @access public |
||
49 | * @param string $widget_id (default: ''). |
||
50 | * @return string $widget_id. |
||
51 | */ |
||
52 | public function homefinder_id( $widget_id = '' ) { |
||
59 | |||
60 | /** |
||
61 | * HomeFinder div Class Names. |
||
62 | * |
||
63 | * @access public |
||
64 | * @param string $widget_name (default: ''). |
||
65 | * @return string class name. |
||
66 | */ |
||
67 | public function homefinder_class( $widget_name = '' ) { |
||
76 | |||
77 | /* HomeFinder WIDGETS. */ |
||
78 | |||
79 | /** |
||
80 | * Get Homes For Sale Widget. |
||
81 | * |
||
82 | * @access public |
||
83 | * @return void |
||
84 | */ |
||
85 | View Code Duplication | public function get_homes_for_sale_widget() { |
|
98 | |||
99 | /** |
||
100 | * Get Open House Widget. |
||
101 | * |
||
102 | * @access public |
||
103 | * @return void |
||
104 | */ |
||
105 | View Code Duplication | public function get_open_house_widget() { |
|
118 | |||
119 | /** |
||
120 | * Get Foreclosure Homes Widget. |
||
121 | * |
||
122 | * @access public |
||
123 | * @return void |
||
124 | */ |
||
125 | View Code Duplication | public function get_foreclosure_homes_widget() { |
|
138 | |||
139 | /** |
||
140 | * Get HomeFinder Widget. |
||
141 | * |
||
142 | * @access public |
||
143 | * @param string $type Widget Type. |
||
144 | * @param [Mixed] $widget_data : Array of widget data to send to js. |
||
145 | * @return void |
||
146 | */ |
||
147 | public function get_affiliates_widget( $type, $widget_data ) { |
||
161 | } |
||
162 | } |
||
163 |
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.