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 |
||
33 | class AlumniObjectTree extends XoopsObjectTree |
||
34 | { |
||
35 | /** |
||
36 | * @var string |
||
37 | */ |
||
38 | private $_parentId; |
||
39 | |||
40 | /** |
||
41 | * @var string |
||
42 | */ |
||
43 | private $_myId; |
||
44 | |||
45 | /** |
||
46 | * @var null|string |
||
47 | */ |
||
48 | private $_rootId = null; |
||
49 | |||
50 | /** |
||
51 | * @var array |
||
52 | */ |
||
53 | private $_tree = []; |
||
54 | |||
55 | /** |
||
56 | * @var array |
||
57 | */ |
||
58 | private $_objects; |
||
59 | |||
60 | /** |
||
61 | * Constructor |
||
62 | * |
||
63 | * @param array $objectArr Array of {@link XoopsObject}s |
||
64 | * @param string $myId field name of object ID |
||
65 | * @param string $parentId field name of parent object ID |
||
66 | * @param string $rootId field name of root object ID |
||
67 | */ |
||
68 | public function __construct(&$objectArr, $myId, $parentId, $rootId = null) |
||
69 | { |
||
70 | $this->_objects = $objectArr; |
||
71 | $this->_myId = $myId; |
||
72 | $this->_parentId = $parentId; |
||
73 | if (null !== $rootId) { |
||
74 | $this->_rootId = $rootId; |
||
75 | } |
||
76 | $this->_initialize(); |
||
77 | } |
||
78 | |||
79 | /** |
||
80 | * Initialize the object |
||
81 | * |
||
82 | * @access private |
||
83 | */ |
||
84 | private function _initialize() |
||
85 | { |
||
86 | /* @var $object XoopsObject */ |
||
87 | foreach ($this->_objects as $object) { |
||
88 | $key1 = $object->getVar($this->_myId); |
||
89 | $this->_tree[$key1]['obj'] = $object; |
||
90 | $key2 = $object->getVar($this->_parentId); |
||
91 | $this->_tree[$key1]['parent'] = $key2; |
||
92 | $this->_tree[$key2]['child'][] = $key1; |
||
93 | if (null !== $this->_rootId) { |
||
94 | $this->_tree[$key1]['root'] = $object->getVar($this->_rootId); |
||
95 | } |
||
96 | } |
||
97 | } |
||
98 | |||
99 | /** |
||
100 | * Get the tree |
||
101 | * |
||
102 | * @return array Associative array comprising the tree |
||
103 | */ |
||
104 | public function alumni_getTree() |
||
108 | |||
109 | /** |
||
110 | * returns an object from the tree specified by its id |
||
111 | * |
||
112 | * @param string $key ID of the object to retrieve |
||
113 | * @return XoopsObject Object within the tree |
||
114 | */ |
||
115 | public function alumni_getByKey($key) |
||
119 | |||
120 | /** |
||
121 | * returns an array of all the first child object of an object specified by its id |
||
122 | * |
||
123 | * @param string $key ID of the parent object |
||
124 | * @return array Array of children of the parent |
||
125 | */ |
||
126 | public function alumni_getFirstChild($key) |
||
137 | |||
138 | /** |
||
139 | * returns an array of all child objects of an object specified by its id |
||
140 | * |
||
141 | * @param string $key ID of the parent |
||
142 | * @param array $ret (Empty when called from client) Array of children from previous recursions. |
||
143 | * @return array Array of child nodes. |
||
144 | */ |
||
145 | public function alumni_getAllChild($key, $ret = []) |
||
159 | |||
160 | /** |
||
161 | * returns an array of all parent objects. |
||
162 | * the key of returned array represents how many levels up from the specified object |
||
163 | * |
||
164 | * @param string $key ID of the child object |
||
165 | * @param array $ret (empty when called from outside) Result from previous recursions |
||
166 | * @param int $uplevel (empty when called from outside) level of recursion |
||
167 | * @return array Array of parent nodes. |
||
168 | */ |
||
169 | public function alumni_getAllParent($key, $ret = [], $uplevel = 1) |
||
181 | |||
182 | /** |
||
183 | * Make options for a select box from |
||
184 | * |
||
185 | * @param string $fieldName Name of the member variable from the |
||
186 | * node objects that should be used as the title for the options. |
||
187 | * @param string $selected Value to display as selected |
||
188 | * @param int $key ID of the object to display as the root of select options |
||
189 | * @param string $ret (reference to a string when called from outside) Result from previous recursions |
||
190 | * @param string $prefix_orig String to indent items at deeper levels |
||
191 | * @param string $prefix_curr String to indent the current item |
||
192 | * |
||
193 | * @return void |
||
194 | */ |
||
195 | private function alumni_makeSelBoxOptions($fieldName, $selected, $key, &$ret, $prefix_orig, $prefix_curr = '') |
||
214 | |||
215 | /** |
||
216 | * Make a select box with options from the tree |
||
217 | * |
||
218 | * @param $name |
||
219 | * @param string $fieldName Name of the member variable from the |
||
220 | * node objects that should be used as the title for the options. |
||
221 | * @param string $prefix String to indent deeper levels |
||
222 | * @param string $selected Value to display as selected |
||
223 | * @param bool $addEmptyOption Set TRUE to add an empty option with value "0" at the top of the hierarchy |
||
224 | * @param integer $key ID of the object to display as the root of select options |
||
225 | * @param string $extra |
||
226 | * @return string HTML select box |
||
227 | */ |
||
228 | public function alumni_makeSelBox($name, $fieldName, $prefix = '-', $selected = '', $addEmptyOption = false, $key = 0, $extra = '') |
||
239 | |||
240 | /** |
||
241 | * Make options for a array |
||
242 | * |
||
243 | * @param string $fieldName Name of the member variable from the |
||
244 | * node objects that should be used as the column. |
||
245 | * @param string $prefix String to indent deeper levels |
||
246 | * @param integer $key ID of the object to display as the root of the array |
||
247 | * @return array |
||
248 | */ |
||
249 | public function alumni_makeArrayTree($fieldName, $prefix = '-', $key = 0) |
||
256 | |||
257 | /** |
||
258 | * Make a array with options from the tree |
||
259 | * |
||
260 | * @param string $fieldName Name of the member variable from the |
||
261 | * node objects that should be used as the column. |
||
262 | * @param integer $key ID of the object to display as the root of the array |
||
263 | * @param $ret |
||
264 | * @param string $prefix_orig String to indent deeper levels (origin) |
||
265 | * @param string $prefix_curr String to indent deeper levels (current) |
||
266 | * |
||
267 | * @return void |
||
268 | */ |
||
269 | public function alumni_makeArrayTreeOptions($fieldName, $key, &$ret, $prefix_orig, $prefix_curr = '') |
||
282 | } |
||
283 |
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.