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 |
||
37 | class ObjectService extends AbstractBrowserBindingService implements ObjectServiceInterface |
||
38 | { |
||
39 | /** |
||
40 | * L1 cache for objects. Fills with two levels: |
||
41 | * |
||
42 | * - First level key is the object ID, path or other singular identifier of object(s) |
||
43 | * - Second level key is a hash of context arguments used to retrieve the object(s) |
||
44 | * |
||
45 | * @var array |
||
46 | */ |
||
47 | protected $objectCache = []; |
||
48 | |||
49 | /** |
||
50 | * Appends the content stream to the content of the document. |
||
51 | * |
||
52 | * The stream in contentStream is consumed but not closed by this method. |
||
53 | * |
||
54 | * @param string $repositoryId the identifier for the repository |
||
55 | * @param string $objectId The identifier for the object. The repository might return a different/new object id |
||
56 | * @param StreamInterface $contentStream The content stream to append |
||
57 | * @param boolean $isLastChunk Indicates if this content stream is the last chunk |
||
58 | * @param string|null $changeToken The last change token of this object that the client received. |
||
59 | * The repository might return a new change token (default is <code>null</code>) |
||
60 | * @param ExtensionDataInterface|null $extension |
||
61 | */ |
||
62 | public function appendContentStream( |
||
72 | |||
73 | /** |
||
74 | * Updates properties and secondary types of one or more objects. |
||
75 | * |
||
76 | * @param string $repositoryId the identifier for the repository |
||
77 | * @param BulkUpdateObjectIdAndChangeTokenInterface[] $objectIdsAndChangeTokens |
||
78 | * @param PropertiesInterface $properties |
||
79 | * @param string[] $addSecondaryTypeIds the secondary types to apply |
||
80 | * @param string[] $removeSecondaryTypeIds the secondary types to remove |
||
81 | * @param ExtensionDataInterface|null $extension |
||
82 | * @return BulkUpdateObjectIdAndChangeTokenInterface[] |
||
83 | */ |
||
84 | public function bulkUpdateProperties( |
||
94 | |||
95 | /** |
||
96 | * @param string $action |
||
97 | * @param PropertiesInterface $properties |
||
98 | * @param string[] $policies |
||
99 | * @param AclInterface $addAces |
||
|
|||
100 | * @param AclInterface $removeAces |
||
101 | * @param ExtensionDataInterface $extension |
||
102 | * @return array |
||
103 | */ |
||
104 | 9 | protected function createQueryArray( |
|
136 | |||
137 | /** |
||
138 | * Creates a document object of the specified type (given by the cmis:objectTypeId property) |
||
139 | * in the (optionally) specified location. |
||
140 | * |
||
141 | * @param string $repositoryId the identifier for the repository |
||
142 | * @param PropertiesInterface $properties the property values that must be applied to the newly |
||
143 | * created document object |
||
144 | * @param string|null $folderId if specified, the identifier for the folder that must be the parent |
||
145 | * folder for the newly created document object |
||
146 | * @param StreamInterface|null $contentStream the content stream that must be stored for the newly |
||
147 | * created document object |
||
148 | * @param VersioningState|null $versioningState specifies what the versioning state of the newly created object |
||
149 | * must be (default is <code>VersioningState::MAJOR</code>) |
||
150 | * @param string[] $policies a list of policy IDs that must be applied to the newly created document object |
||
151 | * @param AclInterface|null $addAces a list of ACEs that must be added to the newly created document object, |
||
152 | * either using the ACL from folderId if specified, or being applied if no folderId is specified |
||
153 | * @param AclInterface|null $removeAces a list of ACEs that must be removed from the newly created document object, |
||
154 | * either using the ACL from folderId if specified, or being ignored if no folderId is specified |
||
155 | * @param ExtensionDataInterface|null $extension |
||
156 | * @return string|null Returns the new object id or <code>null</code> if the repository sent an empty |
||
157 | * result (which should not happen) |
||
158 | */ |
||
159 | 3 | public function createDocument( |
|
207 | |||
208 | /** |
||
209 | * Creates a document object as a copy of the given source document in the (optionally) specified location. |
||
210 | * |
||
211 | * @param string $repositoryId the identifier for the repository |
||
212 | * @param string $sourceId the identifier for the source document |
||
213 | * @param PropertiesInterface $properties the property values that must be applied to the newly |
||
214 | * created document object |
||
215 | * @param string|null $folderId if specified, the identifier for the folder that must be the parent folder for the |
||
216 | * newly created document object |
||
217 | * @param VersioningState|null $versioningState specifies what the versioning state of the newly created object |
||
218 | * must be (default is <code>VersioningState::MAJOR</code>) |
||
219 | * @param string[] $policies a list of policy IDs that must be applied to the newly created document object |
||
220 | * @param AclInterface|null $addAces a list of ACEs that must be added to the newly created document object, |
||
221 | * either using the ACL from folderId if specified, or being applied if no folderId is specified |
||
222 | * @param AclInterface|null $removeAces a list of ACEs that must be removed from the newly created document object, |
||
223 | * either using the ACL from folderId if specified, or being ignored if no folderId is specified |
||
224 | * @param ExtensionDataInterface|null $extension |
||
225 | * @return string|null Returns the new object id or <code>null</code> if the repository sent an empty |
||
226 | * result (which should not happen) |
||
227 | */ |
||
228 | public function createDocumentFromSource( |
||
262 | 2 | ||
263 | /** |
||
264 | 2 | * Creates a folder object of the specified type (given by the cmis:objectTypeId property) in |
|
265 | * the specified location. |
||
266 | * |
||
267 | * @param string $repositoryId the identifier for the repository |
||
268 | * @param PropertiesInterface $properties the property values that must be applied to the newly |
||
269 | * created document object |
||
270 | * @param string $folderId if specified, the identifier for the folder that must be the parent folder for the |
||
271 | * newly created document object |
||
272 | * @param string[] $policies a list of policy IDs that must be applied to the newly created document object |
||
273 | * @param AclInterface|null $addAces a list of ACEs that must be added to the newly created document object, |
||
274 | * either using the ACL from folderId if specified, or being applied if no folderId is specified |
||
275 | * @param AclInterface|null $removeAces a list of ACEs that must be removed from the newly created document object, |
||
276 | * either using the ACL from folderId if specified, or being ignored if no folderId is specified |
||
277 | * @param ExtensionDataInterface|null $extension |
||
278 | * @return string|null Returns the new object id or <code>null</code> if the repository sent an empty |
||
279 | * result (which should not happen) |
||
280 | */ |
||
281 | View Code Duplication | public function createFolder( |
|
304 | 2 | ||
305 | /** |
||
306 | 2 | * Creates an item object of the specified type (given by the cmis:objectTypeId property). |
|
307 | * |
||
308 | 2 | * @param string $repositoryId The identifier for the repository |
|
309 | * @param PropertiesInterface $properties The property values that must be applied to the newly |
||
310 | * created document object |
||
311 | * @param string|null $folderId If specified, the identifier for the folder that must be the parent folder for the |
||
312 | * newly created document object |
||
313 | * @param string[] $policies A list of policy IDs that must be applied to the newly created document object |
||
314 | * @param AclInterface|null $addAces A list of ACEs that must be added to the newly created document object, |
||
315 | * either using the ACL from folderId if specified, or being applied if no folderId is specified |
||
316 | * @param AclInterface|null $removeAces A list of ACEs that must be removed from the newly created document object, |
||
317 | * either using the ACL from folderId if specified, or being ignored if no folderId is specified |
||
318 | * @param ExtensionDataInterface|null $extension |
||
319 | * @return string|null Returns the new item id or <code>null</code> if the repository sent an empty |
||
320 | * result (which should not happen) |
||
321 | */ |
||
322 | public function createItem( |
||
350 | 2 | ||
351 | /** |
||
352 | 2 | * Creates a policy object of the specified type (given by the cmis:objectTypeId property). |
|
353 | * |
||
354 | 2 | * @param string $repositoryId The identifier for the repository |
|
355 | * @param PropertiesInterface $properties The property values that must be applied to the newly |
||
356 | 2 | * created document object |
|
357 | * @param string|null $folderId If specified, the identifier for the folder that must be the parent folder for the |
||
358 | * newly created document object |
||
359 | * @param string[] $policies A list of policy IDs that must be applied to the newly created document object |
||
360 | * @param AclInterface|null $addAces A list of ACEs that must be added to the newly created document object, |
||
361 | * either using the ACL from folderId if specified, or being applied if no folderId is specified |
||
362 | * @param AclInterface|null $removeAces A list of ACEs that must be removed from the newly created document object, |
||
363 | * either using the ACL from folderId if specified, or being ignored if no folderId is specified |
||
364 | * @param ExtensionDataInterface|null $extension |
||
365 | * @return string The id of the newly-created policy. |
||
366 | */ |
||
367 | public function createPolicy( |
||
378 | |||
379 | /** |
||
380 | * Creates a relationship object of the specified type (given by the cmis:objectTypeId property). |
||
381 | * |
||
382 | * @param string $repositoryId the identifier for the repository |
||
383 | * @param PropertiesInterface $properties the property values that must be applied to the newly |
||
384 | * created document object |
||
385 | * @param string[] $policies a list of policy IDs that must be applied to the newly created document object |
||
386 | * @param AclInterface|null $addAces a list of ACEs that must be added to the newly created document object, |
||
387 | * either using the ACL from folderId if specified, or being applied if no folderId is specified |
||
388 | * @param AclInterface|null $removeAces a list of ACEs that must be removed from the newly created document object, |
||
389 | * either using the ACL from folderId if specified, or being ignored if no folderId is specified |
||
390 | * @param ExtensionDataInterface|null $extension |
||
391 | * @return string|null Returns the new item id of the relationship object or <code>null</code> if the repository |
||
392 | * sent an empty result (which should not happen) |
||
393 | */ |
||
394 | View Code Duplication | public function createRelationship( |
|
417 | |||
418 | /** |
||
419 | * Deletes the content stream for the specified document object. |
||
420 | * |
||
421 | * @param string $repositoryId the identifier for the repository |
||
422 | * @param string $objectId the identifier for the object. The repository might return a different/new object id |
||
423 | * @param string|null $changeToken the last change token of this object that the client received. |
||
424 | * The repository might return a new change token (default is <code>null</code>) |
||
425 | * @param ExtensionDataInterface|null $extension |
||
426 | * @throws CmisInvalidArgumentException If $objectId is empty |
||
427 | */ |
||
428 | public function deleteContentStream( |
||
467 | 3 | ||
468 | 3 | /** |
|
469 | 3 | * Deletes the specified object. |
|
470 | 3 | * |
|
471 | 3 | * @param string $repositoryId the identifier for the repository |
|
472 | 2 | * @param string $objectId the identifier for the object |
|
473 | * @param boolean $allVersions If <code>true</code> then delete all versions of the document, otherwise delete only |
||
474 | 2 | * the document object specified (default is <code>true</code>) |
|
475 | 2 | * @param ExtensionDataInterface|null $extension |
|
476 | 3 | */ |
|
477 | 3 | View Code Duplication | public function deleteObject( |
494 | 2 | ||
495 | 2 | /** |
|
496 | * Deletes the specified folder object and all of its child- and descendant-objects. |
||
497 | 2 | * |
|
498 | 2 | * @param string $repositoryId the identifier for the repository |
|
499 | * @param string $folderId the identifier for the folder |
||
500 | 2 | * @param boolean $allVersions If <code>true</code> then delete all versions of the document, otherwise delete only |
|
501 | * the document object specified (default is <code>true</code>) |
||
502 | 2 | * @param UnfileObject|null $unfileObjects defines how the repository must process file-able child- or |
|
503 | 2 | * descendant-objects (default is <code>UnfileObject::DELETE</code>) |
|
504 | 2 | * @param boolean $continueOnFailure If <code>true</code>, then the repository should continue attempting to |
|
505 | * perform this operation even if deletion of a child- or descendant-object in the specified folder cannot |
||
506 | * be deleted |
||
507 | * @param ExtensionDataInterface|null $extension |
||
508 | * @return FailedToDeleteDataInterface Returns a list of object ids that could not be deleted |
||
509 | */ |
||
510 | public function deleteTree( |
||
534 | 4 | ||
535 | 4 | /** |
|
536 | 4 | * Gets the list of allowable actions for an object. |
|
537 | 4 | * |
|
538 | * @param string $repositoryId the identifier for the repository |
||
539 | 4 | * @param string $objectId the identifier for the object |
|
540 | 1 | * @param ExtensionDataInterface|null $extension |
|
541 | 1 | * @return AllowableActionsInterface |
|
542 | */ |
||
543 | 4 | public function getAllowableActions($repositoryId, $objectId, ExtensionDataInterface $extension = null) |
|
547 | 4 | ||
548 | /** |
||
549 | 4 | * Gets the content stream for the specified document object, or gets a rendition stream for |
|
550 | 4 | * a specified rendition of a document or folder object. |
|
551 | * |
||
552 | * @param string $repositoryId the identifier for the repository |
||
553 | * @param string $objectId the identifier for the object |
||
554 | * @param string|null $streamId The identifier for the rendition stream, when used to get a rendition stream. |
||
555 | * For documents, if not provided then this method returns the content stream. For folders, |
||
556 | * it MUST be provided. |
||
557 | * @param integer|null $offset |
||
558 | * @param integer|null $length |
||
559 | * @param ExtensionDataInterface|null $extension |
||
560 | * @return StreamInterface|null |
||
561 | * @throws CmisInvalidArgumentException If object id is empty |
||
562 | */ |
||
563 | public function getContentStream( |
||
595 | 3 | ||
596 | 1 | /** |
|
597 | 1 | * Gets the specified information for the object specified by id. |
|
598 | * |
||
599 | * @param string $repositoryId the identifier for the repository |
||
600 | 3 | * @param string $objectId the identifier for the object |
|
601 | * @param string|null $filter a comma-separated list of query names that defines which properties must be |
||
602 | 3 | * returned by the repository (default is repository specific) |
|
603 | 3 | * @param boolean $includeAllowableActions if <code>true</code>, then the repository must return the allowable |
|
604 | * actions for the object (default is <code>false</code>) |
||
605 | * @param IncludeRelationships|null $includeRelationships indicates what relationships in which the objects |
||
606 | * participate must be returned (default is <code>IncludeRelationships::NONE</code>) |
||
607 | 3 | * @param string $renditionFilter indicates what set of renditions the repository must return whose kind |
|
608 | 1 | * matches this filter (default is "cmis:none") |
|
609 | 1 | * @param boolean $includePolicyIds if <code>true</code>, then the repository must return the policy ids for |
|
610 | * the object (default is <code>false</code>) |
||
611 | 3 | * @param boolean $includeAcl if <code>true</code>, then the repository must return the ACL for the object |
|
612 | * (default is <code>false</code>) |
||
613 | * @param ExtensionDataInterface|null $extension |
||
614 | * @return ObjectDataInterface|null Returns object of type ObjectDataInterface or <code>null</code> |
||
615 | * if the repository response was empty |
||
616 | */ |
||
617 | View Code Duplication | public function getObject( |
|
672 | 3 | ||
673 | 3 | /** |
|
674 | * Gets the specified information for the object specified by path. |
||
675 | 3 | * |
|
676 | 2 | * @param string $repositoryId the identifier for the repository |
|
677 | 2 | * @param string $path the path to the object |
|
678 | * @param string|null $filter a comma-separated list of query names that defines which properties must be |
||
679 | 3 | * returned by the repository (default is repository specific) |
|
680 | 2 | * @param boolean $includeAllowableActions if <code>true</code>, then the repository must return the allowable |
|
681 | 2 | * actions for the object (default is <code>false</code>) |
|
682 | * @param IncludeRelationships|null $includeRelationships indicates what relationships in which the objects |
||
683 | 3 | * participate must be returned (default is <code>IncludeRelationships::NONE</code>) |
|
684 | * @param string $renditionFilter indicates what set of renditions the repository must return whose kind |
||
685 | 3 | * matches this filter (default is "cmis:none") |
|
686 | 3 | * @param boolean $includePolicyIds if <code>true</code>, then the repository must return the policy ids for |
|
687 | 3 | * the object (default is <code>false</code>) |
|
688 | 3 | * @param boolean $includeAcl if <code>true</code>, then the repository must return the ACL for the object |
|
689 | * (default is <code>false</code>) |
||
690 | * @param ExtensionDataInterface|null $extension |
||
691 | * @return ObjectDataInterface|null Returns object of type <code>ObjectDataInterface</code> or <code>null</code> |
||
692 | * if the repository response was empty |
||
693 | */ |
||
694 | View Code Duplication | public function getObjectByPath( |
|
750 | 3 | ||
751 | 3 | /** |
|
752 | * Gets the list of properties for an object. |
||
753 | 3 | * |
|
754 | 2 | * @param string $repositoryId the identifier for the repository |
|
755 | 2 | * @param string $objectId the identifier for the object |
|
756 | * @param string|null $filter a comma-separated list of query names that defines which properties must be |
||
757 | 3 | * returned by the repository (default is repository specific) |
|
758 | 2 | * @param ExtensionDataInterface|null $extension |
|
759 | 2 | * @return PropertiesInterface |
|
760 | */ |
||
761 | 3 | public function getProperties( |
|
806 | |||
807 | 2 | /** |
|
808 | 1 | * Gets the list of associated renditions for the specified object. |
|
809 | 1 | * Only rendition attributes are returned, not rendition stream. |
|
810 | * |
||
811 | 2 | * @param string $repositoryId the identifier for the repository |
|
812 | * @param string $objectId the identifier for the object |
||
813 | 2 | * @param string $renditionFilter indicates what set of renditions the repository must return whose kind |
|
814 | * matches this filter (default is "cmis:none") |
||
815 | * @param integer|null $maxItems the maximum number of items to return in a response |
||
816 | 2 | * (default is repository specific) |
|
817 | * @param integer $skipCount number of potential results that the repository MUST skip/page over before |
||
818 | * returning any results (default is 0) |
||
819 | 2 | * @param ExtensionDataInterface|null $extension |
|
820 | 2 | * @return RenditionDataInterface[] |
|
821 | * @throws CmisInvalidArgumentException If object id is empty or skip count not of type integer |
||
822 | 2 | */ |
|
823 | public function getRenditions( |
||
855 | |||
856 | /** |
||
857 | 2 | * Moves the specified file-able object from one folder to another. |
|
858 | 2 | * |
|
859 | * @param string $repositoryId the identifier for the repository |
||
860 | 2 | * @param string $objectId the identifier for the object. The repository might return a different/new object id |
|
861 | 2 | * @param string $targetFolderId the identifier for the target folder |
|
862 | * @param string $sourceFolderId the identifier for the source folder |
||
863 | 2 | * @param ExtensionDataInterface|null $extension |
|
864 | * @return ObjectDataInterface|null Returns object of type ObjectDataInterface or <code>null</code> |
||
865 | 2 | * if the repository response was empty |
|
866 | 1 | */ |
|
867 | 1 | public function moveObject( |
|
893 | |||
894 | 1 | /** |
|
895 | 1 | * Sets the content stream for the specified document object. |
|
896 | * |
||
897 | 1 | * @param string $repositoryId The identifier for the repository |
|
898 | 1 | * @param string $objectId The identifier for the object. The repository might return a different/new object id |
|
899 | 1 | * @param StreamInterface $contentStream The content stream |
|
900 | 1 | * @param boolean $overwriteFlag If <code>true</code>, then the repository must replace the existing content stream |
|
901 | 1 | * for the object (if any) with the input content stream. If <code>false</code>, then the repository must |
|
902 | 1 | * only set the input content stream for the object if the object currently does not have a content stream |
|
903 | * (default is <code>true</code>) |
||
904 | 1 | * @param string|null $changeToken The last change token of this object that the client received. |
|
905 | 1 | * The repository might return a new change token (default is <code>null</code>) |
|
906 | * @param ExtensionDataInterface|null $extension |
||
907 | * @throws CmisInvalidArgumentException If object id is empty |
||
908 | 1 | */ |
|
909 | public function setContentStream( |
||
953 | 1 | ||
954 | 1 | /** |
|
955 | * Updates properties of the specified object. |
||
956 | 3 | * |
|
957 | 3 | * @param string $repositoryId The identifier for the repository |
|
958 | * @param string $objectId The identifier for the object. The repository might return a different/new object id |
||
959 | 3 | * @param PropertiesInterface $properties The updated property values that must be applied to the object |
|
960 | * @param string|null $changeToken The last change token of this object that the client received. |
||
961 | 3 | * The repository might return a new change token (default is <code>null</code>) |
|
962 | * @param ExtensionDataInterface|null $extension |
||
963 | * @throws CmisInvalidArgumentException If $objectId is empty |
||
964 | 3 | */ |
|
965 | 3 | public function updateProperties( |
|
1001 | |||
1002 | 3 | /** |
|
1003 | 1 | * @param string $identifier |
|
1004 | 1 | * @param mixed $additionalHashValues |
|
1005 | * @return array |
||
1006 | 3 | */ |
|
1007 | 3 | protected function createCacheKey($identifier, $additionalHashValues) |
|
1014 | 3 | ||
1015 | 3 | /** |
|
1016 | 3 | * Returns TRUE if an object with cache key $identifier is currently cached. |
|
1017 | 3 | * |
|
1018 | 2 | * @param array $identifier |
|
1019 | * @return boolean |
||
1020 | 2 | */ |
|
1021 | 2 | protected function isCached(array $identifier) |
|
1025 | |||
1026 | /** |
||
1027 | * Gets the cached object with cache key $identifier. |
||
1028 | * |
||
1029 | * @param string $identifier |
||
1030 | 8 | * @return mixed |
|
1031 | */ |
||
1032 | protected function getCached(array $identifier) |
||
1036 | |||
1037 | /** |
||
1038 | * Gets the cached object with cache key $identifier. |
||
1039 | * |
||
1040 | * @param string $identifier |
||
1041 | * @param mixed $object |
||
1042 | * @return mixed |
||
1043 | */ |
||
1044 | 8 | protected function cache(array $identifier, $object) |
|
1049 | |||
1050 | /** |
||
1051 | * Flushes all cached entries. This is implemented as a flush-all with |
||
1052 | * no way to flush individual entries due to the way CMIS object data |
||
1053 | * gets returned from CMIS. Two widely different object data sets may |
||
1054 | * contain a reference to the same item and even with extensive cross |
||
1055 | * referencing it would be technically unfeasible to selectively clear |
||
1056 | * or reload an object by identifier. Such flushing would be inevitably |
||
1057 | * flawed with edge cases of incomplete flushing or become so complex |
||
1058 | * that it defeats the purpose of caching in the first place. |
||
1059 | * |
||
1060 | * Note that cache flushing only happens when modifying the repository |
||
1061 | * contents - which should limit the negative impact. The cache is also |
||
1062 | * not persistent and will only affect the current request. As such, it |
||
1063 | * is implemented to optimise requests where the same object, type, |
||
1064 | * policy etc. gets accessed multiple times. |
||
1065 | * |
||
1066 | * @return void |
||
1067 | */ |
||
1068 | protected function flushCached() |
||
1072 | } |
||
1073 |
This check looks for
@param
annotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.