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:
Complex classes like EE_Model_Relation_Base often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use EE_Model_Relation_Base, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
18 | abstract class EE_Model_Relation_Base implements HasSchemaInterface |
||
19 | { |
||
20 | /** |
||
21 | * The model name of which this relation is a component (ie, the model that called new EE_Model_Relation_Base) |
||
22 | * |
||
23 | * @var string eg Event, Question_Group, Registration |
||
24 | */ |
||
25 | private $_this_model_name; |
||
26 | /** |
||
27 | * The model name pointed to by this relation (ie, the model we want to establish a relationship to) |
||
28 | * |
||
29 | * @var string eg Event, Question_Group, Registration |
||
30 | */ |
||
31 | private $_other_model_name; |
||
32 | |||
33 | /** |
||
34 | * this is typically used when calling the relation models to make sure they inherit any set timezone from the |
||
35 | * initiating model. |
||
36 | * |
||
37 | * @var string |
||
38 | */ |
||
39 | protected $_timezone; |
||
40 | |||
41 | /** |
||
42 | * If you try to delete "this_model", and there are related "other_models", |
||
43 | * and this isn't null, then abandon the deletion and add this warning. |
||
44 | * This effectively makes it impossible to delete "this_model" while there are |
||
45 | * related "other_models" along this relation. |
||
46 | * |
||
47 | * @var string (internationalized) |
||
48 | */ |
||
49 | protected $_blocking_delete_error_message; |
||
50 | |||
51 | protected $_blocking_delete = false; |
||
52 | |||
53 | /** |
||
54 | * Object representing the relationship between two models. This knows how to join the models, |
||
55 | * get related models across the relation, and add-and-remove the relationships. |
||
56 | * |
||
57 | * @param boolean $block_deletes if there are related models across this relation, block (prevent |
||
58 | * and add an error) the deletion of this model |
||
59 | * @param string $blocking_delete_error_message a customized error message on blocking deletes instead of the |
||
60 | * default |
||
61 | */ |
||
62 | public function __construct($block_deletes, $blocking_delete_error_message) |
||
67 | |||
68 | |||
69 | /** |
||
70 | * @param $this_model_name |
||
71 | * @param $other_model_name |
||
72 | * @throws EE_Error |
||
73 | */ |
||
74 | public function _construct_finalize_set_models($this_model_name, $other_model_name) |
||
84 | |||
85 | |||
86 | /** |
||
87 | * Gets the model where this relation is defined. |
||
88 | * |
||
89 | * @return EEM_Base |
||
90 | */ |
||
91 | public function get_this_model() |
||
95 | |||
96 | |||
97 | /** |
||
98 | * Gets the model which this relation establishes the relation TO (ie, |
||
99 | * this relation object was defined on get_this_model(), get_other_model() is the other one) |
||
100 | * |
||
101 | * @return EEM_Base |
||
102 | */ |
||
103 | public function get_other_model() |
||
107 | |||
108 | |||
109 | /** |
||
110 | * Internally used by get_this_model() and get_other_model() |
||
111 | * |
||
112 | * @param string $model_name like Event, Question_Group, etc. omit the EEM_ |
||
113 | * @return EEM_Base |
||
114 | */ |
||
115 | protected function _get_model($model_name) |
||
121 | |||
122 | |||
123 | /** |
||
124 | * entirely possible that relations may be called from a model and we need to make sure those relations have their |
||
125 | * timezone set correctly. |
||
126 | * |
||
127 | * @param string $timezone timezone to set. |
||
128 | */ |
||
129 | public function set_timezone($timezone) |
||
135 | |||
136 | |||
137 | /** |
||
138 | * @param $other_table |
||
139 | * @param $other_table_alias |
||
140 | * @param $other_table_column |
||
141 | * @param $this_table_alias |
||
142 | * @param $this_table_join_column |
||
143 | * @param string $extra_join_sql |
||
144 | * @return string |
||
145 | */ |
||
146 | protected function _left_join( |
||
156 | |||
157 | |||
158 | /** |
||
159 | * Gets all the model objects of type of other model related to $model_object, |
||
160 | * according to this relation. This is the same code for EE_HABTM_Relation and EE_Has_Many_Relation. |
||
161 | * For both of those child classes, $model_object must be saved so that it has an ID before querying, |
||
162 | * otherwise an error will be thrown. Note: by default we disable default_where_conditions |
||
163 | * EE_Belongs_To_Relation doesn't need to be saved before querying. |
||
164 | * |
||
165 | * @param EE_Base_Class|int $model_object_or_id or the primary key of this model |
||
166 | * @param array $query_params like EEM_Base::get_all's $query_params |
||
167 | * @param boolean $values_already_prepared_by_model_object @deprecated since 4.8.1 |
||
168 | * @return EE_Base_Class[] |
||
169 | * @throws \EE_Error |
||
170 | */ |
||
171 | public function get_all_related( |
||
189 | |||
190 | |||
191 | /** |
||
192 | * Alters the $query_params to disable default where conditions, unless otherwise specified |
||
193 | * |
||
194 | * @param string $query_params |
||
195 | * @return array |
||
196 | */ |
||
197 | protected function _disable_default_where_conditions_on_query_param($query_params) |
||
204 | |||
205 | |||
206 | /** |
||
207 | * Deletes the related model objects which meet the query parameters. If no |
||
208 | * parameters are specified, then all related model objects will be deleted. |
||
209 | * Note: If the related model is extends EEM_Soft_Delete_Base, then the related |
||
210 | * model objects will only be soft-deleted. |
||
211 | * |
||
212 | * @param EE_Base_Class|int|string $model_object_or_id |
||
213 | * @param array $query_params |
||
214 | * @return int of how many related models got deleted |
||
215 | * @throws \EE_Error |
||
216 | */ |
||
217 | public function delete_all_related($model_object_or_id, $query_params = array()) |
||
235 | |||
236 | |||
237 | /** |
||
238 | * Deletes the related model objects which meet the query parameters. If no |
||
239 | * parameters are specified, then all related model objects will be deleted. |
||
240 | * Note: If the related model is extends EEM_Soft_Delete_Base, then the related |
||
241 | * model objects will only be soft-deleted. |
||
242 | * |
||
243 | * @param EE_Base_Class|int|string $model_object_or_id |
||
244 | * @param array $query_params |
||
245 | * @return int of how many related models got deleted |
||
246 | * @throws \EE_Error |
||
247 | */ |
||
248 | public function delete_related_permanently($model_object_or_id, $query_params = array()) |
||
279 | |||
280 | |||
281 | /** |
||
282 | * this just returns a model_object_id for incoming item that could be an object or id. |
||
283 | * |
||
284 | * @param EE_Base_Class|int $model_object_or_id model object or the primary key of this model |
||
285 | * @throws EE_Error |
||
286 | * @return int |
||
287 | */ |
||
288 | protected function _get_model_object_id($model_object_or_id) |
||
301 | |||
302 | |||
303 | /** |
||
304 | * Gets the SQL string for performing the join between this model and the other model. |
||
305 | * |
||
306 | * @param string $model_relation_chain like 'Event.Event_Venue.Venue' |
||
307 | * @return string of SQL, eg "LEFT JOIN table_name AS table_alias ON this_model_primary_table.pk = |
||
308 | * other_model_primary_table.fk" etc |
||
309 | */ |
||
310 | abstract public function get_join_statement($model_relation_chain); |
||
311 | |||
312 | |||
313 | /** |
||
314 | * Adds a relationships between the two model objects provided. Each type of relationship handles this differently |
||
315 | * (EE_Belongs_To is a slight exception, it should more accurately be called set_relation_to(...), as this |
||
316 | * relationship only allows this model to be related to a single other model of this type) |
||
317 | * |
||
318 | * @param $this_obj_or_id |
||
319 | * @param $other_obj_or_id |
||
320 | * @param array $extra_join_model_fields_n_values |
||
321 | * @return \EE_Base_Class the EE_Base_Class which was added as a relation. (Convenient if you only pass an ID for |
||
322 | * $other_obj_or_id) |
||
323 | */ |
||
324 | abstract public function add_relation_to( |
||
329 | |||
330 | |||
331 | /** |
||
332 | * Similar to 'add_relation_to(...)', performs the opposite action of removing the relationship between the two |
||
333 | * model objects |
||
334 | * |
||
335 | * @param $this_obj_or_id |
||
336 | * @param $other_obj_or_id |
||
337 | * @param array $where_query |
||
338 | * @return bool |
||
339 | */ |
||
340 | abstract public function remove_relation_to($this_obj_or_id, $other_obj_or_id, $where_query = array()); |
||
341 | |||
342 | |||
343 | /** |
||
344 | * Removes ALL relation instances for this relation obj |
||
345 | * |
||
346 | * @param EE_Base_Class|int $this_obj_or_id |
||
347 | * @param array $where_query_param like EEM_Base::get_all's $query_params[0] (where conditions) |
||
348 | * @return EE_Base_Class[] |
||
349 | * @throws \EE_Error |
||
350 | */ |
||
351 | public function remove_relations($this_obj_or_id, $where_query_param = array()) |
||
360 | |||
361 | |||
362 | /** |
||
363 | * If you aren't allowed to delete this model when there are related models across this |
||
364 | * relation object, return true. Otherwise, if you can delete this model even though |
||
365 | * related objects exist, returns false. |
||
366 | * |
||
367 | * @return boolean |
||
368 | */ |
||
369 | public function block_delete_if_related_models_exist() |
||
373 | |||
374 | |||
375 | /** |
||
376 | * Gets the error message to show |
||
377 | * |
||
378 | * @return string |
||
379 | */ |
||
380 | public function get_deletion_error_message() |
||
395 | |||
396 | /** |
||
397 | * Returns whatever is set as the nicename for the object. |
||
398 | * |
||
399 | * @return string |
||
400 | */ |
||
401 | public function getSchemaDescription() |
||
402 | { |
||
403 | $description = $this instanceof EE_Belongs_To_Relation |
||
404 | ? esc_html__('The related %1$s entity to the %2$s.', 'event_espresso') |
||
405 | : esc_html__('The related %1$s entities to the %2$s.', 'event_espresso'); |
||
406 | return sprintf( |
||
407 | $description, |
||
408 | $this->get_other_model()->get_this_model_name(), |
||
409 | $this->get_this_model()->get_this_model_name() |
||
410 | ); |
||
411 | } |
||
412 | |||
413 | /** |
||
414 | * Returns whatever is set as the $_schema_type property for the object. |
||
415 | * Note: this will automatically add 'null' to the schema if the object is_nullable() |
||
416 | * |
||
417 | * @return string|array |
||
418 | */ |
||
419 | public function getSchemaType() |
||
420 | { |
||
421 | return $this instanceof EE_Belongs_To_Relation ? 'object' : 'array'; |
||
422 | } |
||
423 | |||
424 | /** |
||
425 | * This is usually present when the $_schema_type property is 'object'. Any child classes will need to override |
||
426 | * this method and return the properties for the schema. |
||
427 | * The reason this is not a property on the class is because there may be filters set on the values for the property |
||
428 | * that won't be exposed on construct. For example enum type schemas may have the enum values filtered. |
||
429 | * |
||
430 | * @return array |
||
431 | */ |
||
432 | public function getSchemaProperties() |
||
436 | |||
437 | /** |
||
438 | * If a child class has enum values, they should override this method and provide a simple array |
||
439 | * of the enum values. |
||
440 | * The reason this is not a property on the class is because there may be filterable enum values that |
||
441 | * are set on the instantiated object that could be filtered after construct. |
||
442 | * |
||
443 | * @return array |
||
444 | */ |
||
445 | public function getSchemaEnum() |
||
449 | |||
450 | /** |
||
451 | * This returns the value of the $_schema_format property on the object. |
||
452 | * |
||
453 | * @return string |
||
454 | */ |
||
455 | public function getSchemaFormat() |
||
459 | |||
460 | /** |
||
461 | * This returns the value of the $_schema_readonly property on the object. |
||
462 | * |
||
463 | * @return bool |
||
464 | */ |
||
465 | public function getSchemaReadonly() |
||
469 | |||
470 | /** |
||
471 | * This returns elements used to represent this field in the json schema. |
||
472 | * |
||
473 | * @link http://json-schema.org/ |
||
474 | * @return array |
||
475 | */ |
||
476 | public function getSchema() |
||
498 | } |
||
499 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: