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 |
||
18 | trait HasLayouts |
||
19 | { |
||
20 | /** |
||
21 | * Debug a layout rendering. |
||
22 | * |
||
23 | * @param string $layoutId Layout identifier |
||
24 | * @param array $data Optional data for the layout |
||
25 | * |
||
26 | * @return string |
||
27 | */ |
||
28 | 1 | View Code Duplication | public function debug($layoutId = null, $data = array()) |
37 | |||
38 | /** |
||
39 | * Get the data that will be sent to the layout. |
||
40 | * |
||
41 | * @param boolean $reload Force reloading data |
||
42 | * |
||
43 | * @return array |
||
44 | */ |
||
45 | abstract protected function getLayoutData($reload = false); |
||
46 | |||
47 | /** |
||
48 | * Get the paths where we will search for layouts. |
||
49 | * |
||
50 | * @return string[] |
||
51 | */ |
||
52 | abstract protected function getLayoutPaths(); |
||
53 | |||
54 | /** |
||
55 | * Render a layout. |
||
56 | * |
||
57 | * @param string $layoutId Layout identifier |
||
58 | * @param array $data Optional data for the layout |
||
59 | * |
||
60 | * @return string |
||
61 | */ |
||
62 | 1 | View Code Duplication | public function render($layoutId = null, $data = array()) |
71 | } |
||
72 |
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.