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 NarWidgets { |
||
12 | |||
13 | /** |
||
14 | * __construct function. |
||
15 | * |
||
16 | * @access public |
||
17 | * @return void |
||
18 | */ |
||
19 | public function __construct() { |
||
21 | |||
22 | |||
23 | /** |
||
24 | * Nar Iframe ID Names. |
||
25 | * |
||
26 | * @access public |
||
27 | * @param string $iframe_id (default: '') |
||
28 | * @return void |
||
29 | */ |
||
30 | public function nar_iframe_id( $iframe_id = '' ) { |
||
37 | |||
38 | /** |
||
39 | * Nar Iframe Class Names. |
||
40 | * |
||
41 | * @access public |
||
42 | * @param string $widget_name (default: '') |
||
43 | * @return void |
||
44 | */ |
||
45 | View Code Duplication | public function nar_iframe_class( $widget_name = '' ) { |
|
54 | |||
55 | /** |
||
56 | * get_mvp_program_widget function. |
||
57 | * |
||
58 | * @access public |
||
59 | * @return void |
||
60 | */ |
||
61 | public function get_mvp_program_widget() { |
||
65 | |||
66 | /** |
||
67 | * get_home_ownership_matters_widget function. |
||
68 | * |
||
69 | * @access public |
||
70 | * @return void |
||
71 | */ |
||
72 | public function get_home_ownership_matters_widget() { |
||
78 | |||
79 | /** |
||
80 | * get_code_of_ethics_widget function. |
||
81 | * |
||
82 | * @access public |
||
83 | * @return void |
||
84 | */ |
||
85 | public function get_code_of_ethics_widget() { |
||
88 | |||
89 | /** |
||
90 | * get_nar_conf_expo_widget function. |
||
91 | * |
||
92 | * @access public |
||
93 | * @return void |
||
94 | */ |
||
95 | public function get_nar_conf_expo_widget() { |
||
98 | |||
99 | } |
||
100 | |||
102 |
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.