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 Manager 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 Manager, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 42 | class Manager implements ICommentsManager { |
||
| 43 | |||
| 44 | /** @var IDBConnection */ |
||
| 45 | protected $dbConn; |
||
| 46 | |||
| 47 | /** @var ILogger */ |
||
| 48 | protected $logger; |
||
| 49 | |||
| 50 | /** @var IConfig */ |
||
| 51 | protected $config; |
||
| 52 | |||
| 53 | /** @var IComment[] */ |
||
| 54 | protected $commentsCache = []; |
||
| 55 | |||
| 56 | /** @var \Closure[] */ |
||
| 57 | protected $eventHandlerClosures = []; |
||
| 58 | |||
| 59 | /** @var ICommentsEventHandler[] */ |
||
| 60 | protected $eventHandlers = []; |
||
| 61 | |||
| 62 | /** @var \Closure[] */ |
||
| 63 | protected $displayNameResolvers = []; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Manager constructor. |
||
| 67 | * |
||
| 68 | * @param IDBConnection $dbConn |
||
| 69 | * @param ILogger $logger |
||
| 70 | * @param IConfig $config |
||
| 71 | */ |
||
| 72 | public function __construct( |
||
| 81 | |||
| 82 | /** |
||
| 83 | * converts data base data into PHP native, proper types as defined by |
||
| 84 | * IComment interface. |
||
| 85 | * |
||
| 86 | * @param array $data |
||
| 87 | * @return array |
||
| 88 | */ |
||
| 89 | protected function normalizeDatabaseData(array $data) { |
||
| 90 | $data['id'] = (string)$data['id']; |
||
| 91 | $data['parent_id'] = (string)$data['parent_id']; |
||
| 92 | $data['topmost_parent_id'] = (string)$data['topmost_parent_id']; |
||
| 93 | $data['creation_timestamp'] = new \DateTime($data['creation_timestamp']); |
||
| 94 | View Code Duplication | if (!is_null($data['latest_child_timestamp'])) { |
|
| 95 | $data['latest_child_timestamp'] = new \DateTime($data['latest_child_timestamp']); |
||
| 96 | } |
||
| 97 | $data['children_count'] = (int)$data['children_count']; |
||
| 98 | return $data; |
||
| 99 | } |
||
| 100 | |||
| 101 | /** |
||
| 102 | * prepares a comment for an insert or update operation after making sure |
||
| 103 | * all necessary fields have a value assigned. |
||
| 104 | * |
||
| 105 | * @param IComment $comment |
||
| 106 | * @return IComment returns the same updated IComment instance as provided |
||
| 107 | * by parameter for convenience |
||
| 108 | * @throws \UnexpectedValueException |
||
| 109 | */ |
||
| 110 | protected function prepareCommentForDatabaseWrite(IComment $comment) { |
||
| 140 | |||
| 141 | /** |
||
| 142 | * returns the topmost parent id of a given comment identified by ID |
||
| 143 | * |
||
| 144 | * @param string $id |
||
| 145 | * @return string |
||
| 146 | * @throws NotFoundException |
||
| 147 | */ |
||
| 148 | protected function determineTopmostParentId($id) { |
||
| 156 | |||
| 157 | /** |
||
| 158 | * updates child information of a comment |
||
| 159 | * |
||
| 160 | * @param string $id |
||
| 161 | * @param \DateTime $cDateTime the date time of the most recent child |
||
| 162 | * @throws NotFoundException |
||
| 163 | */ |
||
| 164 | protected function updateChildrenInformation($id, \DateTime $cDateTime) { |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Tests whether actor or object type and id parameters are acceptable. |
||
| 184 | * Throws exception if not. |
||
| 185 | * |
||
| 186 | * @param string $role |
||
| 187 | * @param string $type |
||
| 188 | * @param string $id |
||
| 189 | * @throws \InvalidArgumentException |
||
| 190 | */ |
||
| 191 | protected function checkRoleParameters($role, $type, $id) { |
||
| 199 | |||
| 200 | /** |
||
| 201 | * run-time caches a comment |
||
| 202 | * |
||
| 203 | * @param IComment $comment |
||
| 204 | */ |
||
| 205 | protected function cache(IComment $comment) { |
||
| 212 | |||
| 213 | /** |
||
| 214 | * removes an entry from the comments run time cache |
||
| 215 | * |
||
| 216 | * @param mixed $id the comment's id |
||
| 217 | */ |
||
| 218 | protected function uncache($id) { |
||
| 224 | |||
| 225 | /** |
||
| 226 | * returns a comment instance |
||
| 227 | * |
||
| 228 | * @param string $id the ID of the comment |
||
| 229 | * @return IComment |
||
| 230 | * @throws NotFoundException |
||
| 231 | * @throws \InvalidArgumentException |
||
| 232 | * @since 9.0.0 |
||
| 233 | */ |
||
| 234 | public function get($id) { |
||
| 260 | |||
| 261 | /** |
||
| 262 | * returns the comment specified by the id and all it's child comments. |
||
| 263 | * At this point of time, we do only support one level depth. |
||
| 264 | * |
||
| 265 | * @param string $id |
||
| 266 | * @param int $limit max number of entries to return, 0 returns all |
||
| 267 | * @param int $offset the start entry |
||
| 268 | * @return array |
||
| 269 | * @since 9.0.0 |
||
| 270 | * |
||
| 271 | * The return array looks like this |
||
| 272 | * [ |
||
| 273 | * 'comment' => IComment, // root comment |
||
| 274 | * 'replies' => |
||
| 275 | * [ |
||
| 276 | * 0 => |
||
| 277 | * [ |
||
| 278 | * 'comment' => IComment, |
||
| 279 | * 'replies' => [] |
||
| 280 | * ] |
||
| 281 | * 1 => |
||
| 282 | * [ |
||
| 283 | * 'comment' => IComment, |
||
| 284 | * 'replies'=> [] |
||
| 285 | * ], |
||
| 286 | * … |
||
| 287 | * ] |
||
| 288 | * ] |
||
| 289 | */ |
||
| 290 | public function getTree($id, $limit = 0, $offset = 0) { |
||
| 322 | |||
| 323 | /** |
||
| 324 | * returns comments for a specific object (e.g. a file). |
||
| 325 | * |
||
| 326 | * The sort order is always newest to oldest. |
||
| 327 | * |
||
| 328 | * @param string $objectType the object type, e.g. 'files' |
||
| 329 | * @param string $objectId the id of the object |
||
| 330 | * @param int $limit optional, number of maximum comments to be returned. if |
||
| 331 | * not specified, all comments are returned. |
||
| 332 | * @param int $offset optional, starting point |
||
| 333 | * @param \DateTime $notOlderThan optional, timestamp of the oldest comments |
||
| 334 | * that may be returned |
||
| 335 | * @return IComment[] |
||
| 336 | * @since 9.0.0 |
||
| 337 | */ |
||
| 338 | public function getForObject( |
||
| 378 | |||
| 379 | /** |
||
| 380 | * @param string $objectType the object type, e.g. 'files' |
||
| 381 | * @param string $objectId the id of the object |
||
| 382 | * @param int $lastKnownCommentId the last known comment (will be used as offset) |
||
| 383 | * @param string $sortDirection direction of the comments (`asc` or `desc`) |
||
| 384 | * @param int $limit optional, number of maximum comments to be returned. if |
||
| 385 | * set to 0, all comments are returned. |
||
| 386 | * @return IComment[] |
||
| 387 | * @return array |
||
| 388 | */ |
||
| 389 | public function getForObjectSince( |
||
| 466 | |||
| 467 | /** |
||
| 468 | * @param string $objectType the object type, e.g. 'files' |
||
| 469 | * @param string $objectId the id of the object |
||
| 470 | * @param int $id the comment to look for |
||
| 471 | * @return Comment|null |
||
| 472 | */ |
||
| 473 | protected function getLastKnownComment(string $objectType, |
||
| 495 | |||
| 496 | /** |
||
| 497 | * Search for comments with a given content |
||
| 498 | * |
||
| 499 | * @param string $search content to search for |
||
| 500 | * @param string $objectType Limit the search by object type |
||
| 501 | * @param string $objectId Limit the search by object id |
||
| 502 | * @param string $verb Limit the verb of the comment |
||
| 503 | * @param int $offset |
||
| 504 | * @param int $limit |
||
| 505 | * @return IComment[] |
||
| 506 | */ |
||
| 507 | public function search(string $search, string $objectType, string $objectId, string $verb, int $offset, int $limit = 50): array { |
||
| 543 | |||
| 544 | /** |
||
| 545 | * @param $objectType string the object type, e.g. 'files' |
||
| 546 | * @param $objectId string the id of the object |
||
| 547 | * @param \DateTime $notOlderThan optional, timestamp of the oldest comments |
||
| 548 | * that may be returned |
||
| 549 | * @return Int |
||
| 550 | * @since 9.0.0 |
||
| 551 | */ |
||
| 552 | public function getNumberOfCommentsForObject($objectType, $objectId, \DateTime $notOlderThan = null) { |
||
| 572 | |||
| 573 | /** |
||
| 574 | * Get the number of unread comments for all files in a folder |
||
| 575 | * |
||
| 576 | * @param int $folderId |
||
| 577 | * @param IUser $user |
||
| 578 | * @return array [$fileId => $unreadCount] |
||
| 579 | */ |
||
| 580 | public function getNumberOfUnreadCommentsForFolder($folderId, IUser $user) { |
||
| 613 | |||
| 614 | /** |
||
| 615 | * creates a new comment and returns it. At this point of time, it is not |
||
| 616 | * saved in the used data storage. Use save() after setting other fields |
||
| 617 | * of the comment (e.g. message or verb). |
||
| 618 | * |
||
| 619 | * @param string $actorType the actor type (e.g. 'users') |
||
| 620 | * @param string $actorId a user id |
||
| 621 | * @param string $objectType the object type the comment is attached to |
||
| 622 | * @param string $objectId the object id the comment is attached to |
||
| 623 | * @return IComment |
||
| 624 | * @since 9.0.0 |
||
| 625 | */ |
||
| 626 | public function create($actorType, $actorId, $objectType, $objectId) { |
||
| 633 | |||
| 634 | /** |
||
| 635 | * permanently deletes the comment specified by the ID |
||
| 636 | * |
||
| 637 | * When the comment has child comments, their parent ID will be changed to |
||
| 638 | * the parent ID of the item that is to be deleted. |
||
| 639 | * |
||
| 640 | * @param string $id |
||
| 641 | * @return bool |
||
| 642 | * @throws \InvalidArgumentException |
||
| 643 | * @since 9.0.0 |
||
| 644 | */ |
||
| 645 | public function delete($id) { |
||
| 676 | |||
| 677 | /** |
||
| 678 | * saves the comment permanently |
||
| 679 | * |
||
| 680 | * if the supplied comment has an empty ID, a new entry comment will be |
||
| 681 | * saved and the instance updated with the new ID. |
||
| 682 | * |
||
| 683 | * Otherwise, an existing comment will be updated. |
||
| 684 | * |
||
| 685 | * Throws NotFoundException when a comment that is to be updated does not |
||
| 686 | * exist anymore at this point of time. |
||
| 687 | * |
||
| 688 | * @param IComment $comment |
||
| 689 | * @return bool |
||
| 690 | * @throws NotFoundException |
||
| 691 | * @since 9.0.0 |
||
| 692 | */ |
||
| 693 | public function save(IComment $comment) { |
||
| 710 | |||
| 711 | /** |
||
| 712 | * inserts the provided comment in the database |
||
| 713 | * |
||
| 714 | * @param IComment $comment |
||
| 715 | * @return bool |
||
| 716 | */ |
||
| 717 | protected function insert(IComment &$comment) { |
||
| 743 | |||
| 744 | /** |
||
| 745 | * updates a Comment data row |
||
| 746 | * |
||
| 747 | * @param IComment $comment |
||
| 748 | * @return bool |
||
| 749 | * @throws NotFoundException |
||
| 750 | */ |
||
| 751 | protected function update(IComment $comment) { |
||
| 784 | |||
| 785 | /** |
||
| 786 | * removes references to specific actor (e.g. on user delete) of a comment. |
||
| 787 | * The comment itself must not get lost/deleted. |
||
| 788 | * |
||
| 789 | * @param string $actorType the actor type (e.g. 'users') |
||
| 790 | * @param string $actorId a user id |
||
| 791 | * @return boolean |
||
| 792 | * @since 9.0.0 |
||
| 793 | */ |
||
| 794 | public function deleteReferencesOfActor($actorType, $actorId) { |
||
| 812 | |||
| 813 | /** |
||
| 814 | * deletes all comments made of a specific object (e.g. on file delete) |
||
| 815 | * |
||
| 816 | * @param string $objectType the object type (e.g. 'files') |
||
| 817 | * @param string $objectId e.g. the file id |
||
| 818 | * @return boolean |
||
| 819 | * @since 9.0.0 |
||
| 820 | */ |
||
| 821 | public function deleteCommentsAtObject($objectType, $objectId) { |
||
| 837 | |||
| 838 | /** |
||
| 839 | * deletes the read markers for the specified user |
||
| 840 | * |
||
| 841 | * @param \OCP\IUser $user |
||
| 842 | * @return bool |
||
| 843 | * @since 9.0.0 |
||
| 844 | */ |
||
| 845 | public function deleteReadMarksFromUser(IUser $user) { |
||
| 859 | |||
| 860 | /** |
||
| 861 | * sets the read marker for a given file to the specified date for the |
||
| 862 | * provided user |
||
| 863 | * |
||
| 864 | * @param string $objectType |
||
| 865 | * @param string $objectId |
||
| 866 | * @param \DateTime $dateTime |
||
| 867 | * @param IUser $user |
||
| 868 | * @since 9.0.0 |
||
| 869 | * @suppress SqlInjectionChecker |
||
| 870 | */ |
||
| 871 | public function setReadMark($objectType, $objectId, \DateTime $dateTime, IUser $user) { |
||
| 905 | |||
| 906 | /** |
||
| 907 | * returns the read marker for a given file to the specified date for the |
||
| 908 | * provided user. It returns null, when the marker is not present, i.e. |
||
| 909 | * no comments were marked as read. |
||
| 910 | * |
||
| 911 | * @param string $objectType |
||
| 912 | * @param string $objectId |
||
| 913 | * @param IUser $user |
||
| 914 | * @return \DateTime|null |
||
| 915 | * @since 9.0.0 |
||
| 916 | */ |
||
| 917 | public function getReadMark($objectType, $objectId, IUser $user) { |
||
| 937 | |||
| 938 | /** |
||
| 939 | * deletes the read markers on the specified object |
||
| 940 | * |
||
| 941 | * @param string $objectType |
||
| 942 | * @param string $objectId |
||
| 943 | * @return bool |
||
| 944 | * @since 9.0.0 |
||
| 945 | */ |
||
| 946 | public function deleteReadMarksOnObject($objectType, $objectId) { |
||
| 964 | |||
| 965 | /** |
||
| 966 | * registers an Entity to the manager, so event notifications can be send |
||
| 967 | * to consumers of the comments infrastructure |
||
| 968 | * |
||
| 969 | * @param \Closure $closure |
||
| 970 | */ |
||
| 971 | public function registerEventHandler(\Closure $closure) { |
||
| 975 | |||
| 976 | /** |
||
| 977 | * registers a method that resolves an ID to a display name for a given type |
||
| 978 | * |
||
| 979 | * @param string $type |
||
| 980 | * @param \Closure $closure |
||
| 981 | * @throws \OutOfBoundsException |
||
| 982 | * @since 11.0.0 |
||
| 983 | * |
||
| 984 | * Only one resolver shall be registered per type. Otherwise a |
||
| 985 | * \OutOfBoundsException has to thrown. |
||
| 986 | */ |
||
| 987 | View Code Duplication | public function registerDisplayNameResolver($type, \Closure $closure) { |
|
| 996 | |||
| 997 | /** |
||
| 998 | * resolves a given ID of a given Type to a display name. |
||
| 999 | * |
||
| 1000 | * @param string $type |
||
| 1001 | * @param string $id |
||
| 1002 | * @return string |
||
| 1003 | * @throws \OutOfBoundsException |
||
| 1004 | * @since 11.0.0 |
||
| 1005 | * |
||
| 1006 | * If a provided type was not registered, an \OutOfBoundsException shall |
||
| 1007 | * be thrown. It is upon the resolver discretion what to return of the |
||
| 1008 | * provided ID is unknown. It must be ensured that a string is returned. |
||
| 1009 | */ |
||
| 1010 | View Code Duplication | public function resolveDisplayName($type, $id) { |
|
| 1019 | |||
| 1020 | /** |
||
| 1021 | * returns valid, registered entities |
||
| 1022 | * |
||
| 1023 | * @return \OCP\Comments\ICommentsEventHandler[] |
||
| 1024 | */ |
||
| 1025 | View Code Duplication | private function getEventHandlers() { |
|
| 1041 | |||
| 1042 | /** |
||
| 1043 | * sends notifications to the registered entities |
||
| 1044 | * |
||
| 1045 | * @param $eventType |
||
| 1046 | * @param IComment $comment |
||
| 1047 | */ |
||
| 1048 | private function sendEvent($eventType, IComment $comment) { |
||
| 1055 | } |
||
| 1056 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: