Complex classes like midcom_helper_reflector_copy 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_reflector_copy, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | class midcom_helper_reflector_copy extends midcom_baseclasses_components_purecode |
||
|
1 ignored issue
–
show
|
|||
| 17 | { |
||
| 18 | /** |
||
| 19 | * Source |
||
| 20 | * |
||
| 21 | * @var mixed GUID, MgdSchema or MidCOM dba object |
||
| 22 | */ |
||
| 23 | public $source = null; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Target |
||
| 27 | * |
||
| 28 | * @var mixed GUID, MgdSchema or MidCOM dba object |
||
| 29 | */ |
||
| 30 | public $target = null; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Exclusion list |
||
| 34 | * |
||
| 35 | * @var array List of GUIDs of objects that shall not be copied |
||
| 36 | */ |
||
| 37 | public $exclude = array(); |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Override properties of the new root object. This feature is |
||
| 41 | * directed for overriding e.g. parent information. |
||
| 42 | * |
||
| 43 | * @var array Property-value pairs |
||
| 44 | */ |
||
| 45 | public $root_object_values = array(); |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Switch for attachments |
||
| 49 | * |
||
| 50 | * @var boolean |
||
| 51 | */ |
||
| 52 | public $copy_attachments = true; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Switch for parameters |
||
| 56 | * |
||
| 57 | * @var boolean |
||
| 58 | */ |
||
| 59 | public $copy_parameters = true; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Switch for privileges |
||
| 63 | * |
||
| 64 | * @var boolean |
||
| 65 | */ |
||
| 66 | public $copy_privileges = true; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Switch for metadata |
||
| 70 | * |
||
| 71 | * @var boolean |
||
| 72 | */ |
||
| 73 | public $copy_metadata = true; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Copy the whole tree |
||
| 77 | * |
||
| 78 | * @var boolean |
||
| 79 | */ |
||
| 80 | public $copy_tree = true; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Switch for name catenating |
||
| 84 | * |
||
| 85 | * @var boolean |
||
| 86 | */ |
||
| 87 | public $allow_name_catenate = true; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Metadata fields that shall be copied |
||
| 91 | */ |
||
| 92 | public $copy_metadata_fields = array |
||
| 93 | ( |
||
| 94 | 'owner', |
||
| 95 | 'authors', |
||
| 96 | 'schedulestart', |
||
| 97 | 'scheduleend', |
||
| 98 | 'navnoentry', |
||
| 99 | 'hidden', |
||
| 100 | 'score', |
||
| 101 | ); |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Switch for halt on error. If this is set to false, errors will be |
||
| 105 | * reported, but will not stop executing |
||
| 106 | * |
||
| 107 | * @var boolean Set to false to continue on errors |
||
| 108 | */ |
||
| 109 | public $halt_on_errors = true; |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Encountered errors |
||
| 113 | * |
||
| 114 | * @var array |
||
| 115 | */ |
||
| 116 | public $errors = array(); |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Newly created objects |
||
| 120 | * |
||
| 121 | * @var array |
||
| 122 | */ |
||
| 123 | public $new_objects = array(); |
||
| 124 | |||
| 125 | /** |
||
| 126 | * New root object |
||
| 127 | */ |
||
| 128 | public $new_root_object = null; |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Properties for each encountered MgdSchema object |
||
| 132 | * |
||
| 133 | * @var array class_name => array of properties |
||
| 134 | */ |
||
| 135 | private $properties = array(); |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Get the newly created root object |
||
| 139 | * |
||
| 140 | * @return mixed Lowest level new MgdSchema object |
||
| 141 | */ |
||
| 142 | public function get_object() |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Get object properties |
||
| 149 | * |
||
| 150 | * @param mixed $object |
||
| 151 | * @return array |
||
| 152 | */ |
||
| 153 | public function get_object_properties($object) |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Get the parent property for overriding it |
||
| 182 | * |
||
| 183 | * @param mixed $object MgdSchema object for resolving the parent property |
||
| 184 | * @return string Parent property |
||
| 185 | */ |
||
| 186 | public function get_parent_property($object) |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Get the target properties and return an array that is used e.g. in copying |
||
| 194 | * |
||
| 195 | * @param mixed $object MgdSchema object or MidCOM db object |
||
| 196 | * @return array id, parent property, class and label of the object |
||
| 197 | */ |
||
| 198 | static public function get_target_properties($object) |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Resolve MgdSchema object from guid or miscellaneous extended object |
||
| 259 | * |
||
| 260 | * @param mixed &$object MgdSchema object, GUID or ID |
||
| 261 | * @return mixed MgdSchema object or false on failure |
||
| 262 | */ |
||
| 263 | static public function resolve_object(&$object) |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Copy an object tree. Both source and parent may be liberally filled. Source can be either |
||
| 288 | * MgdSchema or MidCOM db object or GUID of the object and parent can be |
||
| 289 | * |
||
| 290 | * - MgdSchema object |
||
| 291 | * - MidCOM db object |
||
| 292 | * - predefined target array (@see get_target_properties()) |
||
| 293 | * - ID or GUID of the object |
||
| 294 | * - left empty to copy as a parentless object |
||
| 295 | * |
||
| 296 | * This method is self-aware and will refuse to perform any infinite loops (e.g. to copy |
||
| 297 | * itself to its descendant, copying itself again and again and again). |
||
| 298 | * |
||
| 299 | * Eventually this method will return the first root object that was created, i.e. the root |
||
| 300 | * of the new tree. |
||
| 301 | * |
||
| 302 | * @param mixed $source GUID or MgdSchema object that will be copied |
||
| 303 | * @param mixed $parent MgdSchema or MidCOM db object, predefined array or ID of the parent object |
||
| 304 | * @return mixed False on failure, newly created MgdSchema root object on success |
||
| 305 | */ |
||
| 306 | public function copy_tree(&$source, &$parent) |
||
| 347 | |||
| 348 | /** |
||
| 349 | * Copy an object |
||
| 350 | * |
||
| 351 | * @param mixed &$source MgdSchema object for reading the parameters |
||
| 352 | * @param mixed &$parent MgdSchema parent object |
||
| 353 | * @param array $defaults |
||
| 354 | * @return boolean Indicating success |
||
| 355 | */ |
||
| 356 | public function copy_object(&$source, &$parent = null, array $defaults = array()) |
||
| 474 | |||
| 475 | /** |
||
| 476 | * Copy object data |
||
| 477 | * |
||
| 478 | * @param string $type The type of data to copy |
||
| 479 | * @param mixed &$source MgdSchema object for reading the parameters |
||
| 480 | * @param mixed &$target MgdSchema object for storing the parameters |
||
| 481 | * @return boolean Indicating success |
||
| 482 | */ |
||
| 483 | private function _copy_data($type, $source, &$target) |
||
| 494 | |||
| 495 | /** |
||
| 496 | * Copy parameters for the object |
||
| 497 | * |
||
| 498 | * @param mixed $source MgdSchema object for reading the parameters |
||
| 499 | * @param mixed $target MgdSchema object for storing the parameters |
||
| 500 | * @return boolean Indicating success |
||
| 501 | */ |
||
| 502 | public function copy_parameters($source, $target) |
||
| 532 | |||
| 533 | /** |
||
| 534 | * Copy metadata for the object |
||
| 535 | * |
||
| 536 | * @param mixed $source MgdSchema object for reading the metadata |
||
| 537 | * @param mixed $target MgdSchema object for storing the metadata |
||
| 538 | * @return boolean Indicating success |
||
| 539 | */ |
||
| 540 | public function copy_metadata($source, $target) |
||
| 555 | |||
| 556 | /** |
||
| 557 | * Copy attachments |
||
| 558 | * |
||
| 559 | * @param mixed &$source MgdSchema object for reading the attachments |
||
| 560 | * @param mixed &$target MgdSchema object for storing the attachments |
||
| 561 | * @return boolean Indicating success |
||
| 562 | */ |
||
| 563 | public function copy_attachments(&$source, &$target) |
||
| 577 | |||
| 578 | /** |
||
| 579 | * Copy privileges |
||
| 580 | * |
||
| 581 | * @param mixed $source MgdSchema object for reading the privileges |
||
| 582 | * @param mixed $target MgdSchema object for storing the privileges |
||
| 583 | * @return boolean Indicating success |
||
| 584 | */ |
||
| 585 | public function copy_privileges($source, $target) |
||
| 624 | |||
| 625 | /** |
||
| 626 | * Copy an object tree. Both source and parent may be liberally filled. Source can be either |
||
| 627 | * MgdSchema or MidCOM db object or GUID of the object and parent can be |
||
| 628 | * |
||
| 629 | * - MgdSchema object |
||
| 630 | * - MidCOM db object |
||
| 631 | * - predefined target array (@see get_target_properties()) |
||
| 632 | * - ID or GUID of the object |
||
| 633 | * - left empty to copy as a parentless object |
||
| 634 | * |
||
| 635 | * This method is self-aware and will refuse to perform any infinite loops (e.g. to copy |
||
| 636 | * itself to its descendant, copying itself again and again and again). |
||
| 637 | * |
||
| 638 | * Eventually this method will return the first root object that was created, i.e. the root |
||
| 639 | * of the new tree. |
||
| 640 | * |
||
| 641 | * @param mixed $source GUID or MgdSchema object that will be copied |
||
| 642 | * @param mixed $parent MgdSchema or MidCOM db object, predefined array or ID of the parent object |
||
| 643 | * @param array $exclude IDs that will be excluded from the copying |
||
| 644 | * @param boolean $copy_parameters Switch to determine if the parameters should be copied |
||
| 645 | * @param boolean $copy_metadata Switch to determine if the metadata should be copied (excluding created and published) |
||
| 646 | * @param boolean $copy_attachments Switch to determine if the attachments should be copied (creates only a new link, doesn't duplicate the content) |
||
| 647 | * @return mixed False on failure, newly created MgdSchema root object on success |
||
| 648 | */ |
||
| 649 | static public function copy_object_tree($source, $parent, $exclude = array(), $copy_parameters = true, $copy_metadata = true, $copy_attachments = true) |
||
| 665 | |||
| 666 | /** |
||
| 667 | * Dispatches the copy command according to the attributes set |
||
| 668 | * |
||
| 669 | * @return boolean Indicating success |
||
| 670 | */ |
||
| 671 | public function execute() |
||
| 699 | } |
||
| 700 |
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.