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 | * Appends the content stream to the content of the document. |
||
41 | * |
||
42 | * The stream in contentStream is consumed but not closed by this method. |
||
43 | * |
||
44 | * @param string $repositoryId the identifier for the repository |
||
45 | * @param string $objectId The identifier for the object. The repository might return a different/new object id |
||
46 | * @param StreamInterface $contentStream The content stream to append |
||
47 | * @param boolean $isLastChunk Indicates if this content stream is the last chunk |
||
48 | * @param string|null $changeToken The last change token of this object that the client received. |
||
49 | * The repository might return a new change token (default is <code>null</code>) |
||
50 | * @param ExtensionDataInterface|null $extension |
||
51 | */ |
||
52 | public function appendContentStream( |
||
62 | |||
63 | /** |
||
64 | * Updates properties and secondary types of one or more objects. |
||
65 | * |
||
66 | * @param string $repositoryId the identifier for the repository |
||
67 | * @param BulkUpdateObjectIdAndChangeTokenInterface[] $objectIdsAndChangeTokens |
||
68 | * @param PropertiesInterface $properties |
||
69 | * @param string[] $addSecondaryTypeIds the secondary types to apply |
||
70 | * @param string[] $removeSecondaryTypeIds the secondary types to remove |
||
71 | * @param ExtensionDataInterface|null $extension |
||
72 | * @return BulkUpdateObjectIdAndChangeTokenInterface[] |
||
73 | */ |
||
74 | public function bulkUpdateProperties( |
||
84 | |||
85 | /** |
||
86 | * @param string $action |
||
87 | * @param PropertiesInterface $properties |
||
88 | * @param string[] $policies |
||
89 | * @param AclInterface $addAces |
||
|
|||
90 | * @param AclInterface $removeAces |
||
91 | * @param ExtensionDataInterface $extension |
||
92 | * @return array |
||
93 | */ |
||
94 | 9 | protected function createQueryArray( |
|
95 | $action, |
||
96 | PropertiesInterface $properties, |
||
97 | array $policies = array(), |
||
98 | AclInterface $addAces = null, |
||
99 | AclInterface $removeAces = null, |
||
100 | ExtensionDataInterface $extension = null |
||
101 | ) { |
||
102 | 9 | $queryArray = array_replace( |
|
103 | array( |
||
104 | 9 | Constants::CONTROL_CMISACTION => $action, |
|
105 | 9 | Constants::PARAM_SUCCINCT => $this->getSuccinct() ? 'true' : 'false', |
|
106 | 9 | ), |
|
107 | 9 | $this->convertPropertiesToQueryArray($properties), |
|
108 | 9 | $this->convertPolicyIdArrayToQueryArray($policies) |
|
109 | 9 | ); |
|
110 | 9 | if (!empty($removeAces)) { |
|
111 | 5 | $queryArray = array_replace($queryArray, $this->convertAclToQueryArray( |
|
112 | 5 | $removeAces, |
|
113 | 5 | Constants::CONTROL_REMOVE_ACE_PRINCIPAL, |
|
114 | Constants::CONTROL_REMOVE_ACE_PERMISSION |
||
115 | 5 | )); |
|
116 | 5 | } |
|
117 | 9 | if (!empty($addAces)) { |
|
118 | 5 | $queryArray = array_replace($queryArray, $this->convertAclToQueryArray( |
|
119 | 5 | $addAces, |
|
120 | 5 | Constants::CONTROL_ADD_ACE_PRINCIPAL, |
|
121 | Constants::CONTROL_ADD_ACE_PERMISSION |
||
122 | 5 | )); |
|
123 | 5 | } |
|
124 | 9 | return $queryArray; |
|
125 | } |
||
126 | |||
127 | /** |
||
128 | * Creates a document object of the specified type (given by the cmis:objectTypeId property) |
||
129 | * in the (optionally) specified location. |
||
130 | * |
||
131 | * @param string $repositoryId the identifier for the repository |
||
132 | * @param PropertiesInterface $properties the property values that must be applied to the newly |
||
133 | * created document object |
||
134 | * @param string|null $folderId if specified, the identifier for the folder that must be the parent |
||
135 | * folder for the newly created document object |
||
136 | * @param StreamInterface|null $contentStream the content stream that must be stored for the newly |
||
137 | * created document object |
||
138 | * @param VersioningState|null $versioningState specifies what the versioning state of the newly created object |
||
139 | * must be (default is <code>VersioningState::MAJOR</code>) |
||
140 | * @param string[] $policies a list of policy IDs that must be applied to the newly created document object |
||
141 | * @param AclInterface|null $addAces a list of ACEs that must be added to the newly created document object, |
||
142 | * either using the ACL from folderId if specified, or being applied if no folderId is specified |
||
143 | * @param AclInterface|null $removeAces a list of ACEs that must be removed from the newly created document object, |
||
144 | * either using the ACL from folderId if specified, or being ignored if no folderId is specified |
||
145 | * @param ExtensionDataInterface|null $extension |
||
146 | * @return string|null Returns the new object id or <code>null</code> if the repository sent an empty |
||
147 | * result (which should not happen) |
||
148 | */ |
||
149 | 3 | public function createDocument( |
|
201 | |||
202 | /** |
||
203 | * Creates a document object as a copy of the given source document in the (optionally) specified location. |
||
204 | * |
||
205 | * @param string $repositoryId the identifier for the repository |
||
206 | * @param string $sourceId the identifier for the source document |
||
207 | * @param PropertiesInterface $properties the property values that must be applied to the newly |
||
208 | * created document object |
||
209 | * @param string|null $folderId if specified, the identifier for the folder that must be the parent folder for the |
||
210 | * newly created document object |
||
211 | * @param VersioningState|null $versioningState specifies what the versioning state of the newly created object |
||
212 | * must be (default is <code>VersioningState::MAJOR</code>) |
||
213 | * @param string[] $policies a list of policy IDs that must be applied to the newly created document object |
||
214 | * @param AclInterface|null $addAces a list of ACEs that must be added to the newly created document object, |
||
215 | * either using the ACL from folderId if specified, or being applied if no folderId is specified |
||
216 | * @param AclInterface|null $removeAces a list of ACEs that must be removed from the newly created document object, |
||
217 | * either using the ACL from folderId if specified, or being ignored if no folderId is specified |
||
218 | * @param ExtensionDataInterface|null $extension |
||
219 | * @return string|null Returns the new object id or <code>null</code> if the repository sent an empty |
||
220 | * result (which should not happen) |
||
221 | */ |
||
222 | 2 | public function createDocumentFromSource( |
|
257 | |||
258 | /** |
||
259 | * Creates a folder object of the specified type (given by the cmis:objectTypeId property) in |
||
260 | * the specified location. |
||
261 | * |
||
262 | * @param string $repositoryId the identifier for the repository |
||
263 | * @param PropertiesInterface $properties the property values that must be applied to the newly |
||
264 | * created document object |
||
265 | * @param string $folderId if specified, the identifier for the folder that must be the parent folder for the |
||
266 | * newly created document object |
||
267 | * @param string[] $policies a list of policy IDs that must be applied to the newly created document object |
||
268 | * @param AclInterface|null $addAces a list of ACEs that must be added to the newly created document object, |
||
269 | * either using the ACL from folderId if specified, or being applied if no folderId is specified |
||
270 | * @param AclInterface|null $removeAces a list of ACEs that must be removed from the newly created document object, |
||
271 | * either using the ACL from folderId if specified, or being ignored if no folderId is specified |
||
272 | * @param ExtensionDataInterface|null $extension |
||
273 | * @return string|null Returns the new object id or <code>null</code> if the repository sent an empty |
||
274 | * result (which should not happen) |
||
275 | */ |
||
276 | 2 | View Code Duplication | public function createFolder( |
301 | |||
302 | /** |
||
303 | * Creates an item object of the specified type (given by the cmis:objectTypeId property). |
||
304 | * |
||
305 | * @param string $repositoryId The identifier for the repository |
||
306 | * @param PropertiesInterface $properties The property values that must be applied to the newly |
||
307 | * created document object |
||
308 | * @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 | 2 | public function createItem( |
|
349 | |||
350 | /** |
||
351 | * Creates a policy object of the specified type (given by the cmis:objectTypeId property). |
||
352 | * |
||
353 | * @param string $repositoryId The identifier for the repository |
||
354 | * @param PropertiesInterface $properties The property values that must be applied to the newly |
||
355 | * created document object |
||
356 | * @param string|null $folderId If specified, the identifier for the folder that must be the parent folder for the |
||
357 | * newly created document object |
||
358 | * @param string[] $policies A list of policy IDs that must be applied to the newly created document object |
||
359 | * @param AclInterface|null $addAces A list of ACEs that must be added to the newly created document object, |
||
360 | * either using the ACL from folderId if specified, or being applied if no folderId is specified |
||
361 | * @param AclInterface|null $removeAces A list of ACEs that must be removed from the newly created document object, |
||
362 | * either using the ACL from folderId if specified, or being ignored if no folderId is specified |
||
363 | * @param ExtensionDataInterface|null $extension |
||
364 | * @return string The id of the newly-created policy. |
||
365 | */ |
||
366 | public function createPolicy( |
||
377 | |||
378 | /** |
||
379 | * Creates a relationship object of the specified type (given by the cmis:objectTypeId property). |
||
380 | * |
||
381 | * @param string $repositoryId the identifier for the repository |
||
382 | * @param PropertiesInterface $properties the property values that must be applied to the newly |
||
383 | * created document object |
||
384 | * @param string[] $policies a list of policy IDs that must be applied to the newly created document object |
||
385 | * @param AclInterface|null $addAces a list of ACEs that must be added to the newly created document object, |
||
386 | * either using the ACL from folderId if specified, or being applied if no folderId is specified |
||
387 | * @param AclInterface|null $removeAces a list of ACEs that must be removed from the newly created document object, |
||
388 | * either using the ACL from folderId if specified, or being ignored if no folderId is specified |
||
389 | * @param ExtensionDataInterface|null $extension |
||
390 | * @return string|null Returns the new item id of the relationship object or <code>null</code> if the repository |
||
391 | * sent an empty result (which should not happen) |
||
392 | */ |
||
393 | View Code Duplication | public function createRelationship( |
|
418 | |||
419 | /** |
||
420 | * Deletes the content stream for the specified document object. |
||
421 | * |
||
422 | * @param string $repositoryId the identifier for the repository |
||
423 | * @param string $objectId the identifier for the object. The repository might return a different/new object id |
||
424 | * @param string|null $changeToken the last change token of this object that the client received. |
||
425 | * The repository might return a new change token (default is <code>null</code>) |
||
426 | * @param ExtensionDataInterface|null $extension |
||
427 | * @throws CmisInvalidArgumentException If $objectId is empty |
||
428 | */ |
||
429 | 3 | public function deleteContentStream( |
|
467 | |||
468 | /** |
||
469 | * Deletes the specified object. |
||
470 | * |
||
471 | * @param string $repositoryId the identifier for the repository |
||
472 | * @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 | * the document object specified (default is <code>true</code>) |
||
475 | * @param ExtensionDataInterface|null $extension |
||
476 | */ |
||
477 | 2 | public function deleteObject( |
|
493 | |||
494 | /** |
||
495 | * Deletes the specified folder object and all of its child- and descendant-objects. |
||
496 | * |
||
497 | * @param string $repositoryId the identifier for the repository |
||
498 | * @param string $folderId the identifier for the folder |
||
499 | * @param boolean $allVersions If <code>true</code> then delete all versions of the document, otherwise delete only |
||
500 | * the document object specified (default is <code>true</code>) |
||
501 | * @param UnfileObject|null $unfileObjects defines how the repository must process file-able child- or |
||
502 | * descendant-objects (default is <code>UnfileObject::DELETE</code>) |
||
503 | * @param boolean $continueOnFailure If <code>true</code>, then the repository should continue attempting to |
||
504 | * perform this operation even if deletion of a child- or descendant-object in the specified folder cannot |
||
505 | * be deleted |
||
506 | * @param ExtensionDataInterface|null $extension |
||
507 | * @return FailedToDeleteDataInterface Returns a list of object ids that could not be deleted |
||
508 | */ |
||
509 | 4 | public function deleteTree( |
|
535 | |||
536 | /** |
||
537 | * Gets the list of allowable actions for an object. |
||
538 | * |
||
539 | * @param string $repositoryId the identifier for the repository |
||
540 | * @param string $objectId the identifier for the object |
||
541 | * @param ExtensionDataInterface|null $extension |
||
542 | * @return AllowableActionsInterface |
||
543 | */ |
||
544 | public function getAllowableActions($repositoryId, $objectId, ExtensionDataInterface $extension = null) |
||
548 | |||
549 | /** |
||
550 | * Gets the content stream for the specified document object, or gets a rendition stream for |
||
551 | * a specified rendition of a document or folder object. |
||
552 | * |
||
553 | * @param string $repositoryId the identifier for the repository |
||
554 | * @param string $objectId the identifier for the object |
||
555 | * @param string|null $streamId The identifier for the rendition stream, when used to get a rendition stream. |
||
556 | * For documents, if not provided then this method returns the content stream. For folders, |
||
557 | * it MUST be provided. |
||
558 | * @param integer|null $offset |
||
559 | * @param integer|null $length |
||
560 | * @param ExtensionDataInterface|null $extension |
||
561 | * @return StreamInterface|null |
||
562 | * @throws CmisInvalidArgumentException If object id is empty |
||
563 | */ |
||
564 | 3 | public function getContentStream( |
|
596 | |||
597 | /** |
||
598 | * Gets the specified information for the object specified by id. |
||
599 | * |
||
600 | * @param string $repositoryId the identifier for the repository |
||
601 | * @param string $objectId the identifier for the object |
||
602 | * @param string|null $filter a comma-separated list of query names that defines which properties must be |
||
603 | * returned by the repository (default is repository specific) |
||
604 | * @param boolean $includeAllowableActions if <code>true</code>, then the repository must return the allowable |
||
605 | * actions for the object (default is <code>false</code>) |
||
606 | * @param IncludeRelationships|null $includeRelationships indicates what relationships in which the objects |
||
607 | * participate must be returned (default is <code>IncludeRelationships::NONE</code>) |
||
608 | * @param string $renditionFilter indicates what set of renditions the repository must return whose kind |
||
609 | * matches this filter (default is "cmis:none") |
||
610 | * @param boolean $includePolicyIds if <code>true</code>, then the repository must return the policy ids for |
||
611 | * the object (default is <code>false</code>) |
||
612 | * @param boolean $includeAcl if <code>true</code>, then the repository must return the ACL for the object |
||
613 | * (default is <code>false</code>) |
||
614 | * @param ExtensionDataInterface|null $extension |
||
615 | * @return ObjectDataInterface|null Returns object of type ObjectDataInterface or <code>null</code> |
||
616 | * if the repository response was empty |
||
617 | */ |
||
618 | 3 | View Code Duplication | public function getObject( |
654 | |||
655 | /** |
||
656 | * Gets the specified information for the object specified by path. |
||
657 | * |
||
658 | * @param string $repositoryId the identifier for the repository |
||
659 | * @param string $path the path to the object |
||
660 | * @param string|null $filter a comma-separated list of query names that defines which properties must be |
||
661 | * returned by the repository (default is repository specific) |
||
662 | * @param boolean $includeAllowableActions if <code>true</code>, then the repository must return the allowable |
||
663 | * actions for the object (default is <code>false</code>) |
||
664 | * @param IncludeRelationships|null $includeRelationships indicates what relationships in which the objects |
||
665 | * participate must be returned (default is <code>IncludeRelationships::NONE</code>) |
||
666 | * @param string $renditionFilter indicates what set of renditions the repository must return whose kind |
||
667 | * matches this filter (default is "cmis:none") |
||
668 | * @param boolean $includePolicyIds if <code>true</code>, then the repository must return the policy ids for |
||
669 | * the object (default is <code>false</code>) |
||
670 | * @param boolean $includeAcl if <code>true</code>, then the repository must return the ACL for the object |
||
671 | * (default is <code>false</code>) |
||
672 | * @param ExtensionDataInterface|null $extension |
||
673 | * @return ObjectDataInterface|null Returns object of type <code>ObjectDataInterface</code> or <code>null</code> |
||
674 | * if the repository response was empty |
||
675 | */ |
||
676 | 3 | View Code Duplication | public function getObjectByPath( |
712 | |||
713 | /** |
||
714 | * Gets the list of properties for an object. |
||
715 | * |
||
716 | * @param string $repositoryId the identifier for the repository |
||
717 | * @param string $objectId the identifier for the object |
||
718 | * @param string|null $filter a comma-separated list of query names that defines which properties must be |
||
719 | * returned by the repository (default is repository specific) |
||
720 | * @param ExtensionDataInterface|null $extension |
||
721 | * @return PropertiesInterface |
||
722 | */ |
||
723 | 2 | public function getProperties( |
|
750 | |||
751 | /** |
||
752 | * Gets the list of associated renditions for the specified object. |
||
753 | * Only rendition attributes are returned, not rendition stream. |
||
754 | * |
||
755 | * @param string $repositoryId the identifier for the repository |
||
756 | * @param string $objectId the identifier for the object |
||
757 | * @param string $renditionFilter indicates what set of renditions the repository must return whose kind |
||
758 | * matches this filter (default is "cmis:none") |
||
759 | * @param integer|null $maxItems the maximum number of items to return in a response |
||
760 | * (default is repository specific) |
||
761 | * @param integer $skipCount number of potential results that the repository MUST skip/page over before |
||
762 | * returning any results (default is 0) |
||
763 | * @param ExtensionDataInterface|null $extension |
||
764 | * @return RenditionDataInterface[] |
||
765 | * @throws CmisInvalidArgumentException If object id is empty or skip count not of type integer |
||
766 | */ |
||
767 | 2 | public function getRenditions( |
|
799 | |||
800 | /** |
||
801 | * Moves the specified file-able object from one folder to another. |
||
802 | * |
||
803 | * @param string $repositoryId the identifier for the repository |
||
804 | * @param string $objectId the identifier for the object. The repository might return a different/new object id |
||
805 | * @param string $targetFolderId the identifier for the target folder |
||
806 | * @param string $sourceFolderId the identifier for the source folder |
||
807 | * @param ExtensionDataInterface|null $extension |
||
808 | * @return ObjectDataInterface|null Returns object of type ObjectDataInterface or <code>null</code> |
||
809 | * if the repository response was empty |
||
810 | */ |
||
811 | 1 | public function moveObject( |
|
836 | |||
837 | /** |
||
838 | * Sets the content stream for the specified document object. |
||
839 | * |
||
840 | * @param string $repositoryId The identifier for the repository |
||
841 | * @param string $objectId The identifier for the object. The repository might return a different/new object id |
||
842 | * @param StreamInterface $contentStream The content stream |
||
843 | * @param boolean $overwriteFlag If <code>true</code>, then the repository must replace the existing content stream |
||
844 | * for the object (if any) with the input content stream. If <code>false</code>, then the repository must |
||
845 | * only set the input content stream for the object if the object currently does not have a content stream |
||
846 | * (default is <code>true</code>) |
||
847 | * @param string|null $changeToken The last change token of this object that the client received. |
||
848 | * The repository might return a new change token (default is <code>null</code>) |
||
849 | * @param ExtensionDataInterface|null $extension |
||
850 | * @throws CmisInvalidArgumentException If object id is empty |
||
851 | */ |
||
852 | 3 | public function setContentStream( |
|
897 | |||
898 | /** |
||
899 | * Updates properties of the specified object. |
||
900 | * |
||
901 | * @param string $repositoryId The identifier for the repository |
||
902 | * @param string $objectId The identifier for the object. The repository might return a different/new object id |
||
903 | * @param PropertiesInterface $properties The updated property values that must be applied to the object |
||
904 | * @param string|null $changeToken The last change token of this object that the client received. |
||
905 | * The repository might return a new change token (default is <code>null</code>) |
||
906 | * @param ExtensionDataInterface|null $extension |
||
907 | * @throws CmisInvalidArgumentException If $objectId is empty |
||
908 | */ |
||
909 | 3 | public function updateProperties( |
|
944 | } |
||
945 |
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.