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 AboutTree extends XoopsObjectTree |
||
12 | { |
||
13 | /** |
||
14 | * AboutTree constructor. |
||
15 | * @param array $objectArr |
||
16 | * @param null $rootId |
||
17 | */ |
||
18 | public function __construct(&$objectArr, $rootId = null) |
||
22 | |||
23 | /** |
||
24 | * @param $key |
||
25 | * @param $ret |
||
26 | * @param $prefix_orig |
||
27 | * @param string $prefix_curr |
||
28 | * @param null $tags |
||
29 | */ |
||
30 | public function makeTreeItems($key, &$ret, $prefix_orig, $prefix_curr = '', $tags = null) |
||
49 | |||
50 | /** |
||
51 | * @param string $prefix |
||
52 | * @param int $key |
||
53 | * @param null $tags |
||
54 | * @return array |
||
55 | */ |
||
56 | public function &makeTree($prefix = '-', $key = 0, $tags = null) |
||
63 | |||
64 | /** |
||
65 | * @param string $name |
||
66 | * @param string $fieldName |
||
67 | * @param string $prefix |
||
68 | * @param string $selected |
||
69 | * @param bool $addEmptyOption |
||
70 | * @param int $key |
||
71 | * @param string $extra |
||
72 | * @return string |
||
73 | */ |
||
74 | public function makeSelBox($name, $fieldName, $prefix = '-', $selected = '', $addEmptyOption = false, $key = 0, $extra = '') |
||
85 | |||
86 | /** |
||
87 | * @param $key |
||
88 | * @param $ret |
||
89 | * @param array $tags |
||
90 | * @param int $depth |
||
91 | */ |
||
92 | public function getAllChildArray($key, &$ret, $tags = array(), $depth = 0) |
||
114 | |||
115 | /** |
||
116 | * @param int $key |
||
117 | * @param null $tags |
||
118 | * @param int $depth |
||
119 | * @return array |
||
120 | */ |
||
121 | public function &makeArrayTree($key = 0, $tags = null, $depth = 0) |
||
131 | } |
||
132 | } |
||
133 |
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.