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 ObjectService 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 ObjectService, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
36 | class ObjectService extends AbstractBrowserBindingService implements ObjectServiceInterface |
||
37 | { |
||
38 | /** |
||
39 | * L1 cache for objects. Fills with two levels: |
||
40 | * |
||
41 | * - First level key is the object ID, path or other singular identifier of object(s) |
||
42 | * - Second level key is a hash of context arguments used to retrieve the object(s) |
||
43 | * |
||
44 | * @var array |
||
45 | */ |
||
46 | protected $objectCache = []; |
||
47 | |||
48 | /** |
||
49 | * Appends the content stream to the content of the document. |
||
50 | * |
||
51 | * The stream in contentStream is consumed but not closed by this method. |
||
52 | * |
||
53 | * @param string $repositoryId the identifier for the repository |
||
54 | * @param string $objectId The identifier for the object. The repository might return a different/new object id |
||
55 | * @param StreamInterface $contentStream The content stream to append |
||
56 | * @param boolean $isLastChunk Indicates if this content stream is the last chunk |
||
57 | * @param string|null $changeToken The last change token of this object that the client received. |
||
58 | * The repository might return a new change token (default is <code>null</code>) |
||
59 | * @param ExtensionDataInterface|null $extension |
||
60 | */ |
||
61 | public function appendContentStream( |
||
71 | |||
72 | /** |
||
73 | * Updates properties and secondary types of one or more objects. |
||
74 | * |
||
75 | * @param string $repositoryId the identifier for the repository |
||
76 | * @param BulkUpdateObjectIdAndChangeTokenInterface[] $objectIdsAndChangeTokens |
||
77 | * @param PropertiesInterface $properties |
||
78 | * @param string[] $addSecondaryTypeIds the secondary types to apply |
||
79 | * @param string[] $removeSecondaryTypeIds the secondary types to remove |
||
80 | * @param ExtensionDataInterface|null $extension |
||
81 | * @return BulkUpdateObjectIdAndChangeTokenInterface[] |
||
82 | */ |
||
83 | public function bulkUpdateProperties( |
||
93 | |||
94 | /** |
||
95 | * @param string $action |
||
96 | * @param PropertiesInterface $properties |
||
97 | * @param string[] $policies |
||
98 | * @param AclInterface $addAces |
||
99 | * @param AclInterface $removeAces |
||
100 | * @param ExtensionDataInterface $extension |
||
101 | * @return array |
||
102 | */ |
||
103 | protected function createQueryArray( |
||
135 | |||
136 | /** |
||
137 | * Creates a document object of the specified type (given by the cmis:objectTypeId property) |
||
138 | * in the (optionally) specified location. |
||
139 | * |
||
140 | * @param string $repositoryId the identifier for the repository |
||
141 | * @param PropertiesInterface $properties the property values that must be applied to the newly |
||
142 | * created document object |
||
143 | * @param string|null $folderId if specified, the identifier for the folder that must be the parent |
||
144 | * folder for the newly created document object |
||
145 | * @param StreamInterface|null $contentStream the content stream that must be stored for the newly |
||
146 | * created document object |
||
147 | * @param VersioningState|null $versioningState specifies what the versioning state of the newly created object |
||
148 | * must be (default is <code>VersioningState::MAJOR</code>) |
||
149 | * @param string[] $policies a list of policy IDs that must be applied to the newly created document object |
||
150 | * @param AclInterface|null $addAces a list of ACEs that must be added to the newly created document object, |
||
151 | * either using the ACL from folderId if specified, or being applied if no folderId is specified |
||
152 | * @param AclInterface|null $removeAces a list of ACEs that must be removed from the newly created document object, |
||
153 | * either using the ACL from folderId if specified, or being ignored if no folderId is specified |
||
154 | * @param ExtensionDataInterface|null $extension |
||
155 | * @return string|null Returns the new object id or <code>null</code> if the repository sent an empty |
||
156 | * result (which should not happen) |
||
157 | */ |
||
158 | public function createDocument( |
||
204 | 2 | ||
205 | 2 | /** |
|
206 | 3 | * Creates a document object as a copy of the given source document in the (optionally) specified location. |
|
207 | * |
||
208 | * @param string $repositoryId the identifier for the repository |
||
209 | * @param string $sourceId the identifier for the source document |
||
210 | * @param PropertiesInterface $properties the property values that must be applied to the newly |
||
211 | * created document object |
||
212 | * @param string|null $folderId if specified, the identifier for the folder that must be the parent folder for the |
||
213 | * newly created document object |
||
214 | * @param VersioningState|null $versioningState specifies what the versioning state of the newly created object |
||
215 | * must be (default is <code>VersioningState::MAJOR</code>) |
||
216 | * @param string[] $policies a list of policy IDs that must be applied to the newly created document object |
||
217 | * @param AclInterface|null $addAces a list of ACEs that must be added to the newly created document object, |
||
218 | * either using the ACL from folderId if specified, or being applied if no folderId is specified |
||
219 | * @param AclInterface|null $removeAces a list of ACEs that must be removed from the newly created document object, |
||
220 | * either using the ACL from folderId if specified, or being ignored if no folderId is specified |
||
221 | * @param ExtensionDataInterface|null $extension |
||
222 | * @return string|null Returns the new object id or <code>null</code> if the repository sent an empty |
||
223 | * result (which should not happen) |
||
224 | */ |
||
225 | public function createDocumentFromSource( |
||
259 | 1 | ||
260 | 2 | /** |
|
261 | * Creates a folder object of the specified type (given by the cmis:objectTypeId property) in |
||
262 | 2 | * the specified location. |
|
263 | * |
||
264 | 2 | * @param string $repositoryId the identifier for the repository |
|
265 | * @param PropertiesInterface $properties the property values that must be applied to the newly |
||
266 | * created document object |
||
267 | * @param string $folderId if specified, the identifier for the folder that must be the parent folder for the |
||
268 | * newly created document object |
||
269 | * @param string[] $policies a list of policy IDs that must be applied to the newly created document object |
||
270 | * @param AclInterface|null $addAces a list of ACEs that must be added to the newly created document object, |
||
271 | * either using the ACL from folderId if specified, or being applied if no folderId is specified |
||
272 | * @param AclInterface|null $removeAces a list of ACEs that must be removed from the newly created document object, |
||
273 | * either using the ACL from folderId if specified, or being ignored if no folderId is specified |
||
274 | * @param ExtensionDataInterface|null $extension |
||
275 | * @return string|null Returns the new object id or <code>null</code> if the repository sent an empty |
||
276 | * result (which should not happen) |
||
277 | */ |
||
278 | View Code Duplication | public function createFolder( |
|
301 | |||
302 | 2 | /** |
|
303 | * Creates an item object of the specified type (given by the cmis:objectTypeId property). |
||
304 | 2 | * |
|
305 | * @param string $repositoryId The identifier for the repository |
||
306 | 2 | * @param PropertiesInterface $properties The property values that must be applied to the newly |
|
307 | * created document object |
||
308 | 2 | * @param string|null $folderId If specified, the identifier for the folder that must be the parent folder for the |
|
309 | * newly created document object |
||
310 | * @param string[] $policies A list of policy IDs that must be applied to the newly created document object |
||
311 | * @param AclInterface|null $addAces A list of ACEs that must be added to the newly created document object, |
||
312 | * either using the ACL from folderId if specified, or being applied if no folderId is specified |
||
313 | * @param AclInterface|null $removeAces A list of ACEs that must be removed from the newly created document object, |
||
314 | * either using the ACL from folderId if specified, or being ignored if no folderId is specified |
||
315 | * @param ExtensionDataInterface|null $extension |
||
316 | * @return string|null Returns the new item id or <code>null</code> if the repository sent an empty |
||
317 | * result (which should not happen) |
||
318 | */ |
||
319 | public function createItem( |
||
347 | 2 | ||
348 | 2 | /** |
|
349 | * Creates a policy object of the specified type (given by the cmis:objectTypeId property). |
||
350 | 2 | * |
|
351 | * @param string $repositoryId The identifier for the repository |
||
352 | 2 | * @param PropertiesInterface $properties The property values that must be applied to the newly |
|
353 | * created document object |
||
354 | 2 | * @param string|null $folderId If specified, the identifier for the folder that must be the parent folder for the |
|
355 | * newly created document object |
||
356 | 2 | * @param string[] $policies A list of policy IDs that must be applied to the newly created document object |
|
357 | * @param AclInterface|null $addAces A list of ACEs that must be added to the newly created document object, |
||
358 | * either using the ACL from folderId if specified, or being applied if no folderId is specified |
||
359 | * @param AclInterface|null $removeAces A list of ACEs that must be removed from the newly created document object, |
||
360 | * either using the ACL from folderId if specified, or being ignored if no folderId is specified |
||
361 | * @param ExtensionDataInterface|null $extension |
||
362 | * @return string The id of the newly-created policy. |
||
363 | */ |
||
364 | public function createPolicy( |
||
375 | |||
376 | /** |
||
377 | * Creates a relationship object of the specified type (given by the cmis:objectTypeId property). |
||
378 | * |
||
379 | * @param string $repositoryId the identifier for the repository |
||
380 | * @param PropertiesInterface $properties the property values that must be applied to the newly |
||
381 | * created document object |
||
382 | * @param string[] $policies a list of policy IDs that must be applied to the newly created document object |
||
383 | * @param AclInterface|null $addAces a list of ACEs that must be added to the newly created document object, |
||
384 | * either using the ACL from folderId if specified, or being applied if no folderId is specified |
||
385 | * @param AclInterface|null $removeAces a list of ACEs that must be removed from the newly created document object, |
||
386 | * either using the ACL from folderId if specified, or being ignored if no folderId is specified |
||
387 | * @param ExtensionDataInterface|null $extension |
||
388 | * @return string|null Returns the new item id of the relationship object or <code>null</code> if the repository |
||
389 | * sent an empty result (which should not happen) |
||
390 | */ |
||
391 | View Code Duplication | public function createRelationship( |
|
414 | |||
415 | /** |
||
416 | * Deletes the content stream for the specified document object. |
||
417 | * |
||
418 | * @param string $repositoryId the identifier for the repository |
||
419 | * @param string $objectId the identifier for the object. The repository might return a different/new object id |
||
420 | * @param string|null $changeToken the last change token of this object that the client received. |
||
421 | * The repository might return a new change token (default is <code>null</code>) |
||
422 | * @param ExtensionDataInterface|null $extension |
||
423 | * @throws CmisInvalidArgumentException If $objectId is empty |
||
424 | */ |
||
425 | public function deleteContentStream( |
||
464 | 3 | ||
465 | /** |
||
466 | * Deletes the specified object. |
||
467 | 3 | * |
|
468 | 3 | * @param string $repositoryId the identifier for the repository |
|
469 | 3 | * @param string $objectId the identifier for the object |
|
470 | 3 | * @param boolean $allVersions If <code>true</code> then delete all versions of the document, otherwise delete only |
|
471 | 3 | * the document object specified (default is <code>true</code>) |
|
472 | 2 | * @param ExtensionDataInterface|null $extension |
|
473 | */ |
||
474 | 2 | View Code Duplication | public function deleteObject( |
489 | |||
490 | /** |
||
491 | * Deletes the specified folder object and all of its child- and descendant-objects. |
||
492 | * |
||
493 | * @param string $repositoryId the identifier for the repository |
||
494 | 2 | * @param string $folderId the identifier for the folder |
|
495 | 2 | * @param boolean $allVersions If <code>true</code> then delete all versions of the document, otherwise delete only |
|
496 | * the document object specified (default is <code>true</code>) |
||
497 | 2 | * @param UnfileObject|null $unfileObjects defines how the repository must process file-able child- or |
|
498 | 2 | * descendant-objects (default is <code>UnfileObject::DELETE</code>) |
|
499 | * @param boolean $continueOnFailure If <code>true</code>, then the repository should continue attempting to |
||
500 | 2 | * perform this operation even if deletion of a child- or descendant-object in the specified folder cannot |
|
501 | * be deleted |
||
502 | 2 | * @param ExtensionDataInterface|null $extension |
|
503 | 2 | * @return FailedToDeleteDataInterface Returns a list of object ids that could not be deleted |
|
504 | 2 | */ |
|
505 | public function deleteTree( |
||
529 | 4 | ||
530 | 4 | /** |
|
531 | * Gets the list of allowable actions for an object. |
||
532 | 4 | * |
|
533 | 4 | * @param string $repositoryId the identifier for the repository |
|
534 | 4 | * @param string $objectId the identifier for the object |
|
535 | 4 | * @param ExtensionDataInterface|null $extension |
|
536 | 4 | * @return AllowableActionsInterface |
|
537 | 4 | */ |
|
538 | public function getAllowableActions($repositoryId, $objectId, ExtensionDataInterface $extension = null) |
||
542 | |||
543 | 4 | /** |
|
544 | * Gets the content stream for the specified document object, or gets a rendition stream for |
||
545 | 4 | * a specified rendition of a document or folder object. |
|
546 | 4 | * |
|
547 | 4 | * @param string $repositoryId the identifier for the repository |
|
548 | * @param string $objectId the identifier for the object |
||
549 | 4 | * @param string|null $streamId The identifier for the rendition stream, when used to get a rendition stream. |
|
550 | 4 | * For documents, if not provided then this method returns the content stream. For folders, |
|
551 | * it MUST be provided. |
||
552 | * @param integer|null $offset |
||
553 | * @param integer|null $length |
||
554 | * @param ExtensionDataInterface|null $extension |
||
555 | * @return StreamInterface|null |
||
556 | * @throws CmisInvalidArgumentException If object id is empty |
||
557 | */ |
||
558 | public function getContentStream( |
||
590 | |||
591 | /** |
||
592 | * Gets the specified information for the object specified by id. |
||
593 | 3 | * |
|
594 | * @param string $repositoryId the identifier for the repository |
||
595 | 3 | * @param string $objectId the identifier for the object |
|
596 | 1 | * @param string|null $filter a comma-separated list of query names that defines which properties must be |
|
597 | 1 | * returned by the repository (default is repository specific) |
|
598 | * @param boolean $includeAllowableActions if <code>true</code>, then the repository must return the allowable |
||
599 | * actions for the object (default is <code>false</code>) |
||
600 | 3 | * @param IncludeRelationships|null $includeRelationships indicates what relationships in which the objects |
|
601 | * participate must be returned (default is <code>IncludeRelationships::NONE</code>) |
||
602 | 3 | * @param string $renditionFilter indicates what set of renditions the repository must return whose kind |
|
603 | 3 | * matches this filter (default is "cmis:none") |
|
604 | * @param boolean $includePolicyIds if <code>true</code>, then the repository must return the policy ids for |
||
605 | * the object (default is <code>false</code>) |
||
606 | * @param boolean $includeAcl if <code>true</code>, then the repository must return the ACL for the object |
||
607 | 3 | * (default is <code>false</code>) |
|
608 | 1 | * @param ExtensionDataInterface|null $extension |
|
609 | 1 | * @return ObjectDataInterface|null Returns object of type ObjectDataInterface or <code>null</code> |
|
610 | * if the repository response was empty |
||
611 | 3 | */ |
|
612 | View Code Duplication | public function getObject( |
|
667 | 3 | ||
668 | 3 | /** |
|
669 | 3 | * Gets the specified information for the object specified by path. |
|
670 | 3 | * |
|
671 | 3 | * @param string $repositoryId the identifier for the repository |
|
672 | 3 | * @param string $path the path to the object |
|
673 | 3 | * @param string|null $filter a comma-separated list of query names that defines which properties must be |
|
674 | * returned by the repository (default is repository specific) |
||
675 | 3 | * @param boolean $includeAllowableActions if <code>true</code>, then the repository must return the allowable |
|
676 | 2 | * actions for the object (default is <code>false</code>) |
|
677 | 2 | * @param IncludeRelationships|null $includeRelationships indicates what relationships in which the objects |
|
678 | * participate must be returned (default is <code>IncludeRelationships::NONE</code>) |
||
679 | 3 | * @param string $renditionFilter indicates what set of renditions the repository must return whose kind |
|
680 | 2 | * matches this filter (default is "cmis:none") |
|
681 | 2 | * @param boolean $includePolicyIds if <code>true</code>, then the repository must return the policy ids for |
|
682 | * the object (default is <code>false</code>) |
||
683 | 3 | * @param boolean $includeAcl if <code>true</code>, then the repository must return the ACL for the object |
|
684 | * (default is <code>false</code>) |
||
685 | 3 | * @param ExtensionDataInterface|null $extension |
|
686 | 3 | * @return ObjectDataInterface|null Returns object of type <code>ObjectDataInterface</code> or <code>null</code> |
|
687 | 3 | * if the repository response was empty |
|
688 | 3 | */ |
|
689 | View Code Duplication | public function getObjectByPath( |
|
745 | 3 | ||
746 | 3 | /** |
|
747 | 3 | * Gets the list of properties for an object. |
|
748 | 3 | * |
|
749 | 3 | * @param string $repositoryId the identifier for the repository |
|
750 | 3 | * @param string $objectId the identifier for the object |
|
751 | 3 | * @param string|null $filter a comma-separated list of query names that defines which properties must be |
|
752 | * returned by the repository (default is repository specific) |
||
753 | 3 | * @param ExtensionDataInterface|null $extension |
|
754 | 2 | * @return PropertiesInterface |
|
755 | 2 | */ |
|
756 | public function getProperties( |
||
801 | |||
802 | 2 | /** |
|
803 | 2 | * Gets the list of associated renditions for the specified object. |
|
804 | 2 | * Only rendition attributes are returned, not rendition stream. |
|
805 | 2 | * |
|
806 | * @param string $repositoryId the identifier for the repository |
||
807 | 2 | * @param string $objectId the identifier for the object |
|
808 | 1 | * @param string $renditionFilter indicates what set of renditions the repository must return whose kind |
|
809 | 1 | * matches this filter (default is "cmis:none") |
|
810 | * @param integer|null $maxItems the maximum number of items to return in a response |
||
811 | 2 | * (default is repository specific) |
|
812 | * @param integer $skipCount number of potential results that the repository MUST skip/page over before |
||
813 | 2 | * returning any results (default is 0) |
|
814 | * @param ExtensionDataInterface|null $extension |
||
815 | * @return RenditionDataInterface[] |
||
816 | 2 | * @throws CmisInvalidArgumentException If object id is empty or skip count not of type integer |
|
817 | */ |
||
818 | public function getRenditions( |
||
850 | |||
851 | /** |
||
852 | * Moves the specified file-able object from one folder to another. |
||
853 | 2 | * |
|
854 | * @param string $repositoryId the identifier for the repository |
||
855 | * @param string $objectId the identifier for the object. The repository might return a different/new object id |
||
856 | * @param string $targetFolderId the identifier for the target folder |
||
857 | 2 | * @param string $sourceFolderId the identifier for the source folder |
|
858 | 2 | * @param ExtensionDataInterface|null $extension |
|
859 | * @return ObjectDataInterface|null Returns object of type ObjectDataInterface or <code>null</code> |
||
860 | 2 | * if the repository response was empty |
|
861 | 2 | */ |
|
862 | public function moveObject( |
||
888 | |||
889 | /** |
||
890 | * Sets the content stream for the specified document object. |
||
891 | * |
||
892 | 1 | * @param string $repositoryId The identifier for the repository |
|
893 | * @param string $objectId The identifier for the object. The repository might return a different/new object id |
||
894 | 1 | * @param StreamInterface $contentStream The content stream |
|
895 | 1 | * @param boolean $overwriteFlag If <code>true</code>, then the repository must replace the existing content stream |
|
896 | * for the object (if any) with the input content stream. If <code>false</code>, then the repository must |
||
897 | 1 | * only set the input content stream for the object if the object currently does not have a content stream |
|
898 | 1 | * (default is <code>true</code>) |
|
899 | 1 | * @param string|null $changeToken The last change token of this object that the client received. |
|
900 | 1 | * The repository might return a new change token (default is <code>null</code>) |
|
901 | 1 | * @param ExtensionDataInterface|null $extension |
|
902 | 1 | * @throws CmisInvalidArgumentException If object id is empty |
|
903 | */ |
||
904 | 1 | public function setContentStream( |
|
948 | 3 | ||
949 | 3 | /** |
|
950 | 3 | * Updates properties of the specified object. |
|
951 | * |
||
952 | 3 | * @param string $repositoryId The identifier for the repository |
|
953 | 1 | * @param string $objectId The identifier for the object. The repository might return a different/new object id |
|
954 | 1 | * @param PropertiesInterface $properties The updated property values that must be applied to the object |
|
955 | * @param string|null $changeToken The last change token of this object that the client received. |
||
956 | 3 | * The repository might return a new change token (default is <code>null</code>) |
|
957 | 3 | * @param ExtensionDataInterface|null $extension |
|
958 | * @throws CmisInvalidArgumentException If $objectId is empty |
||
959 | 3 | */ |
|
960 | public function updateProperties( |
||
996 | |||
997 | /** |
||
998 | 3 | * @param string $identifier |
|
999 | * @param mixed $additionalHashValues |
||
1000 | 3 | * @return array |
|
1001 | */ |
||
1002 | 3 | protected function createCacheKey($identifier, $additionalHashValues) |
|
1009 | 3 | ||
1010 | 3 | /** |
|
1011 | * Returns TRUE if an object with cache key $identifier is currently cached. |
||
1012 | * |
||
1013 | 3 | * @param array $identifier |
|
1014 | 3 | * @return boolean |
|
1015 | 3 | */ |
|
1016 | 3 | protected function isCached(array $identifier) |
|
1020 | 2 | ||
1021 | 2 | /** |
|
1022 | 3 | * Gets the cached object with cache key $identifier. |
|
1023 | 3 | * |
|
1024 | * @param string $identifier |
||
1025 | * @return mixed |
||
1026 | */ |
||
1027 | protected function getCached(array $identifier) |
||
1031 | |||
1032 | /** |
||
1033 | 8 | * Gets the cached object with cache key $identifier. |
|
1034 | 8 | * |
|
1035 | 8 | * @param string $identifier |
|
1036 | * @param mixed $object |
||
1037 | * @return mixed |
||
1038 | */ |
||
1039 | protected function cache(array $identifier, $object) |
||
1044 | 8 | ||
1045 | /** |
||
1046 | 8 | * Flushes all cached entries. This is implemented as a flush-all with |
|
1047 | * no way to flush individual entries due to the way CMIS object data |
||
1048 | * gets returned from CMIS. Two widely different object data sets may |
||
1049 | * contain a reference to the same item and even with extensive cross |
||
1050 | * referencing it would be technically unfeasible to selectively clear |
||
1051 | * or reload an object by identifier. Such flushing would be inevitably |
||
1052 | * flawed with edge cases of incomplete flushing or become so complex |
||
1053 | * that it defeats the purpose of caching in the first place. |
||
1054 | * |
||
1055 | * Note that cache flushing only happens when modifying the repository |
||
1056 | * contents - which should limit the negative impact. The cache is also |
||
1057 | * not persistent and will only affect the current request. As such, it |
||
1058 | * is implemented to optimise requests where the same object, type, |
||
1059 | * policy etc. gets accessed multiple times. |
||
1060 | * |
||
1061 | * @return void |
||
1062 | */ |
||
1063 | protected function flushCached() |
||
1067 | } |
||
1068 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.