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 |
||
46 | abstract class AbstractCmisObject implements CmisObjectInterface |
||
47 | { |
||
48 | /** |
||
49 | * @var SessionInterface |
||
50 | */ |
||
51 | protected $session; |
||
52 | |||
53 | /** |
||
54 | * @var ObjectTypeInterface |
||
55 | */ |
||
56 | protected $objectType; |
||
57 | |||
58 | /** |
||
59 | * @var null|SecondaryTypeInterface[] |
||
60 | */ |
||
61 | protected $secondaryTypes; |
||
62 | |||
63 | /** |
||
64 | * @var PropertyInterface[] |
||
65 | */ |
||
66 | protected $properties = []; |
||
67 | |||
68 | /** |
||
69 | * @var AllowableActionsInterface|null |
||
70 | */ |
||
71 | protected $allowableActions; |
||
72 | |||
73 | /** |
||
74 | * @var RenditionInterface[] |
||
75 | */ |
||
76 | protected $renditions = []; |
||
77 | |||
78 | /** |
||
79 | * @var AclInterface|null |
||
80 | */ |
||
81 | protected $acl; |
||
82 | |||
83 | /** |
||
84 | * @var PolicyInterface[] |
||
85 | */ |
||
86 | protected $policies = []; |
||
87 | |||
88 | /** |
||
89 | * @var RelationshipInterface[] |
||
90 | */ |
||
91 | protected $relationships = []; |
||
92 | |||
93 | /** |
||
94 | * A list that contains a list of <code>CmisExtensionElementInterface</code> identified by |
||
95 | * <code>ExtensionLevel</code>. The key is the string representation of <code>ExtensionLevel</code> and the value |
||
96 | * the array of <code>CmisExtensionElementInterface</code> |
||
97 | * |
||
98 | * @see ExtensionLevel |
||
99 | * @see CmisExtensionElementInterface |
||
100 | * @var array[] |
||
101 | */ |
||
102 | protected $extensions = []; |
||
103 | |||
104 | /** |
||
105 | * @var OperationContextInterface |
||
106 | */ |
||
107 | protected $creationContext; |
||
108 | |||
109 | /** |
||
110 | * @var integer |
||
111 | */ |
||
112 | protected $refreshTimestamp = 0; |
||
113 | |||
114 | /** |
||
115 | * Initialize the CMIS Object |
||
116 | * |
||
117 | * @param SessionInterface $session |
||
118 | * @param ObjectTypeInterface $objectType |
||
119 | * @param OperationContextInterface $context |
||
120 | * @param ObjectDataInterface|null $objectData |
||
121 | */ |
||
122 | public function initialize( |
||
148 | |||
149 | /** |
||
150 | * Handle initialization for objectData |
||
151 | * |
||
152 | * @param ObjectDataInterface $objectData |
||
153 | */ |
||
154 | private function initializeObjectData(ObjectDataInterface $objectData) |
||
199 | |||
200 | /** |
||
201 | * Handle initialization of properties from the object data |
||
202 | * |
||
203 | * @param PropertiesInterface $properties |
||
204 | */ |
||
205 | private function initializeObjectDataProperties(PropertiesInterface $properties) |
||
229 | |||
230 | /** |
||
231 | * Handle initialization of policies from the object data |
||
232 | * |
||
233 | * @param PolicyIdListInterface $policies |
||
234 | */ |
||
235 | private function initializeObjectDataPolicies(PolicyIdListInterface $policies) |
||
246 | |||
247 | |||
248 | /** |
||
249 | * Returns a list of missing property keys |
||
250 | * |
||
251 | 4 | * @param PropertyDefinitionInterface[]|null $properties |
|
252 | * @return array |
||
253 | 4 | */ |
|
254 | protected function getMissingBaseProperties(array $properties = null) |
||
272 | |||
273 | /** |
||
274 | * Returns the session object |
||
275 | * |
||
276 | * @return SessionInterface |
||
277 | */ |
||
278 | protected function getSession() |
||
282 | |||
283 | /** |
||
284 | * Returns the repository id |
||
285 | * |
||
286 | * @return string |
||
287 | */ |
||
288 | protected function getRepositoryId() |
||
292 | |||
293 | /** |
||
294 | * Returns the object type |
||
295 | * |
||
296 | * @return ObjectTypeInterface |
||
297 | */ |
||
298 | protected function getObjectType() |
||
302 | |||
303 | /** |
||
304 | * Returns the object factory. |
||
305 | * |
||
306 | * @return ObjectFactoryInterface |
||
307 | */ |
||
308 | protected function getObjectFactory() |
||
312 | |||
313 | /** |
||
314 | * Get the binding object |
||
315 | * |
||
316 | * @return CmisBindingInterface |
||
317 | */ |
||
318 | protected function getBinding() |
||
322 | |||
323 | /** |
||
324 | * Returns the OperationContext that was used to create this object. |
||
325 | * |
||
326 | * @return OperationContextInterface |
||
327 | */ |
||
328 | protected function getCreationContext() |
||
332 | |||
333 | /** |
||
334 | * Returns the query name of a property. |
||
335 | * |
||
336 | * @param string $propertyId |
||
337 | * @return null|string |
||
338 | */ |
||
339 | protected function getPropertyQueryName($propertyId) |
||
348 | |||
349 | /** |
||
350 | * Delete this object |
||
351 | * |
||
352 | * @param boolean $allVersions indicates if all versions of the object should be deleted |
||
353 | */ |
||
354 | public function delete($allVersions = true) |
||
358 | |||
359 | /** |
||
360 | * Updates the provided properties. If the repository created a new object, for example a new version, |
||
361 | * the object ID of the new object is returned. Otherwise the object ID of the current object is returned. |
||
362 | * |
||
363 | * @param mixed[] $properties the properties to update |
||
364 | * @param boolean $refresh <code>true</code> if this object should be refresh after the update, |
||
365 | * <code>false</code> if not |
||
366 | * @return CmisObjectInterface|null the object ID of the updated object - can return <code>null</code> in case |
||
367 | * of a repository failure |
||
368 | */ |
||
369 | public function updateProperties(array $properties, $refresh = true) |
||
413 | |||
414 | /** |
||
415 | * Renames this object (changes the value of cmis:name). |
||
416 | * If the repository created a new object, for example a new version, the object id of the |
||
417 | * new object is returned. Otherwise the object id of the current object is returned. |
||
418 | * |
||
419 | * @param string $newName the new name, not <code>null</code> or empty |
||
420 | * @param boolean $refresh <code>true</code> if this object should be refresh after the update, |
||
421 | * <code>false</code> if not |
||
422 | * @return CmisObjectInterface|null the object ID of the updated object - can return <code>null</code> in case of |
||
423 | * a repository failure |
||
424 | */ |
||
425 | public function rename($newName, $refresh = true) |
||
439 | |||
440 | /** |
||
441 | * Returns the type of this CMIS object (object type identified by <code>cmis:objectTypeId</code>). |
||
442 | * |
||
443 | * @return ObjectTypeInterface the type of the object or <code>null</code> if the property |
||
444 | * <code>cmis:objectTypeId</code> hasn't been requested or hasn't been provided by the repository |
||
445 | */ |
||
446 | public function getType() |
||
450 | |||
451 | /** |
||
452 | * Returns the base type of this CMIS object (object type identified by cmis:baseTypeId). |
||
453 | * |
||
454 | * @return ObjectTypeInterface the base type of the object or <code>null</code> if the property cmis:baseTypeId |
||
455 | * hasn't been requested or hasn't been provided by the repository |
||
456 | */ |
||
457 | public function getBaseType() |
||
466 | |||
467 | /** |
||
468 | * Returns the base type of this CMIS object (object type identified by cmis:baseTypeId). |
||
469 | * |
||
470 | * @return BaseTypeId|null the base type of the object or <code>null</code> if the property |
||
471 | * cmis:baseTypeId hasn't been requested or hasn't been provided by the repository |
||
472 | */ |
||
473 | public function getBaseTypeId() |
||
482 | |||
483 | /** |
||
484 | * Returns the change token (CMIS property cmis:changeToken). |
||
485 | * |
||
486 | * @return string the change token of the object or <code>null</code> if the property hasn't been requested or |
||
487 | * hasn't been provided or isn't supported by the repository |
||
488 | */ |
||
489 | public function getChangeToken() |
||
493 | |||
494 | /** |
||
495 | * Returns the user who created this CMIS object (CMIS property cmis:createdBy). |
||
496 | * |
||
497 | * @return string the creator of the object or <code>null</code> if the property hasn't been requested or hasn't |
||
498 | * been provided by the repository |
||
499 | */ |
||
500 | public function getCreatedBy() |
||
504 | |||
505 | /** |
||
506 | * Returns the timestamp when this CMIS object has been created (CMIS property cmis:creationDate). |
||
507 | * |
||
508 | * @return \DateTime|null the creation time of the object or <code>null</code> if the property hasn't been |
||
509 | * requested or hasn't been provided by the repository |
||
510 | */ |
||
511 | public function getCreationDate() |
||
515 | |||
516 | /** |
||
517 | * Returns the object ID |
||
518 | * |
||
519 | * @return string |
||
520 | */ |
||
521 | public function getId() |
||
525 | |||
526 | /** |
||
527 | * Returns the timestamp when this CMIS object has been modified (CMIS property cmis:lastModificationDate). |
||
528 | * |
||
529 | * @return \DateTime|null the last modification date of the object or <code>null</code> if the property hasn't been |
||
530 | * requested or hasn't been provided by the repository |
||
531 | */ |
||
532 | public function getLastModificationDate() |
||
536 | |||
537 | /** |
||
538 | * Returns the user who modified this CMIS object (CMIS property cmis:lastModifiedBy). |
||
539 | * |
||
540 | * @return string|null the last modifier of the object or <code>null</code> if the property hasn't |
||
541 | * been requested or hasn't been provided by the repository |
||
542 | */ |
||
543 | public function getLastModifiedBy() |
||
547 | |||
548 | /** |
||
549 | * Returns the name of this CMIS object (CMIS property cmis:name). |
||
550 | * |
||
551 | * @return string|null the name of the object or <code>null</code> if the property hasn't been requested |
||
552 | * or hasn't been provided by the repository |
||
553 | */ |
||
554 | public function getName() |
||
558 | |||
559 | /** |
||
560 | * Returns the description of this CMIS object (CMIS property cmis:description). |
||
561 | * |
||
562 | * @return string|null the description of the object or <code>null</code> if the property hasn't been requested, |
||
563 | * hasn't been provided by the repository, or the property value isn't set |
||
564 | */ |
||
565 | public function getDescription() |
||
569 | |||
570 | /** |
||
571 | * Returns a list of all available CMIS properties. |
||
572 | * |
||
573 | * @return PropertyInterface[] all available CMIS properties |
||
574 | */ |
||
575 | public function getProperties() |
||
579 | |||
580 | /** |
||
581 | * Returns a property. |
||
582 | * |
||
583 | * @param string $id the ID of the property |
||
584 | * @return PropertyInterface|null the property or <code>null</code> if the property hasn't been requested or |
||
585 | * hasn't been provided by the repository |
||
586 | */ |
||
587 | public function getProperty($id) |
||
595 | |||
596 | /** |
||
597 | * Returns the value of a property. |
||
598 | * |
||
599 | * @param string $id the ID of the property |
||
600 | * @return mixed the property value or <code>null</code> if the property hasn't been requested, |
||
601 | * hasn't been provided by the repository, or the property value isn't set |
||
602 | */ |
||
603 | public function getPropertyValue($id) |
||
612 | |||
613 | /** |
||
614 | * Returns the secondary types of this CMIS object (object types identified by cmis:secondaryObjectTypeIds). |
||
615 | * |
||
616 | * @return SecondaryTypeInterface[]|null the secondary types of the object or <code>null</code> if the property |
||
617 | * cmis:secondaryObjectTypeIds hasn't been requested or hasn't been provided by the repository |
||
618 | */ |
||
619 | public function getSecondaryTypes() |
||
623 | |||
624 | /** |
||
625 | * Returns a list of primary and secondary object types that define the given property. |
||
626 | * |
||
627 | * @param string $id the ID of the property |
||
628 | * @return ObjectTypeInterface[]|null a list of object types that define the given property or <code>null</code> |
||
629 | * if the property could not be found in the object types that are attached to this object |
||
630 | */ |
||
631 | public function findObjectType($id) |
||
650 | |||
651 | /** |
||
652 | * Returns the allowable actions if they have been fetched for this object. |
||
653 | * |
||
654 | * @return AllowableActionsInterface|null |
||
655 | */ |
||
656 | public function getAllowableActions() |
||
660 | |||
661 | /** |
||
662 | * Checks if the given action is an allowed action for the object |
||
663 | * |
||
664 | * @param Action $action |
||
665 | * @return boolean |
||
666 | */ |
||
667 | public function hasAllowableAction(Action $action) |
||
676 | |||
677 | /** |
||
678 | * Returns the renditions if they have been fetched for this object. |
||
679 | * |
||
680 | * @return RenditionInterface[] |
||
681 | */ |
||
682 | public function getRenditions() |
||
686 | |||
687 | /** |
||
688 | * Adds and removes ACEs to the object and refreshes this object afterwards. |
||
689 | * |
||
690 | * @param AceInterface[] $addAces |
||
691 | * @param AceInterface[] $removeAces |
||
692 | * @param AclPropagation $aclPropagation |
||
693 | * @return AclInterface the new ACL of this object |
||
694 | */ |
||
695 | public function applyAcl(array $addAces, array $removeAces, AclPropagation $aclPropagation) |
||
703 | |||
704 | /** |
||
705 | * Adds ACEs to the object and refreshes this object afterwards. |
||
706 | * |
||
707 | * @param AceInterface[] $addAces |
||
708 | * @param AclPropagation $aclPropagation |
||
709 | * @return AclInterface the new ACL of this object |
||
710 | */ |
||
711 | public function addAcl(array $addAces, AclPropagation $aclPropagation) |
||
715 | |||
716 | /** |
||
717 | * Removes ACEs to the object and refreshes this object afterwards. |
||
718 | * |
||
719 | * @param array $removeAces |
||
720 | * @param AclPropagation $aclPropagation |
||
721 | * @return AclInterface the new ACL of this object |
||
722 | */ |
||
723 | public function removeAcl(array $removeAces, AclPropagation $aclPropagation) |
||
727 | |||
728 | /** |
||
729 | * Removes the direct ACE of this object, sets the provided ACEs to the object and refreshes this object afterwards. |
||
730 | * |
||
731 | * @param AceInterface[] $aces |
||
732 | * @return AclInterface |
||
733 | */ |
||
734 | public function setAcl(array $aces) |
||
742 | |||
743 | /** |
||
744 | * Returns the ACL if it has been fetched for this object. |
||
745 | * |
||
746 | * @return AclInterface|null |
||
747 | */ |
||
748 | public function getAcl() |
||
752 | |||
753 | /** |
||
754 | * Returns all permissions for the given principal from the ACL. |
||
755 | * |
||
756 | * @param string $principalId the principal ID |
||
757 | * @return string[] the set of permissions for this user, or an empty set if principal is not in the ACL |
||
758 | * @throws IllegalStateException if the ACL hasn't been fetched or provided by the repository |
||
759 | */ |
||
760 | public function getPermissionsForPrincipal($principalId) |
||
782 | |||
783 | /** |
||
784 | * Applies the provided policies and refreshes this object afterwards. |
||
785 | * |
||
786 | * @param ObjectIdInterface[] $policyIds |
||
787 | */ |
||
788 | public function applyPolicies(array $policyIds) |
||
792 | |||
793 | /** |
||
794 | * Returns the applied policies if they have been fetched for this object. |
||
795 | * |
||
796 | * @return PolicyInterface[] |
||
797 | */ |
||
798 | public function getPolicies() |
||
802 | |||
803 | /** |
||
804 | * Removes the provided policies and refreshes this object afterwards. |
||
805 | * |
||
806 | * @param ObjectIdInterface[] $policyIds |
||
807 | */ |
||
808 | public function removePolicy(array $policyIds) |
||
812 | |||
813 | /** |
||
814 | * Returns the relationships if they have been fetched for this object. |
||
815 | * |
||
816 | * @return RelationshipInterface[] |
||
817 | */ |
||
818 | public function getRelationships() |
||
822 | |||
823 | /** |
||
824 | * Returns the extensions for the given level. |
||
825 | * |
||
826 | * @param ExtensionLevel $level the level |
||
827 | * @return array[]|null A list of <code>CmisExtensionElementInterface</code> at the requested level or |
||
828 | * <code>null</code> if there are no extensions for the requested level |
||
829 | * @see CmisExtensionElementInterface |
||
830 | */ |
||
831 | public function getExtensions(ExtensionLevel $level) |
||
839 | |||
840 | /** |
||
841 | * Returns the timestamp of the last refresh. |
||
842 | * |
||
843 | * @return integer returns the Java-style milliseconds UNIX timestamp of last refresh |
||
844 | */ |
||
845 | public function getRefreshTimestamp() |
||
849 | |||
850 | /** |
||
851 | * Reloads this object from the repository. |
||
852 | * |
||
853 | * @throws CmisObjectNotFoundException - if the object doesn't exist anymore in the repository |
||
854 | */ |
||
855 | public function refresh() |
||
878 | |||
879 | /** |
||
880 | * Reloads the data from the repository if the last refresh did not occur within durationInMillis. |
||
881 | * |
||
882 | * @param integer $durationInMillis |
||
883 | * @throws CmisObjectNotFoundException - if the object doesn't exist anymore in the repository |
||
884 | */ |
||
885 | public function refreshIfOld($durationInMillis = 0) |
||
891 | } |
||
892 |