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 = array(); |
||
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( |
|
211 | |||
212 | /** |
||
213 | * Creates a document object as a copy of the given source document in the (optionally) specified location. |
||
214 | * |
||
215 | * @param string $repositoryId the identifier for the repository |
||
216 | * @param string $sourceId the identifier for the source document |
||
217 | * @param PropertiesInterface $properties the property values that must be applied to the newly |
||
218 | * created document object |
||
219 | * @param string|null $folderId if specified, the identifier for the folder that must be the parent folder for the |
||
220 | * newly created document object |
||
221 | * @param VersioningState|null $versioningState specifies what the versioning state of the newly created object |
||
222 | * must be (default is <code>VersioningState::MAJOR</code>) |
||
223 | * @param string[] $policies a list of policy IDs that must be applied to the newly created document object |
||
224 | * @param AclInterface|null $addAces a list of ACEs that must be added to the newly created document object, |
||
225 | * either using the ACL from folderId if specified, or being applied if no folderId is specified |
||
226 | * @param AclInterface|null $removeAces a list of ACEs that must be removed from the newly created document object, |
||
227 | * either using the ACL from folderId if specified, or being ignored if no folderId is specified |
||
228 | * @param ExtensionDataInterface|null $extension |
||
229 | * @return string|null Returns the new object id or <code>null</code> if the repository sent an empty |
||
230 | * result (which should not happen) |
||
231 | */ |
||
232 | 2 | public function createDocumentFromSource( |
|
267 | |||
268 | /** |
||
269 | * Creates a folder object of the specified type (given by the cmis:objectTypeId property) in |
||
270 | * the specified location. |
||
271 | * |
||
272 | * @param string $repositoryId the identifier for the repository |
||
273 | * @param PropertiesInterface $properties the property values that must be applied to the newly |
||
274 | * created document object |
||
275 | * @param string $folderId if specified, the identifier for the folder that must be the parent folder for the |
||
276 | * newly created document object |
||
277 | * @param string[] $policies a list of policy IDs that must be applied to the newly created document object |
||
278 | * @param AclInterface|null $addAces a list of ACEs that must be added to the newly created document object, |
||
279 | * either using the ACL from folderId if specified, or being applied if no folderId is specified |
||
280 | * @param AclInterface|null $removeAces a list of ACEs that must be removed from the newly created document object, |
||
281 | * either using the ACL from folderId if specified, or being ignored if no folderId is specified |
||
282 | * @param ExtensionDataInterface|null $extension |
||
283 | * @return string|null Returns the new object id or <code>null</code> if the repository sent an empty |
||
284 | * result (which should not happen) |
||
285 | */ |
||
286 | 2 | View Code Duplication | public function createFolder( |
311 | |||
312 | /** |
||
313 | * Creates an item object of the specified type (given by the cmis:objectTypeId property). |
||
314 | * |
||
315 | * @param string $repositoryId The identifier for the repository |
||
316 | * @param PropertiesInterface $properties The property values that must be applied to the newly |
||
317 | * created document object |
||
318 | * @param string|null $folderId If specified, the identifier for the folder that must be the parent folder for the |
||
319 | * newly created document object |
||
320 | * @param string[] $policies A list of policy IDs that must be applied to the newly created document object |
||
321 | * @param AclInterface|null $addAces A list of ACEs that must be added to the newly created document object, |
||
322 | * either using the ACL from folderId if specified, or being applied if no folderId is specified |
||
323 | * @param AclInterface|null $removeAces A list of ACEs that must be removed from the newly created document object, |
||
324 | * either using the ACL from folderId if specified, or being ignored if no folderId is specified |
||
325 | * @param ExtensionDataInterface|null $extension |
||
326 | * @return string|null Returns the new item id or <code>null</code> if the repository sent an empty |
||
327 | * result (which should not happen) |
||
328 | */ |
||
329 | 2 | public function createItem( |
|
359 | |||
360 | /** |
||
361 | * Creates a policy object of the specified type (given by the cmis:objectTypeId property). |
||
362 | * |
||
363 | * @param string $repositoryId The identifier for the repository |
||
364 | * @param PropertiesInterface $properties The property values that must be applied to the newly |
||
365 | * created document object |
||
366 | * @param string|null $folderId If specified, the identifier for the folder that must be the parent folder for the |
||
367 | * newly created document object |
||
368 | * @param string[] $policies A list of policy IDs that must be applied to the newly created document object |
||
369 | * @param AclInterface|null $addAces A list of ACEs that must be added to the newly created document object, |
||
370 | * either using the ACL from folderId if specified, or being applied if no folderId is specified |
||
371 | * @param AclInterface|null $removeAces A list of ACEs that must be removed from the newly created document object, |
||
372 | * either using the ACL from folderId if specified, or being ignored if no folderId is specified |
||
373 | * @param ExtensionDataInterface|null $extension |
||
374 | * @return string The id of the newly-created policy. |
||
375 | */ |
||
376 | public function createPolicy( |
||
387 | |||
388 | /** |
||
389 | * Creates a relationship object of the specified type (given by the cmis:objectTypeId property). |
||
390 | * |
||
391 | * @param string $repositoryId the identifier for the repository |
||
392 | * @param PropertiesInterface $properties the property values that must be applied to the newly |
||
393 | * created document object |
||
394 | * @param string[] $policies a list of policy IDs that must be applied to the newly created document object |
||
395 | * @param AclInterface|null $addAces a list of ACEs that must be added to the newly created document object, |
||
396 | * either using the ACL from folderId if specified, or being applied if no folderId is specified |
||
397 | * @param AclInterface|null $removeAces a list of ACEs that must be removed from the newly created document object, |
||
398 | * either using the ACL from folderId if specified, or being ignored if no folderId is specified |
||
399 | * @param ExtensionDataInterface|null $extension |
||
400 | * @return string|null Returns the new item id of the relationship object or <code>null</code> if the repository |
||
401 | * sent an empty result (which should not happen) |
||
402 | */ |
||
403 | View Code Duplication | public function createRelationship( |
|
428 | |||
429 | /** |
||
430 | * Deletes the content stream for the specified document object. |
||
431 | * |
||
432 | * @param string $repositoryId the identifier for the repository |
||
433 | * @param string $objectId the identifier for the object. The repository might return a different/new object id |
||
434 | * @param string|null $changeToken the last change token of this object that the client received. |
||
435 | * The repository might return a new change token (default is <code>null</code>) |
||
436 | * @param ExtensionDataInterface|null $extension |
||
437 | * @throws CmisInvalidArgumentException If $objectId is empty |
||
438 | */ |
||
439 | 3 | public function deleteContentStream( |
|
479 | |||
480 | /** |
||
481 | * Deletes the specified object. |
||
482 | * |
||
483 | * @param string $repositoryId the identifier for the repository |
||
484 | * @param string $objectId the identifier for the object |
||
485 | * @param boolean $allVersions If <code>true</code> then delete all versions of the document, otherwise delete only |
||
486 | * the document object specified (default is <code>true</code>) |
||
487 | * @param ExtensionDataInterface|null $extension |
||
488 | */ |
||
489 | 2 | public function deleteObject( |
|
506 | |||
507 | /** |
||
508 | * Deletes the specified folder object and all of its child- and descendant-objects. |
||
509 | * |
||
510 | * @param string $repositoryId the identifier for the repository |
||
511 | * @param string $folderId the identifier for the folder |
||
512 | * @param boolean $allVersions If <code>true</code> then delete all versions of the document, otherwise delete only |
||
513 | * the document object specified (default is <code>true</code>) |
||
514 | * @param UnfileObject|null $unfileObjects defines how the repository must process file-able child- or |
||
515 | * descendant-objects (default is <code>UnfileObject::DELETE</code>) |
||
516 | * @param boolean $continueOnFailure If <code>true</code>, then the repository should continue attempting to |
||
517 | * perform this operation even if deletion of a child- or descendant-object in the specified folder cannot |
||
518 | * be deleted |
||
519 | * @param ExtensionDataInterface|null $extension |
||
520 | * @return FailedToDeleteDataInterface Returns a list of object ids that could not be deleted |
||
521 | */ |
||
522 | 4 | public function deleteTree( |
|
548 | |||
549 | /** |
||
550 | * Gets the list of allowable actions for an object. |
||
551 | * |
||
552 | * @param string $repositoryId the identifier for the repository |
||
553 | * @param string $objectId the identifier for the object |
||
554 | * @param ExtensionDataInterface|null $extension |
||
555 | * @return AllowableActionsInterface |
||
556 | */ |
||
557 | public function getAllowableActions($repositoryId, $objectId, ExtensionDataInterface $extension = null) |
||
561 | |||
562 | /** |
||
563 | * Gets the content stream for the specified document object, or gets a rendition stream for |
||
564 | * a specified rendition of a document or folder object. |
||
565 | * |
||
566 | * @param string $repositoryId the identifier for the repository |
||
567 | * @param string $objectId the identifier for the object |
||
568 | * @param string|null $streamId The identifier for the rendition stream, when used to get a rendition stream. |
||
569 | * For documents, if not provided then this method returns the content stream. For folders, |
||
570 | * it MUST be provided. |
||
571 | * @param integer|null $offset |
||
572 | * @param integer|null $length |
||
573 | * @param ExtensionDataInterface|null $extension |
||
574 | * @return StreamInterface|null |
||
575 | * @throws CmisInvalidArgumentException If object id is empty |
||
576 | */ |
||
577 | 3 | public function getContentStream( |
|
609 | |||
610 | /** |
||
611 | * Gets the specified information for the object specified by id. |
||
612 | * |
||
613 | * @param string $repositoryId the identifier for the repository |
||
614 | * @param string $objectId the identifier for the object |
||
615 | * @param string|null $filter a comma-separated list of query names that defines which properties must be |
||
616 | * returned by the repository (default is repository specific) |
||
617 | * @param boolean $includeAllowableActions if <code>true</code>, then the repository must return the allowable |
||
618 | * actions for the object (default is <code>false</code>) |
||
619 | * @param IncludeRelationships|null $includeRelationships indicates what relationships in which the objects |
||
620 | * participate must be returned (default is <code>IncludeRelationships::NONE</code>) |
||
621 | * @param string $renditionFilter indicates what set of renditions the repository must return whose kind |
||
622 | * matches this filter (default is "cmis:none") |
||
623 | * @param boolean $includePolicyIds if <code>true</code>, then the repository must return the policy ids for |
||
624 | * the object (default is <code>false</code>) |
||
625 | * @param boolean $includeAcl if <code>true</code>, then the repository must return the ACL for the object |
||
626 | * (default is <code>false</code>) |
||
627 | * @param ExtensionDataInterface|null $extension |
||
628 | * @return ObjectDataInterface|null Returns object of type ObjectDataInterface or <code>null</code> |
||
629 | * if the repository response was empty |
||
630 | */ |
||
631 | 3 | View Code Duplication | public function getObject( |
686 | |||
687 | /** |
||
688 | * Gets the specified information for the object specified by path. |
||
689 | * |
||
690 | * @param string $repositoryId the identifier for the repository |
||
691 | * @param string $path the path to the object |
||
692 | * @param string|null $filter a comma-separated list of query names that defines which properties must be |
||
693 | * returned by the repository (default is repository specific) |
||
694 | * @param boolean $includeAllowableActions if <code>true</code>, then the repository must return the allowable |
||
695 | * actions for the object (default is <code>false</code>) |
||
696 | * @param IncludeRelationships|null $includeRelationships indicates what relationships in which the objects |
||
697 | * participate must be returned (default is <code>IncludeRelationships::NONE</code>) |
||
698 | * @param string $renditionFilter indicates what set of renditions the repository must return whose kind |
||
699 | * matches this filter (default is "cmis:none") |
||
700 | * @param boolean $includePolicyIds if <code>true</code>, then the repository must return the policy ids for |
||
701 | * the object (default is <code>false</code>) |
||
702 | * @param boolean $includeAcl if <code>true</code>, then the repository must return the ACL for the object |
||
703 | * (default is <code>false</code>) |
||
704 | * @param ExtensionDataInterface|null $extension |
||
705 | * @return ObjectDataInterface|null Returns object of type <code>ObjectDataInterface</code> or <code>null</code> |
||
706 | * if the repository response was empty |
||
707 | */ |
||
708 | 3 | View Code Duplication | public function getObjectByPath( |
764 | |||
765 | /** |
||
766 | * Gets the list of properties for an object. |
||
767 | * |
||
768 | * @param string $repositoryId the identifier for the repository |
||
769 | * @param string $objectId the identifier for the object |
||
770 | * @param string|null $filter a comma-separated list of query names that defines which properties must be |
||
771 | * returned by the repository (default is repository specific) |
||
772 | * @param ExtensionDataInterface|null $extension |
||
773 | * @return PropertiesInterface |
||
774 | */ |
||
775 | 2 | public function getProperties( |
|
820 | |||
821 | /** |
||
822 | * Gets the list of associated renditions for the specified object. |
||
823 | * Only rendition attributes are returned, not rendition stream. |
||
824 | * |
||
825 | * @param string $repositoryId the identifier for the repository |
||
826 | * @param string $objectId the identifier for the object |
||
827 | * @param string $renditionFilter indicates what set of renditions the repository must return whose kind |
||
828 | * matches this filter (default is "cmis:none") |
||
829 | * @param integer|null $maxItems the maximum number of items to return in a response |
||
830 | * (default is repository specific) |
||
831 | * @param integer $skipCount number of potential results that the repository MUST skip/page over before |
||
832 | * returning any results (default is 0) |
||
833 | * @param ExtensionDataInterface|null $extension |
||
834 | * @return RenditionDataInterface[] |
||
835 | * @throws CmisInvalidArgumentException If object id is empty or skip count not of type integer |
||
836 | */ |
||
837 | 2 | public function getRenditions( |
|
869 | |||
870 | /** |
||
871 | * Moves the specified file-able object from one folder to another. |
||
872 | * |
||
873 | * @param string $repositoryId the identifier for the repository |
||
874 | * @param string $objectId the identifier for the object. The repository might return a different/new object id |
||
875 | * @param string $targetFolderId the identifier for the target folder |
||
876 | * @param string $sourceFolderId the identifier for the source folder |
||
877 | * @param ExtensionDataInterface|null $extension |
||
878 | * @return ObjectDataInterface|null Returns object of type ObjectDataInterface or <code>null</code> |
||
879 | * if the repository response was empty |
||
880 | */ |
||
881 | 1 | public function moveObject( |
|
908 | |||
909 | /** |
||
910 | * Sets the content stream for the specified document object. |
||
911 | * |
||
912 | * @param string $repositoryId The identifier for the repository |
||
913 | * @param string $objectId The identifier for the object. The repository might return a different/new object id |
||
914 | * @param StreamInterface $contentStream The content stream |
||
915 | * @param boolean $overwriteFlag If <code>true</code>, then the repository must replace the existing content stream |
||
916 | * for the object (if any) with the input content stream. If <code>false</code>, then the repository must |
||
917 | * only set the input content stream for the object if the object currently does not have a content stream |
||
918 | * (default is <code>true</code>) |
||
919 | * @param string|null $changeToken The last change token of this object that the client received. |
||
920 | * The repository might return a new change token (default is <code>null</code>) |
||
921 | * @param ExtensionDataInterface|null $extension |
||
922 | * @throws CmisInvalidArgumentException If object id is empty |
||
923 | */ |
||
924 | 3 | public function setContentStream( |
|
971 | |||
972 | /** |
||
973 | * Updates properties of the specified object. |
||
974 | * |
||
975 | * @param string $repositoryId The identifier for the repository |
||
976 | * @param string $objectId The identifier for the object. The repository might return a different/new object id |
||
977 | * @param PropertiesInterface $properties The updated property values that must be applied to the object |
||
978 | * @param string|null $changeToken The last change token of this object that the client received. |
||
979 | * The repository might return a new change token (default is <code>null</code>) |
||
980 | * @param ExtensionDataInterface|null $extension |
||
981 | * @throws CmisInvalidArgumentException If $objectId is empty |
||
982 | */ |
||
983 | 3 | public function updateProperties( |
|
1020 | |||
1021 | /** |
||
1022 | * @param string $identifier |
||
1023 | * @param mixed $additionalHashValues |
||
1024 | * @return array |
||
1025 | */ |
||
1026 | 8 | protected function createCacheKey($identifier, $additionalHashValues) |
|
1033 | |||
1034 | /** |
||
1035 | * Returns TRUE if an object with cache key $identifier is currently cached. |
||
1036 | * |
||
1037 | * @param array $identifier |
||
1038 | * @return boolean |
||
1039 | */ |
||
1040 | 8 | protected function isCached(array $identifier) |
|
1044 | |||
1045 | /** |
||
1046 | * Gets the cached object with cache key $identifier. |
||
1047 | * |
||
1048 | * @param string $identifier |
||
1049 | * @return mixed |
||
1050 | */ |
||
1051 | protected function getCached(array $identifier) |
||
1058 | |||
1059 | /** |
||
1060 | * Gets the cached object with cache key $identifier. |
||
1061 | * |
||
1062 | * @param string $identifier |
||
1063 | * @param mixed $object |
||
1064 | * @return mixed |
||
1065 | */ |
||
1066 | 8 | protected function cache(array $identifier, $object) |
|
1071 | |||
1072 | /** |
||
1073 | * Flushes all cached entries. This is implemented as a flush-all with |
||
1074 | * no way to flush individual entries due to the way CMIS object data |
||
1075 | * gets returned from CMIS. Two widely different object data sets may |
||
1076 | * contain a reference to the same item and even with extensive cross |
||
1077 | * referencing it would be technically unfeasible to selectively clear |
||
1078 | * or reload an object by identifier. Such flushing would be inevitably |
||
1079 | * flawed with edge cases of incomplete flushing or become so complex |
||
1080 | * that it defeats the purpose of caching in the first place. |
||
1081 | * |
||
1082 | * Note that cache flushing only happens when modifying the repository |
||
1083 | * contents - which should limit the negative impact. The cache is also |
||
1084 | * not persistent and will only affect the current request. As such, it |
||
1085 | * is implemented to optimise requests where the same object, type, |
||
1086 | * policy etc. gets accessed multiple times. |
||
1087 | * |
||
1088 | * @return void |
||
1089 | */ |
||
1090 | 12 | protected function flushCached() |
|
1094 | } |
||
1095 |
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.