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_Field_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_Field_Base, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | abstract class EE_Model_Field_Base implements HasSchemaInterface |
||
| 23 | { |
||
| 24 | /** |
||
| 25 | * The alias for the table the column belongs to. |
||
| 26 | * @var string |
||
| 27 | */ |
||
| 28 | protected $_table_alias; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * The actual db column name for the table |
||
| 32 | * @var string |
||
| 33 | */ |
||
| 34 | protected $_table_column; |
||
| 35 | |||
| 36 | |||
| 37 | /** |
||
| 38 | * The authoritative name for the table column (used by client code to reference the field). |
||
| 39 | * @var string |
||
| 40 | */ |
||
| 41 | protected $_name; |
||
| 42 | |||
| 43 | |||
| 44 | /** |
||
| 45 | * A description for the field. |
||
| 46 | * @var string |
||
| 47 | */ |
||
| 48 | protected $_nicename; |
||
| 49 | |||
| 50 | |||
| 51 | /** |
||
| 52 | * Whether the field is nullable or not |
||
| 53 | * @var bool |
||
| 54 | */ |
||
| 55 | protected $_nullable; |
||
| 56 | |||
| 57 | |||
| 58 | /** |
||
| 59 | * What the default value for the field should be. |
||
| 60 | * @var mixed |
||
| 61 | */ |
||
| 62 | protected $_default_value; |
||
| 63 | |||
| 64 | |||
| 65 | /** |
||
| 66 | * Other configuration for the field |
||
| 67 | * @var mixed |
||
| 68 | */ |
||
| 69 | protected $_other_config; |
||
| 70 | |||
| 71 | |||
| 72 | /** |
||
| 73 | * The name of the model this field is instantiated for. |
||
| 74 | * @var string |
||
| 75 | */ |
||
| 76 | protected $_model_name; |
||
| 77 | |||
| 78 | |||
| 79 | /** |
||
| 80 | * This should be a json-schema valid data type for the field. |
||
| 81 | * @link http://json-schema.org/latest/json-schema-core.html#rfc.section.4.2 |
||
| 82 | * @var string |
||
| 83 | */ |
||
| 84 | private $_schema_type = 'string'; |
||
| 85 | |||
| 86 | |||
| 87 | /** |
||
| 88 | * If the schema has a defined format then it should be defined via this property. |
||
| 89 | * @link http://json-schema.org/latest/json-schema-validation.html#rfc.section.7 |
||
| 90 | * @var string |
||
| 91 | */ |
||
| 92 | private $_schema_format = ''; |
||
| 93 | |||
| 94 | |||
| 95 | /** |
||
| 96 | * Indicates that the value of the field is managed exclusively by the server/model and not something |
||
| 97 | * settable by client code. |
||
| 98 | * @link http://json-schema.org/latest/json-schema-hypermedia.html#rfc.section.4.4 |
||
| 99 | * @var bool |
||
| 100 | */ |
||
| 101 | private $_schema_readonly = false; |
||
| 102 | |||
| 103 | |||
| 104 | /** |
||
| 105 | * @param string $table_column |
||
| 106 | * @param string $nicename |
||
| 107 | * @param bool $nullable |
||
| 108 | * @param null $default_value |
||
| 109 | */ |
||
| 110 | public function __construct($table_column, $nicename, $nullable, $default_value = null) |
||
| 117 | |||
| 118 | |||
| 119 | /** |
||
| 120 | * @param $table_alias |
||
| 121 | * @param $name |
||
| 122 | * @param $model_name |
||
| 123 | */ |
||
| 124 | public function _construct_finalize($table_alias, $name, $model_name) |
||
| 137 | |||
| 138 | public function get_table_alias() |
||
| 142 | |||
| 143 | public function get_table_column() |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Returns the name of the model this field is on. Eg 'Event' or 'Ticket_Datetime' |
||
| 150 | * |
||
| 151 | * @return string |
||
| 152 | */ |
||
| 153 | public function get_model_name() |
||
| 157 | |||
| 158 | /** |
||
| 159 | * @throws \EE_Error |
||
| 160 | * @return string |
||
| 161 | */ |
||
| 162 | public function get_name() |
||
| 171 | |||
| 172 | public function get_nicename() |
||
| 176 | |||
| 177 | public function is_nullable() |
||
| 181 | |||
| 182 | /** |
||
| 183 | * returns whether this field is an auto-increment field or not. If it is, then |
||
| 184 | * on insertion it can be null. However, on updates it must be present. |
||
| 185 | * |
||
| 186 | * @return boolean |
||
| 187 | */ |
||
| 188 | public function is_auto_increment() |
||
| 192 | |||
| 193 | /** |
||
| 194 | * The default value in the model object's value domain. See lengthy comment about |
||
| 195 | * value domains at the top of EEM_Base |
||
| 196 | * |
||
| 197 | * @return mixed |
||
| 198 | */ |
||
| 199 | public function get_default_value() |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Returns the table alias joined to the table column, however this isn't the right |
||
| 206 | * table alias if the aliased table is being joined to. In that case, you can use |
||
| 207 | * EE_Model_Parser::extract_table_alias_model_relation_chain_prefix() to find the table's current alias |
||
| 208 | * in the current query |
||
| 209 | * |
||
| 210 | * @return string |
||
| 211 | */ |
||
| 212 | public function get_qualified_column() |
||
| 216 | |||
| 217 | /** |
||
| 218 | * When get() is called on a model object (eg EE_Event), before returning its value, |
||
| 219 | * call this function on it, allowing us to customize the returned value based on |
||
| 220 | * the field's type. Eg, we may want ot serialize it, strip tags, etc. By default, |
||
| 221 | * we simply return it. |
||
| 222 | * |
||
| 223 | * @param mixed $value_of_field_on_model_object |
||
| 224 | * @return mixed |
||
| 225 | */ |
||
| 226 | public function prepare_for_get($value_of_field_on_model_object) |
||
| 230 | |||
| 231 | /** |
||
| 232 | * When inserting or updating a field on a model object, run this function on each |
||
| 233 | * value to prepare it for insertion into the db. We may want to add slashes, serialize it, etc. |
||
| 234 | * By default, we do nothing. |
||
| 235 | * |
||
| 236 | * @param mixed $value_of_field_on_model_object |
||
| 237 | * @return mixed |
||
| 238 | */ |
||
| 239 | public function prepare_for_use_in_db($value_of_field_on_model_object) |
||
| 243 | |||
| 244 | /** |
||
| 245 | * When creating a brand-new model object, or setting a particular value for one of its fields, this function |
||
| 246 | * is called before setting it on the model object. We may want to strip slashes, unserialize the value, etc. |
||
| 247 | * By default, we do nothing. |
||
| 248 | * |
||
| 249 | * @param mixed $value_inputted_for_field_on_model_object |
||
| 250 | * @return mixed |
||
| 251 | */ |
||
| 252 | public function prepare_for_set($value_inputted_for_field_on_model_object) |
||
| 256 | |||
| 257 | |||
| 258 | /** |
||
| 259 | * When instantiating a model object from DB results, this function is called before setting each field. |
||
| 260 | * We may want to serialize the value, etc. By default, we return the value using prepare_for_set() method as that |
||
| 261 | * is the one child classes will most often define. |
||
| 262 | * |
||
| 263 | * @param mixed $value_found_in_db_for_model_object |
||
| 264 | * @return mixed |
||
| 265 | */ |
||
| 266 | public function prepare_for_set_from_db($value_found_in_db_for_model_object) |
||
| 270 | |||
| 271 | /** |
||
| 272 | * When echoing a field's value on a model object, this function is run to prepare the value for presentation in a |
||
| 273 | * webpage. For example, we may want to output floats with 2 decimal places by default, dates as "Monday Jan 12, |
||
| 274 | * 2013, at 3:23pm" instead of |
||
| 275 | * "8765678632", or any other modifications to how the value should be displayed, but not modified itself. |
||
| 276 | * |
||
| 277 | * @param mixed $value_on_field_to_be_outputted |
||
| 278 | * @return mixed |
||
| 279 | */ |
||
| 280 | public function prepare_for_pretty_echoing($value_on_field_to_be_outputted) |
||
| 284 | |||
| 285 | |||
| 286 | /** |
||
| 287 | * Returns whatever is set as the nicename for the object. |
||
| 288 | * @return string |
||
| 289 | */ |
||
| 290 | public function getSchemaDescription() |
||
| 294 | |||
| 295 | |||
| 296 | /** |
||
| 297 | * Returns whatever is set as the $_schema_type property for the object. |
||
| 298 | * Note: this will automatically add 'null' to the schema if the object is_nullable() |
||
| 299 | * @return string|array |
||
| 300 | */ |
||
| 301 | public function getSchemaType() |
||
| 311 | |||
| 312 | |||
| 313 | /** |
||
| 314 | * Sets the _schema_type property. Child classes should call this in their constructors to override the default state |
||
| 315 | * for this property. |
||
| 316 | * @param string|array $type |
||
| 317 | * @throws InvalidArgumentException |
||
| 318 | */ |
||
| 319 | protected function setSchemaType($type) |
||
| 324 | |||
| 325 | |||
| 326 | /** |
||
| 327 | * This is usually present when the $_schema_type property is 'object'. Any child classes will need to override |
||
| 328 | * this method and return the properties for the schema. |
||
| 329 | * |
||
| 330 | * The reason this is not a property on the class is because there may be filters set on the values for the property |
||
| 331 | * that won't be exposed on construct. For example enum type schemas may have the enum values filtered. |
||
| 332 | * |
||
| 333 | * @return array |
||
| 334 | */ |
||
| 335 | public function getSchemaProperties() |
||
| 339 | |||
| 340 | |||
| 341 | /** |
||
| 342 | * If a child class has enum values, they should override this method and provide a simple array |
||
| 343 | * of the enum values. |
||
| 344 | |||
| 345 | * The reason this is not a property on the class is because there may be filterable enum values that |
||
| 346 | * are set on the instantiated object that could be filtered after construct. |
||
| 347 | * |
||
| 348 | * @return array |
||
| 349 | */ |
||
| 350 | public function getSchemaEnum() |
||
| 354 | |||
| 355 | |||
| 356 | /** |
||
| 357 | * This returns the value of the $_schema_format property on the object. |
||
| 358 | * @return string |
||
| 359 | */ |
||
| 360 | public function getSchemaFormat() |
||
| 364 | |||
| 365 | |||
| 366 | /** |
||
| 367 | * Sets the schema format property. |
||
| 368 | * @throws InvalidArgumentException |
||
| 369 | * @param string $format |
||
| 370 | */ |
||
| 371 | protected function setSchemaFormat($format) |
||
| 376 | |||
| 377 | |||
| 378 | /** |
||
| 379 | * This returns the value of the $_schema_readonly property on the object. |
||
| 380 | * @return bool |
||
| 381 | */ |
||
| 382 | public function getSchemaReadonly() |
||
| 386 | |||
| 387 | |||
| 388 | /** |
||
| 389 | * This sets the value for the $_schema_readonly property. |
||
| 390 | * @param bool $readonly (only explicit boolean values are accepted) |
||
| 391 | */ |
||
| 392 | protected function setSchemaReadOnly($readonly) |
||
| 405 | |||
| 406 | |||
| 407 | |||
| 408 | |||
| 409 | /** |
||
| 410 | * Return `%d`, `%s` or `%f` to indicate the data type for the field. |
||
| 411 | * @uses _get_wpdb_data_type() |
||
| 412 | * |
||
| 413 | * @return string |
||
| 414 | */ |
||
| 415 | public function get_wpdb_data_type() |
||
| 419 | |||
| 420 | |||
| 421 | /** |
||
| 422 | * Return `%d`, `%s` or `%f` to indicate the data type for the field that should be indicated in wpdb queries. |
||
| 423 | * @param string $type Included if a specific type is requested. |
||
| 424 | * @uses get_schema_type() |
||
| 425 | * @return string |
||
| 426 | */ |
||
| 427 | protected function _get_wpdb_data_type($type='') |
||
| 454 | |||
| 455 | |||
| 456 | |||
| 457 | protected function _get_wpdb_data_type_for_type_array($type) |
||
| 483 | |||
| 484 | |||
| 485 | /** |
||
| 486 | * This returns elements used to represent this field in the json schema. |
||
| 487 | * |
||
| 488 | * @link http://json-schema.org/ |
||
| 489 | * @return array |
||
| 490 | */ |
||
| 491 | public function getSchema() |
||
| 516 | |||
| 517 | /** |
||
| 518 | * Some fields are in the database-only, (ie, used in queries etc), but shouldn't necessarily be part |
||
| 519 | * of the model objects (ie, client code shouldn't care to ever see their value... if client code does |
||
| 520 | * want to see their value, then they shouldn't be db-only fields!) |
||
| 521 | * Eg, when doing events as custom post types, querying the post_type is essential, but |
||
| 522 | * post_type is irrelevant for EE_Event objects (because they will ALL be of post_type 'esp_event'). |
||
| 523 | * By default, all fields aren't db-only. |
||
| 524 | * |
||
| 525 | * @return boolean |
||
| 526 | */ |
||
| 527 | public function is_db_only_field() |
||
| 531 | |||
| 532 | |||
| 533 | /** |
||
| 534 | * Validates the incoming string|array to ensure its an allowable type. |
||
| 535 | * @throws InvalidArgumentException |
||
| 536 | * @param string|array $type |
||
| 537 | */ |
||
| 538 | private function validateSchemaType($type) |
||
| 580 | |||
| 581 | |||
| 582 | /** |
||
| 583 | * Validates that the incoming format is an allowable string to use for the _schema_format property |
||
| 584 | * @throws InvalidArgumentException |
||
| 585 | * @param $format |
||
| 586 | */ |
||
| 587 | private function validateSchemaFormat($format) |
||
| 622 | } |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..