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 |
||
16 | View Code Duplication | class HomeFinderOpenHouse extends WP_Widget { |
|
17 | |||
18 | /** |
||
19 | * __construct function. |
||
20 | * |
||
21 | * @access public |
||
22 | * @return void |
||
23 | */ |
||
24 | public function __construct() { |
||
36 | |||
37 | /** |
||
38 | * Widget function. |
||
39 | * |
||
40 | * @access public |
||
41 | * @param mixed $args Arguments. |
||
42 | * @param mixed $instance Instance. |
||
43 | */ |
||
44 | public function widget( $args, $instance ) { |
||
59 | |||
60 | /** |
||
61 | * Form function. |
||
62 | * |
||
63 | * @access public |
||
64 | * @param mixed $instance Instance. |
||
65 | * @return void |
||
66 | */ |
||
67 | public function form( $instance ) { |
||
84 | |||
85 | /** |
||
86 | * Update Widget Instance. |
||
87 | * |
||
88 | * @access public |
||
89 | * @param mixed $new_instance New Instance. |
||
90 | * @param mixed $old_instance Old Instance. |
||
91 | * @return $instance |
||
92 | */ |
||
93 | public function update( $new_instance, $old_instance ) { |
||
101 | } |
||
102 | |||
115 |
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.