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 midcom_helper_datamanager2_datamanager 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 midcom_helper_datamanager2_datamanager, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | class midcom_helper_datamanager2_datamanager extends midcom_baseclasses_components_purecode |
||
|
1 ignored issue
–
show
|
|||
| 19 | { |
||
| 20 | /** |
||
| 21 | * The schema database to use for operation. This variable will always contain a parsed |
||
| 22 | * representation of the schema, so that one can swiftly switch between individual schemas |
||
| 23 | * of the Database. This is a list of midcom_helper_datamanager2_schema |
||
| 24 | * instances, indexed by their name. |
||
| 25 | * |
||
| 26 | * @var Array |
||
| 27 | */ |
||
| 28 | private $_schemadb = null; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * This variable holds the schema currently in use, it has been created from the array |
||
| 32 | * stored in the $_schemadb member. |
||
| 33 | * |
||
| 34 | * This object can be modified as long as the types are not initialized. If you change |
||
| 35 | * the schema afterwards, the changes will not propagate to any dependant object until |
||
| 36 | * you reinitialize the class. |
||
| 37 | * |
||
| 38 | * @var midcom_helper_datamanager2_schema |
||
| 39 | */ |
||
| 40 | public $schema = null; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * The id (array index) of the current schema |
||
| 44 | * @var string schema_name |
||
| 45 | */ |
||
| 46 | var $schema_name = ''; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * This is the storage implementation which is used for operation on the types. It encapsulates |
||
| 50 | * the storage target. |
||
| 51 | * |
||
| 52 | * @var midcom_helper_datamanager2_storage |
||
| 53 | */ |
||
| 54 | public $storage = null; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * This is a listing of all types that have been loaded from the storage object. You may |
||
| 58 | * manipulate these types and their values at will, and then store them back to the database |
||
| 59 | * using the functions available in this class. |
||
| 60 | * |
||
| 61 | * @var Array |
||
| 62 | */ |
||
| 63 | var $types = null; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * This variable contains an Array of all validation errors that have occurred |
||
| 67 | * during saving. As outlined in the type, these messages my have inline-html |
||
| 68 | * in it and it is assumed to be localized. |
||
| 69 | * |
||
| 70 | * The errors are indexed by field name. |
||
| 71 | * |
||
| 72 | * @var Array |
||
| 73 | * @see midcom_helper_datamanager2_type::$validation_error |
||
| 74 | */ |
||
| 75 | var $validation_errors = Array(); |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Reference to the form manager instance which is currently in use. Usually, it is created and referenced here by the controller |
||
| 79 | * class during initialization. |
||
| 80 | * |
||
| 81 | * @var midcom_helper_datamanager2_formmanager |
||
| 82 | */ |
||
| 83 | var $formmanager = null; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * The constructor loads the schema database to use but does nothing else |
||
| 87 | * so far. |
||
| 88 | * |
||
| 89 | * @param array &$schemadb A list of midcom_helper_datamanager2_schema instances, |
||
| 90 | * indexed by their schema name. This member is taken by reference. |
||
| 91 | * @see midcom_helper_datamanager2_schema::load_database() |
||
| 92 | */ |
||
| 93 | public function __construct(array &$schemadb) |
||
| 98 | |||
| 99 | /** |
||
| 100 | * This function activates the given schema. This will drop all existing types |
||
| 101 | * and create a new set of them which are in the default state at this point. |
||
| 102 | * |
||
| 103 | * This will reset the existing schema and type listing. If a storage object |
||
| 104 | * exists, the change of the schema will be propagated implicitly, as it will |
||
| 105 | * reference the schema member of ours. |
||
| 106 | * |
||
| 107 | * @param string $name The name of the schema to use, omit this to use the default |
||
| 108 | * schema. |
||
| 109 | * @return boolean Indicating success. |
||
| 110 | */ |
||
| 111 | function set_schema($name = null) |
||
| 129 | |||
| 130 | /** |
||
| 131 | * This function sets the system to use a specific storage object. You can pass |
||
| 132 | * either a MidCOM DBA object or a fully initialized storage subclass. The former |
||
| 133 | * is automatically wrapped in a midcom storage object. If you pass your own |
||
| 134 | * storage object, ensure that it uses the same schema as this class. Ideally, |
||
| 135 | * you should use references for this. |
||
| 136 | * |
||
| 137 | * This call will fail if there is no schema set. All types will be set and |
||
| 138 | * initialized to the new storage object. Thus, it is possible to call set_storage |
||
| 139 | * repeatedly thus switching an existing DM instance over to a new storage object |
||
| 140 | * as long as you work with the same schema. |
||
| 141 | * |
||
| 142 | * @param mixed $object A reference to either a MidCOM DBA class or a subclass of |
||
| 143 | * midcom_helper_datamanager2_storage. |
||
| 144 | * @return boolean Indicating success. |
||
| 145 | */ |
||
| 146 | function set_storage($object) |
||
| 175 | |||
| 176 | /** |
||
| 177 | * This function will create all type objects for the current schema. It will load class |
||
| 178 | * files where necessary (using require_once), and then create a set of instances |
||
| 179 | * based on the schema. |
||
| 180 | * |
||
| 181 | * @return boolean Indicating success |
||
| 182 | */ |
||
| 183 | private function _load_types() |
||
| 201 | |||
| 202 | private function _load_type($name, $config) |
||
| 227 | |||
| 228 | /** |
||
| 229 | * This function is a shortcut that combines set_schema and set_storage together. |
||
| 230 | * The schema name is looked up in the parameter 'midcom.helper.datamanager2/schema_name', |
||
| 231 | * if it is not found, the first schema from the schema database is used implicitly. |
||
| 232 | * |
||
| 233 | * @see set_schema() |
||
| 234 | * @see set_storage() |
||
| 235 | * @param mixed $object Either a MidCOM DBA class or a subclass of midcom_helper_datamanager2_storage. |
||
| 236 | * @param boolean $strict Whether we should strictly use only the schema given by object params |
||
| 237 | * @return boolean Indicating success. |
||
| 238 | */ |
||
| 239 | function autoset_storage($object, $strict = false) |
||
| 275 | |||
| 276 | /** |
||
| 277 | * This function will cycle through all fields of the loaded schema, and run a "recreate" method for the |
||
| 278 | * type, if it has one. |
||
| 279 | * |
||
| 280 | * This can be used for regenerating all images or converted files of a given object when for instance |
||
| 281 | * schema changes. |
||
| 282 | * |
||
| 283 | * @return boolean |
||
| 284 | */ |
||
| 285 | function recreate() |
||
| 303 | |||
| 304 | /** |
||
| 305 | * This function will save the current state of all types to disk. A full |
||
| 306 | * validation cycle is done beforehand, if any validation fails, the function |
||
| 307 | * aborts and sets the $validation_errors member variable accordingly. |
||
| 308 | * |
||
| 309 | * @return boolean Indicating success |
||
| 310 | */ |
||
| 311 | function save() |
||
| 323 | |||
| 324 | /** |
||
| 325 | * Validate the current object state. It will populate $validation_errors |
||
| 326 | * accordingly. |
||
| 327 | * |
||
| 328 | * @return boolean Indicating validation success. |
||
| 329 | */ |
||
| 330 | function validate() |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Very basic schema sanity checking, raises messages as needed |
||
| 351 | * |
||
| 352 | * @param string $name field name |
||
| 353 | * @return boolean indicating b0rkedness |
||
| 354 | */ |
||
| 355 | function _schema_field_is_broken($name) |
||
| 368 | |||
| 369 | /** |
||
| 370 | * Little helper function returning an associative array of all field values converted to HTML |
||
| 371 | * using their default convert_to_html option. |
||
| 372 | * |
||
| 373 | * @return Array All field values in their HTML representation indexed by their name. |
||
| 374 | */ |
||
| 375 | function get_content_html() |
||
| 403 | |||
| 404 | /** |
||
| 405 | * Little helper function returning an associative array of all field values converted to XML |
||
| 406 | * using their default convert_to_csv or convert_to_raw options. |
||
| 407 | * |
||
| 408 | * @return Array All field values in their XML representation indexed by their name. |
||
| 409 | */ |
||
| 410 | function get_content_xml() |
||
| 436 | |||
| 437 | /** |
||
| 438 | * Little helper function returning an associative array of all field values converted to CSV |
||
| 439 | * using their default convert_to_csv option. |
||
| 440 | * |
||
| 441 | * @return Array All field values in their CSV representation indexed by their name. |
||
| 442 | */ |
||
| 443 | View Code Duplication | function get_content_csv() |
|
| 456 | |||
| 457 | /** |
||
| 458 | * Little helper function returning an associative array of all field values converted to email-friendly format |
||
| 459 | * using their default convert_to_email option. |
||
| 460 | * |
||
| 461 | * @return Array All field values in their CSV representation indexed by their name. |
||
| 462 | */ |
||
| 463 | View Code Duplication | function get_content_email() |
|
| 476 | |||
| 477 | /** |
||
| 478 | * Get all field values converted to their raw storage representation |
||
| 479 | * |
||
| 480 | * @return Array All field values in their raw storage representation indexed by their name. |
||
| 481 | */ |
||
| 482 | View Code Duplication | function get_content_raw() |
|
| 495 | |||
| 496 | /** |
||
| 497 | * This function displays a quick view of the record, using some simple div based layout, |
||
| 498 | * which can be formatted using CSS. |
||
| 499 | * |
||
| 500 | * Be aware that this is only geared for simple administration interfaces, it will provide |
||
| 501 | * *no* editing capabilities (like AJAX) etc. If you want that to work, you need a formmanger |
||
| 502 | * instance instead. |
||
| 503 | * |
||
| 504 | * @param boolean $skip_empty Should empty fields be rendered or not |
||
| 505 | */ |
||
| 506 | function display_view($skip_empty = false) |
||
| 607 | } |
||
| 608 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.