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 | /** |
||
57 | * RelationNode constructor. |
||
58 | * @param $main_model_obj_id |
||
59 | * @param EEM_Base $main_model |
||
60 | * @param EEM_Base $related_model |
||
61 | * @param array $dont_traverse_models array of model names we DON'T want to traverse |
||
62 | */ |
||
63 | public function __construct( |
||
75 | |||
76 | |||
77 | /** |
||
78 | * Here is where most of the work happens. We've counted how many related model objects exist, here we identify |
||
79 | * them (ie, learn their IDs). But its recursive, so we'll also find their related dependent model objects etc. |
||
80 | * @since $VID:$ |
||
81 | * @param int $model_objects_to_identify |
||
82 | * @return int |
||
83 | * @throws EE_Error |
||
84 | * @throws InvalidArgumentException |
||
85 | * @throws InvalidDataTypeException |
||
86 | * @throws InvalidInterfaceException |
||
87 | * @throws ReflectionException |
||
88 | */ |
||
89 | protected function work($model_objects_to_identify) |
||
125 | |||
126 | /** |
||
127 | * Checks if all the identified child nodes are complete or not. |
||
128 | * @since $VID:$ |
||
129 | * @return bool |
||
130 | */ |
||
131 | protected function allChildrenComplete() |
||
140 | |||
141 | /** |
||
142 | * Visits the provided nodes and keeps track of how much work was done, making sure to not go over budget. |
||
143 | * @since $VID:$ |
||
144 | * @param ModelObjNode[] $model_obj_nodes |
||
145 | * @param $work_budget |
||
146 | * @return int |
||
147 | */ |
||
148 | protected function visitAlreadyDiscoveredNodes($model_obj_nodes, $work_budget) |
||
162 | |||
163 | /** |
||
164 | * Whether this item has already been initialized |
||
165 | */ |
||
166 | protected function isDiscovered() |
||
170 | |||
171 | /** |
||
172 | * @since $VID:$ |
||
173 | * @return boolean |
||
174 | */ |
||
175 | public function isComplete() |
||
186 | |||
187 | /** |
||
188 | * Discovers how many related model objects exist. |
||
189 | * @since $VID:$ |
||
190 | * @return mixed|void |
||
191 | * @throws EE_Error |
||
192 | * @throws InvalidArgumentException |
||
193 | * @throws InvalidDataTypeException |
||
194 | * @throws InvalidInterfaceException |
||
195 | * @throws ReflectionException |
||
196 | */ |
||
197 | protected function discover() |
||
201 | |||
202 | /** |
||
203 | * @since $VID:$ |
||
204 | * @return array |
||
205 | * @throws EE_Error |
||
206 | * @throws InvalidDataTypeException |
||
207 | * @throws InvalidInterfaceException |
||
208 | * @throws InvalidArgumentException |
||
209 | * @throws ReflectionException |
||
210 | */ |
||
211 | protected function whereQueryParams() |
||
230 | /** |
||
231 | * @since $VID:$ |
||
232 | * @return array |
||
233 | */ |
||
234 | public function toArray() |
||
246 | |||
247 | /** |
||
248 | * Gets the IDs of all the model objects to delete; indexed first by model object name. |
||
249 | * @since $VID:$ |
||
250 | * @return array |
||
251 | */ |
||
252 | public function getIds() |
||
268 | |||
269 | /** |
||
270 | * Returns the number of sub-nodes found (ie, related model objects across this relation.) |
||
271 | * @since $VID:$ |
||
272 | * @return int |
||
273 | */ |
||
274 | public function countSubNodes() |
||
278 | |||
279 | /** |
||
280 | * Don't serialize the models. Just record their names on some dynamic properties. |
||
281 | * @since $VID:$ |
||
282 | */ |
||
283 | public function __sleep() |
||
298 | |||
299 | /** |
||
300 | * Use the dynamic properties to instantiate the models we use. |
||
301 | * @since $VID:$ |
||
302 | * @throws EE_Error |
||
303 | * @throws InvalidArgumentException |
||
304 | * @throws InvalidDataTypeException |
||
305 | * @throws InvalidInterfaceException |
||
306 | * @throws ReflectionException |
||
307 | */ |
||
308 | public function __wakeup() |
||
314 | } |
||
315 | // End of file RelationNode.php |
||
317 |
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: