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 |
||
35 | class NewbbObjectTree extends XoopsObjectTree |
||
36 | { |
||
37 | /** |
||
38 | * @param $objectArr |
||
39 | * @param null $rootId |
||
40 | */ |
||
41 | public function __construct(&$objectArr, $rootId = null) |
||
45 | |||
46 | /** |
||
47 | * Make options for a select box from |
||
48 | * |
||
49 | * @param int $key ID of the object to display as the root of select options |
||
50 | * @param string $ret (reference to a string when called from outside) Result from previous recursions |
||
51 | * @param string $prefix_orig String to indent items at deeper levels |
||
52 | * @param string $prefix_curr String to indent the current item |
||
53 | * @param null|array $tags |
||
54 | * @internal param string $fieldName Name of the member variable from the |
||
55 | * node objects that should be used as the title for the options. |
||
56 | * @internal param string $selected Value to display as selected |
||
57 | * @access private |
||
58 | */ |
||
59 | protected function makeTreeItems($key, &$ret, $prefix_orig, $prefix_curr = '', $tags = null) |
||
60 | { |
||
61 | if ($key > 0) { |
||
62 | View Code Duplication | if (count($tags) > 0) { |
|
63 | foreach ($tags as $tag) { |
||
64 | $ret[$key][$tag] = $this->tree[$key]['obj']->getVar($tag); |
||
65 | } |
||
66 | } else { |
||
67 | $ret[$key]['forum_name'] = $this->tree[$key]['obj']->getVar('forum_name'); |
||
68 | } |
||
69 | $ret[$key]['prefix'] = $prefix_curr; |
||
70 | $prefix_curr .= $prefix_orig; |
||
71 | } |
||
72 | if (isset($this->tree[$key]['child']) && !empty($this->tree[$key]['child'])) { |
||
73 | foreach ($this->tree[$key]['child'] as $childkey) { |
||
74 | $this->makeTreeItems($childkey, $ret, $prefix_orig, $prefix_curr, $tags); |
||
75 | } |
||
76 | } |
||
77 | } |
||
78 | |||
79 | /** |
||
80 | * Make a select box with options from the tree |
||
81 | * |
||
82 | * @param string $prefix String to indent deeper levels |
||
83 | * @param integer $key ID of the object to display as the root of select options |
||
84 | * @param null $tags |
||
85 | * @return array|string HTML select box |
||
86 | * @internal param string $name Name of the select box |
||
87 | * @internal param string $fieldName Name of the member variable from the |
||
88 | * node objects that should be used as the title for the options. |
||
89 | * @internal param string $selected Value to display as selected |
||
90 | * @internal param bool $addEmptyOption Set TRUE to add an empty option with value "0" at the top of the hierarchy |
||
91 | */ |
||
92 | public function &makeTree($prefix = '-', $key = 0, $tags = null) |
||
99 | |||
100 | /** |
||
101 | * Make a select box with options from the tree |
||
102 | * |
||
103 | * @param string $name Name of the select box |
||
104 | * @param string $fieldName |
||
105 | * @param string $prefix String to indent deeper levels |
||
106 | * @param string $selected Value to display as selected |
||
107 | * @param bool $addEmptyOption |
||
108 | * @param integer $key ID of the object to display as the root of select options |
||
109 | * @param string $extra |
||
110 | * @return string HTML select box |
||
111 | * @internal param bool $EmptyOption |
||
112 | * @internal param string $fieldName Name of the member variable from the |
||
113 | * node objects that should be used as the title for the options. |
||
114 | * @internal param bool $addEmptyOption Set TRUE to add an empty option with value "0" at the top of the hierarchy |
||
115 | */ |
||
116 | public function makeSelBox( |
||
135 | |||
136 | /** |
||
137 | * Make a tree for the array of a given category |
||
138 | * |
||
139 | * @param string $key top key of the tree |
||
140 | * @param array $ret the tree |
||
141 | * @param integer $depth level of subcategories |
||
142 | * @return array |
||
143 | * @internal param array $tags fields to be used |
||
144 | */ |
||
145 | public function getAllChildObject($key, &$ret, $depth = 0) |
||
146 | { |
||
147 | if (--$depth == 0) { |
||
148 | return; |
||
149 | } |
||
150 | |||
151 | if (isset($this->tree[$key]['child'])) { |
||
152 | foreach ($this->tree[$key]['child'] as $childkey) { |
||
153 | if (isset($this->tree[$childkey]['obj'])) { |
||
154 | $ret['child'][$childkey] = $this->tree[$childkey]['obj']; |
||
155 | } |
||
156 | $this->getAllChild_object($childkey, $ret['child'][$childkey], $depth); |
||
157 | } |
||
158 | } |
||
159 | } |
||
160 | |||
161 | /** |
||
162 | * Make a tree for the array |
||
163 | * |
||
164 | * @param int|string $key top key of the tree |
||
165 | * @param integer $depth level of subcategories |
||
166 | * @return array |
||
167 | * @internal param array $tags fields to be used |
||
168 | */ |
||
169 | public function &makeObjectTree($key = 0, $depth = 0) |
||
179 | |||
180 | /** |
||
181 | * Make a tree for the array of a given category |
||
182 | * |
||
183 | * @param string $key top key of the tree |
||
184 | * @param array $ret the tree |
||
185 | * @param array $tags fields to be used |
||
186 | * @param integer $depth level of subcategories |
||
187 | * @return array |
||
188 | **/ |
||
189 | public function getAllChildArray($key, &$ret, array $tags = [], $depth = 0) |
||
190 | { |
||
191 | if (--$depth == 0) { |
||
192 | return; |
||
193 | } |
||
194 | |||
195 | if (isset($this->tree[$key]['child'])) { |
||
196 | foreach ($this->tree[$key]['child'] as $childkey) { |
||
197 | if (isset($this->tree[$childkey]['obj'])) { |
||
198 | View Code Duplication | if (count($tags) > 0) { |
|
199 | foreach ($tags as $tag) { |
||
200 | $ret['child'][$childkey][$tag] = $this->tree[$childkey]['obj']->getVar($tag); |
||
201 | } |
||
202 | } else { |
||
203 | $ret['child'][$childkey]['forum_name'] = $this->tree[$childkey]['obj']->getVar('forum_name'); |
||
204 | } |
||
205 | } |
||
206 | |||
207 | $this->getAllChildArray($childkey, $ret['child'][$childkey], $tags, $depth); |
||
208 | } |
||
209 | } |
||
210 | } |
||
211 | |||
212 | /** |
||
213 | * Make a tree for the array |
||
214 | * |
||
215 | * @param int|string $key top key of the tree |
||
216 | * @param array $tags fields to be used |
||
217 | * @param integer $depth level of subcategories |
||
218 | * @return array |
||
219 | */ |
||
220 | public function &makeArrayTree($key = 0, $tags = null, $depth = 0) |
||
230 | |||
231 | /**#@+ |
||
232 | * get all parent forums |
||
233 | * |
||
234 | * @param string $key ID of the child object |
||
235 | * @param array $ret (empty when called from outside) Result from previous recursions |
||
236 | * @param int $uplevel (empty when called from outside) level of recursion |
||
237 | * @return array Array of parent nodes. |
||
238 | */ |
||
239 | public function &myGetParentForums($key, array $ret = [], $uplevel = 0) |
||
240 | { |
||
241 | if (isset($this->tree[$key]['parent']) && isset($this->tree[$this->tree[$key]['parent']]['obj'])) { |
||
242 | $ret[$uplevel] = $this->tree[$this->tree[$key]['parent']]['obj']; |
||
243 | if ($this->tree[$key]['parent'] !== $key) { |
||
244 | //$parents = $this->getParentForums($this->tree[$key]['parent'], $ret, $uplevel+1); |
||
245 | $parents = $this->getParentForums($this->tree[$key]['parent']); |
||
246 | foreach (array_keys($parents) as $newkey) { |
||
247 | $ret[$newkey] = $parents[$newkey]; |
||
248 | } |
||
249 | } |
||
250 | } |
||
251 | |||
252 | return $ret; |
||
253 | } |
||
254 | |||
255 | /** |
||
256 | * @param $key |
||
257 | * @param bool $reverse |
||
258 | * @return array |
||
259 | */ |
||
260 | public function &getParentForums($key, $reverse = true) |
||
282 | /**#@-*/ |
||
283 | } |
||
284 | } |
||
285 |
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.