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 | class LogsTable extends \WP_List_Table |
||
17 | { |
||
18 | /** |
||
19 | * The storage. |
||
20 | * |
||
21 | * @var Storage |
||
22 | */ |
||
23 | private $storage; |
||
24 | |||
25 | /** |
||
26 | * The form type name. |
||
27 | * |
||
28 | * @var string |
||
29 | */ |
||
30 | private $formType; |
||
31 | |||
32 | /** |
||
33 | * Constructor. |
||
34 | * |
||
35 | * @param string $formType The form type name |
||
36 | * @param Storage $storage The storage |
||
37 | */ |
||
38 | public function __construct($formType, Storage $storage) |
||
50 | |||
51 | /** |
||
52 | * {@inheritdoc} |
||
53 | */ |
||
54 | public function no_items() |
||
58 | |||
59 | /** |
||
60 | * {@inheritdoc} |
||
61 | */ |
||
62 | public function column_default($item, $column_name) |
||
66 | |||
67 | /** |
||
68 | * {@inheritdoc} |
||
69 | */ |
||
70 | public function get_columns() |
||
79 | |||
80 | /** |
||
81 | * {@inheritdoc} |
||
82 | */ |
||
83 | public function get_sortable_columns() |
||
90 | |||
91 | /** |
||
92 | * {@inheritdoc} |
||
93 | */ |
||
94 | View Code Duplication | public function prepare_items() |
|
108 | |||
109 | /** |
||
110 | * Callback that customize the header of the table. |
||
111 | */ |
||
112 | public function header() |
||
134 | } |
||
135 |
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.