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 |
||
28 | class RelationNode extends BaseNode |
||
29 | { |
||
30 | |||
31 | /** |
||
32 | * @var string|int |
||
33 | */ |
||
34 | protected $id; |
||
35 | |||
36 | /** |
||
37 | * @var EEM_Base |
||
38 | */ |
||
39 | protected $main_model; |
||
40 | |||
41 | /** |
||
42 | * @var int |
||
43 | */ |
||
44 | protected $count; |
||
45 | |||
46 | /** |
||
47 | * @var EEM_Base |
||
48 | */ |
||
49 | protected $related_model; |
||
50 | |||
51 | /** |
||
52 | * @var ModelObjNode[] |
||
53 | */ |
||
54 | protected $nodes; |
||
55 | |||
56 | public function __construct($main_model_obj_id, EEM_Base $main_model, EEM_Base $related_model) |
||
63 | |||
64 | |||
65 | /** |
||
66 | * Here is where most of the work happens. We've counted how many related model objects exist, here we identify |
||
67 | * them (ie, learn their IDs). But its recursive, so we'll also find their related dependent model objects etc. |
||
68 | * @since $VID:$ |
||
69 | * @param int $model_objects_to_identify |
||
70 | * @return int |
||
71 | * @throws EE_Error |
||
72 | * @throws InvalidArgumentException |
||
73 | * @throws InvalidDataTypeException |
||
74 | * @throws InvalidInterfaceException |
||
75 | * @throws ReflectionException |
||
76 | */ |
||
77 | protected function work($model_objects_to_identify) |
||
113 | |||
114 | /** |
||
115 | * Checks if all the identified child nodes are complete or not. |
||
116 | * @since $VID:$ |
||
117 | * @return bool |
||
118 | */ |
||
119 | protected function allChildrenComplete() |
||
128 | |||
129 | /** |
||
130 | * Visits the provided nodes and keeps track of how much work was done, making sure to not go over budget. |
||
131 | * @since $VID:$ |
||
132 | * @param ModelObjNode[] $model_obj_nodes |
||
133 | * @param $work_budget |
||
134 | * @return int |
||
135 | */ |
||
136 | protected function visitAlreadyDiscoveredNodes($model_obj_nodes, $work_budget) |
||
150 | |||
151 | /** |
||
152 | * Whether this item has already been initialized |
||
153 | */ |
||
154 | protected function isDiscovered() |
||
158 | |||
159 | /** |
||
160 | * @since $VID:$ |
||
161 | * @return boolean |
||
162 | */ |
||
163 | public function isComplete() |
||
174 | |||
175 | /** |
||
176 | * Discovers how many related model objects exist. |
||
177 | * @since $VID:$ |
||
178 | * @return mixed|void |
||
179 | * @throws EE_Error |
||
180 | * @throws InvalidArgumentException |
||
181 | * @throws InvalidDataTypeException |
||
182 | * @throws InvalidInterfaceException |
||
183 | * @throws ReflectionException |
||
184 | */ |
||
185 | protected function discover() |
||
189 | |||
190 | /** |
||
191 | * @since $VID:$ |
||
192 | * @return array |
||
193 | * @throws EE_Error |
||
194 | * @throws InvalidDataTypeException |
||
195 | * @throws InvalidInterfaceException |
||
196 | * @throws InvalidArgumentException |
||
197 | * @throws ReflectionException |
||
198 | */ |
||
199 | protected function whereQueryParams() |
||
218 | /** |
||
219 | * @since $VID:$ |
||
220 | * @return array |
||
221 | */ |
||
222 | public function toArray() |
||
234 | |||
235 | /** |
||
236 | * Gets the IDs of all the model objects to delete; indexed first by model object name. |
||
237 | * @since $VID:$ |
||
238 | * @return array |
||
239 | */ |
||
240 | public function getIds() |
||
256 | |||
257 | /** |
||
258 | * Returns the number of sub-nodes found (ie, related model objects across this relation.) |
||
259 | * @since $VID:$ |
||
260 | * @return int |
||
261 | */ |
||
262 | public function countSubNodes() |
||
266 | |||
267 | /** |
||
268 | * Don't serialize the models. Just record their names on some dynamic properties. |
||
269 | * @since $VID:$ |
||
270 | */ |
||
271 | public function __sleep() |
||
286 | |||
287 | /** |
||
288 | * Use the dynamic properties to instantiate the models we use. |
||
289 | * @since $VID:$ |
||
290 | * @throws EE_Error |
||
291 | * @throws InvalidArgumentException |
||
292 | * @throws InvalidDataTypeException |
||
293 | * @throws InvalidInterfaceException |
||
294 | * @throws ReflectionException |
||
295 | */ |
||
296 | public function __wakeup() |
||
302 | } |
||
303 | // End of file RelationNode.php |
||
305 |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: