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 AbstractCmisObject 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 AbstractCmisObject, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
43 | abstract class AbstractCmisObject implements CmisObjectInterface |
||
44 | { |
||
45 | /** |
||
46 | * @var SessionInterface |
||
47 | */ |
||
48 | protected $session; |
||
49 | |||
50 | /** |
||
51 | * @var ObjectTypeInterface |
||
52 | */ |
||
53 | protected $objectType; |
||
54 | |||
55 | /** |
||
56 | * @var null|SecondaryTypeInterface[] |
||
57 | */ |
||
58 | protected $secondaryTypes; |
||
59 | |||
60 | /** |
||
61 | * @var PropertyInterface[] |
||
62 | */ |
||
63 | protected $properties = array(); |
||
64 | |||
65 | /** |
||
66 | * @var AllowableActionsInterface|null |
||
67 | */ |
||
68 | protected $allowableActions; |
||
69 | |||
70 | /** |
||
71 | * @var RenditionInterface[] |
||
72 | */ |
||
73 | protected $renditions = array(); |
||
74 | |||
75 | /** |
||
76 | * @var AclInterface|null |
||
77 | */ |
||
78 | protected $acl; |
||
79 | |||
80 | /** |
||
81 | * @var PolicyInterface[] |
||
82 | */ |
||
83 | protected $policies = array(); |
||
84 | |||
85 | /** |
||
86 | * @var RelationshipInterface[] |
||
87 | */ |
||
88 | protected $relationships = array(); |
||
89 | |||
90 | /** |
||
91 | * A list that contains a list of <code>CmisExtensionElementInterface</code> identified by |
||
92 | * <code>ExtensionLevel</code>. The key is the string representation of <code>ExtensionLevel</code> and the value |
||
93 | * the array of <code>CmisExtensionElementInterface</code> |
||
94 | * |
||
95 | * @see ExtensionLevel |
||
96 | * @see CmisExtensionElementInterface |
||
97 | * @var array[] |
||
98 | */ |
||
99 | protected $extensions = array(); |
||
100 | |||
101 | /** |
||
102 | * @var OperationContextInterface |
||
103 | */ |
||
104 | protected $creationContext; |
||
105 | |||
106 | /** |
||
107 | * @var integer |
||
108 | */ |
||
109 | protected $refreshTimestamp = 0; |
||
110 | |||
111 | /** |
||
112 | * Initialize the CMIS Object |
||
113 | * |
||
114 | * @param SessionInterface $session |
||
115 | * @param ObjectTypeInterface $objectType |
||
116 | * @param OperationContextInterface $context |
||
117 | * @param ObjectDataInterface|null $objectData |
||
118 | */ |
||
119 | public function initialize( |
||
145 | |||
146 | /** |
||
147 | * Handle initialization for objectData |
||
148 | * |
||
149 | * @param ObjectDataInterface $objectData |
||
150 | */ |
||
151 | private function initializeObjectData(ObjectDataInterface $objectData) |
||
196 | |||
197 | /** |
||
198 | * Handle initialization of properties from the object data |
||
199 | * |
||
200 | * @param PropertiesInterface $properties |
||
201 | */ |
||
202 | private function initializeObjectDataProperties(PropertiesInterface $properties) |
||
226 | |||
227 | /** |
||
228 | * Handle initialization of policies from the object data |
||
229 | * |
||
230 | * @param PolicyIdListInterface $policies |
||
231 | */ |
||
232 | private function initializeObjectDataPolicies(PolicyIdListInterface $policies) |
||
243 | |||
244 | |||
245 | /** |
||
246 | * Returns a list of missing property keys |
||
247 | * |
||
248 | * @param PropertyDefinitionInterface[]|null $properties |
||
249 | * @return array |
||
250 | */ |
||
251 | 4 | protected function getMissingBaseProperties(array $properties = null) |
|
269 | |||
270 | /** |
||
271 | * Returns the session object |
||
272 | * |
||
273 | * @return SessionInterface |
||
274 | */ |
||
275 | protected function getSession() |
||
279 | |||
280 | /** |
||
281 | * Returns the repository id |
||
282 | * |
||
283 | * @return string |
||
284 | */ |
||
285 | protected function getRepositoryId() |
||
289 | |||
290 | /** |
||
291 | * Returns the object type |
||
292 | * |
||
293 | * @return ObjectTypeInterface |
||
294 | */ |
||
295 | protected function getObjectType() |
||
299 | |||
300 | /** |
||
301 | * Returns the object factory. |
||
302 | * |
||
303 | * @return ObjectFactoryInterface |
||
304 | */ |
||
305 | protected function getObjectFactory() |
||
309 | |||
310 | /** |
||
311 | * Get the binding object |
||
312 | * |
||
313 | * @return CmisBindingInterface |
||
314 | */ |
||
315 | protected function getBinding() |
||
319 | |||
320 | /** |
||
321 | * Returns the OperationContext that was used to create this object. |
||
322 | * |
||
323 | * @return OperationContextInterface |
||
324 | */ |
||
325 | protected function getCreationContext() |
||
329 | |||
330 | /** |
||
331 | * Returns the query name of a property. |
||
332 | * |
||
333 | * @param string $propertyId |
||
334 | * @return null|string |
||
335 | */ |
||
336 | protected function getPropertyQueryName($propertyId) |
||
345 | |||
346 | /** |
||
347 | * Delete this object |
||
348 | * |
||
349 | * @param boolean $allVersions indicates if all versions of the object should be deleted |
||
350 | */ |
||
351 | public function delete($allVersions = true) |
||
355 | |||
356 | /** |
||
357 | * Updates the provided properties. If the repository created a new object, for example a new version, |
||
358 | * the object ID of the new object is returned. Otherwise the object ID of the current object is returned. |
||
359 | * |
||
360 | * @param mixed[] $properties the properties to update |
||
361 | * @param boolean $refresh <code>true</code> if this object should be refresh after the update, |
||
362 | * <code>false</code> if not |
||
363 | * @return CmisObjectInterface|null the object ID of the updated object - can return <code>null</code> in case |
||
364 | * of a repository failure |
||
365 | */ |
||
366 | public function updateProperties(array $properties, $refresh = true) |
||
410 | |||
411 | /** |
||
412 | * Renames this object (changes the value of cmis:name). |
||
413 | * If the repository created a new object, for example a new version, the object id of the |
||
414 | * new object is returned. Otherwise the object id of the current object is returned. |
||
415 | * |
||
416 | * @param string $newName the new name, not <code>null</code> or empty |
||
417 | * @param boolean $refresh <code>true</code> if this object should be refresh after the update, |
||
418 | * <code>false</code> if not |
||
419 | * @return CmisObjectInterface|null the object ID of the updated object - can return <code>null</code> in case of |
||
420 | * a repository failure |
||
421 | */ |
||
422 | public function rename($newName, $refresh = true) |
||
437 | |||
438 | /** |
||
439 | * Returns the type of this CMIS object (object type identified by <code>cmis:objectTypeId</code>). |
||
440 | * |
||
441 | * @return ObjectTypeInterface the type of the object or <code>null</code> if the property |
||
442 | * <code>cmis:objectTypeId</code> hasn't been requested or hasn't been provided by the repository |
||
443 | */ |
||
444 | public function getType() |
||
448 | |||
449 | /** |
||
450 | * Returns the base type of this CMIS object (object type identified by cmis:baseTypeId). |
||
451 | * |
||
452 | * @return ObjectTypeInterface the base type of the object or <code>null</code> if the property cmis:baseTypeId |
||
453 | * hasn't been requested or hasn't been provided by the repository |
||
454 | */ |
||
455 | public function getBaseType() |
||
464 | |||
465 | /** |
||
466 | * Returns the base type of this CMIS object (object type identified by cmis:baseTypeId). |
||
467 | * |
||
468 | * @return BaseTypeId|null the base type of the object or <code>null</code> if the property |
||
469 | * cmis:baseTypeId hasn't been requested or hasn't been provided by the repository |
||
470 | */ |
||
471 | public function getBaseTypeId() |
||
480 | |||
481 | /** |
||
482 | * Returns the change token (CMIS property cmis:changeToken). |
||
483 | * |
||
484 | * @return string the change token of the object or <code>null</code> if the property hasn't been requested or |
||
485 | * hasn't been provided or isn't supported by the repository |
||
486 | */ |
||
487 | public function getChangeToken() |
||
491 | |||
492 | /** |
||
493 | * Returns the user who created this CMIS object (CMIS property cmis:createdBy). |
||
494 | * |
||
495 | * @return string the creator of the object or <code>null</code> if the property hasn't been requested or hasn't |
||
496 | * been provided by the repository |
||
497 | */ |
||
498 | public function getCreatedBy() |
||
502 | |||
503 | /** |
||
504 | * Returns the timestamp when this CMIS object has been created (CMIS property cmis:creationDate). |
||
505 | * |
||
506 | * @return \DateTime|null the creation time of the object or <code>null</code> if the property hasn't been |
||
507 | * requested or hasn't been provided by the repository |
||
508 | */ |
||
509 | public function getCreationDate() |
||
513 | |||
514 | /** |
||
515 | * Returns the object ID |
||
516 | * |
||
517 | * @return string |
||
518 | */ |
||
519 | public function getId() |
||
523 | |||
524 | /** |
||
525 | * Returns the timestamp when this CMIS object has been modified (CMIS property cmis:lastModificationDate). |
||
526 | * |
||
527 | * @return \DateTime|null the last modification date of the object or <code>null</code> if the property hasn't been |
||
528 | * requested or hasn't been provided by the repository |
||
529 | */ |
||
530 | public function getLastModificationDate() |
||
534 | |||
535 | /** |
||
536 | * Returns the user who modified this CMIS object (CMIS property cmis:lastModifiedBy). |
||
537 | * |
||
538 | * @return string|null the last modifier of the object or <code>null</code> if the property hasn't |
||
539 | * been requested or hasn't been provided by the repository |
||
540 | */ |
||
541 | public function getLastModifiedBy() |
||
545 | |||
546 | /** |
||
547 | * Returns the name of this CMIS object (CMIS property cmis:name). |
||
548 | * |
||
549 | * @return string|null the name of the object or <code>null</code> if the property hasn't been requested |
||
550 | * or hasn't been provided by the repository |
||
551 | */ |
||
552 | public function getName() |
||
556 | |||
557 | /** |
||
558 | * Returns the description of this CMIS object (CMIS property cmis:description). |
||
559 | * |
||
560 | * @return string|null the description of the object or <code>null</code> if the property hasn't been requested, |
||
561 | * hasn't been provided by the repository, or the property value isn't set |
||
562 | */ |
||
563 | public function getDescription() |
||
567 | |||
568 | /** |
||
569 | * Returns a list of all available CMIS properties. |
||
570 | * |
||
571 | * @return PropertyInterface[] all available CMIS properties |
||
572 | */ |
||
573 | public function getProperties() |
||
577 | |||
578 | /** |
||
579 | * Returns a property. |
||
580 | * |
||
581 | * @param string $id the ID of the property |
||
582 | * @return PropertyInterface|null the property or <code>null</code> if the property hasn't been requested or |
||
583 | * hasn't been provided by the repository |
||
584 | */ |
||
585 | public function getProperty($id) |
||
593 | |||
594 | /** |
||
595 | * Returns the value of a property. |
||
596 | * |
||
597 | * @param string $id the ID of the property |
||
598 | * @return mixed the property value or <code>null</code> if the property hasn't been requested, |
||
599 | * hasn't been provided by the repository, or the property value isn't set |
||
600 | */ |
||
601 | public function getPropertyValue($id) |
||
610 | |||
611 | /** |
||
612 | * Returns the secondary types of this CMIS object (object types identified by cmis:secondaryObjectTypeIds). |
||
613 | * |
||
614 | * @return SecondaryTypeInterface[]|null the secondary types of the object or <code>null</code> if the property |
||
615 | * cmis:secondaryObjectTypeIds hasn't been requested or hasn't been provided by the repository |
||
616 | */ |
||
617 | public function getSecondaryTypes() |
||
621 | |||
622 | /** |
||
623 | * Returns a list of primary and secondary object types that define the given property. |
||
624 | * |
||
625 | * @param string $id the ID of the property |
||
626 | * @return ObjectTypeInterface[]|null a list of object types that define the given property or <code>null</code> |
||
627 | * if the property could not be found in the object types that are attached to this object |
||
628 | */ |
||
629 | public function findObjectType($id) |
||
648 | |||
649 | /** |
||
650 | * Returns the allowable actions if they have been fetched for this object. |
||
651 | * |
||
652 | * @return AllowableActionsInterface|null |
||
653 | */ |
||
654 | public function getAllowableActions() |
||
658 | |||
659 | /** |
||
660 | * Checks if the given action is an allowed action for the object |
||
661 | * |
||
662 | * @param Action $action |
||
663 | * @return boolean |
||
664 | */ |
||
665 | public function hasAllowableAction(Action $action) |
||
674 | |||
675 | /** |
||
676 | * Returns the renditions if they have been fetched for this object. |
||
677 | * |
||
678 | * @return RenditionInterface[] |
||
679 | */ |
||
680 | public function getRenditions() |
||
684 | |||
685 | /** |
||
686 | * Adds and removes ACEs to the object and refreshes this object afterwards. |
||
687 | * |
||
688 | * @param AceInterface[] $addAces |
||
689 | * @param AceInterface[] $removeAces |
||
690 | * @param AclPropagation $aclPropagation |
||
691 | * @return AclInterface the new ACL of this object |
||
692 | */ |
||
693 | public function applyAcl(array $addAces, array $removeAces, AclPropagation $aclPropagation) |
||
701 | |||
702 | /** |
||
703 | * Adds ACEs to the object and refreshes this object afterwards. |
||
704 | * |
||
705 | * @param AceInterface[] $addAces |
||
706 | * @param AclPropagation $aclPropagation |
||
707 | * @return AclInterface the new ACL of this object |
||
708 | */ |
||
709 | public function addAcl(array $addAces, AclPropagation $aclPropagation) |
||
713 | |||
714 | /** |
||
715 | * Removes ACEs to the object and refreshes this object afterwards. |
||
716 | * |
||
717 | * @param array $removeAces |
||
718 | * @param AclPropagation $aclPropagation |
||
719 | * @return AclInterface the new ACL of this object |
||
720 | */ |
||
721 | public function removeAcl(array $removeAces, AclPropagation $aclPropagation) |
||
725 | |||
726 | /** |
||
727 | * Removes the direct ACE of this object, sets the provided ACEs to the object and refreshes this object afterwards. |
||
728 | * |
||
729 | * @param AceInterface[] $aces |
||
730 | * @return AclInterface |
||
731 | */ |
||
732 | public function setAcl(array $aces) |
||
740 | |||
741 | /** |
||
742 | * Returns the ACL if it has been fetched for this object. |
||
743 | * |
||
744 | * @return AclInterface|null |
||
745 | */ |
||
746 | public function getAcl() |
||
750 | |||
751 | /** |
||
752 | * Returns all permissions for the given principal from the ACL. |
||
753 | * |
||
754 | * @param string $principalId the principal ID |
||
755 | * @return string[] the set of permissions for this user, or an empty set if principal is not in the ACL |
||
756 | * @throws IllegalStateException if the ACL hasn't been fetched or provided by the repository |
||
757 | */ |
||
758 | public function getPermissionsForPrincipal($principalId) |
||
780 | |||
781 | /** |
||
782 | * Applies the provided policies and refreshes this object afterwards. |
||
783 | * |
||
784 | * @param ObjectIdInterface[] $policyIds |
||
785 | */ |
||
786 | public function applyPolicies(array $policyIds) |
||
790 | |||
791 | /** |
||
792 | * Returns the applied policies if they have been fetched for this object. |
||
793 | * |
||
794 | * @return PolicyInterface[] |
||
795 | */ |
||
796 | public function getPolicies() |
||
800 | |||
801 | /** |
||
802 | * Removes the provided policies and refreshes this object afterwards. |
||
803 | * |
||
804 | * @param ObjectIdInterface[] $policyIds |
||
805 | */ |
||
806 | public function removePolicy(array $policyIds) |
||
810 | |||
811 | /** |
||
812 | * Returns the relationships if they have been fetched for this object. |
||
813 | * |
||
814 | * @return RelationshipInterface[] |
||
815 | */ |
||
816 | public function getRelationships() |
||
820 | |||
821 | /** |
||
822 | * Returns the extensions for the given level. |
||
823 | * |
||
824 | * @param ExtensionLevel $level the level |
||
825 | * @return array[]|null A list of <code>CmisExtensionElementInterface</code> at the requested level or |
||
826 | * <code>null</code> if there are no extensions for the requested level |
||
827 | * @see CmisExtensionElementInterface |
||
828 | */ |
||
829 | public function getExtensions(ExtensionLevel $level) |
||
837 | |||
838 | /** |
||
839 | * Returns the timestamp of the last refresh. |
||
840 | * |
||
841 | * @return integer returns the Java-style milliseconds UNIX timestamp of last refresh |
||
842 | */ |
||
843 | public function getRefreshTimestamp() |
||
847 | |||
848 | /** |
||
849 | * Reloads this object from the repository. |
||
850 | * |
||
851 | * @throws CmisObjectNotFoundException - if the object doesn't exist anymore in the repository |
||
852 | */ |
||
853 | public function refresh() |
||
876 | |||
877 | /** |
||
878 | * Reloads the data from the repository if the last refresh did not occur within durationInMillis. |
||
879 | * |
||
880 | * @param integer $durationInMillis |
||
881 | * @throws CmisObjectNotFoundException - if the object doesn't exist anymore in the repository |
||
882 | */ |
||
883 | public function refreshIfOld($durationInMillis = 0) |
||
889 | } |
||
890 |