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 |
||
| 39 | class Manager implements ICommentsManager { |
||
| 40 | |||
| 41 | /** @var IDBConnection */ |
||
| 42 | protected $dbConn; |
||
| 43 | |||
| 44 | /** @var ILogger */ |
||
| 45 | protected $logger; |
||
| 46 | |||
| 47 | /** @var IConfig */ |
||
| 48 | protected $config; |
||
| 49 | |||
| 50 | /** @var IComment[] */ |
||
| 51 | protected $commentsCache = []; |
||
| 52 | |||
| 53 | /** @var \Closure[] */ |
||
| 54 | protected $eventHandlerClosures = []; |
||
| 55 | |||
| 56 | /** @var ICommentsEventHandler[] */ |
||
| 57 | protected $eventHandlers = []; |
||
| 58 | |||
| 59 | /** @var \Closure[] */ |
||
| 60 | protected $displayNameResolvers = []; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Manager constructor. |
||
| 64 | * |
||
| 65 | * @param IDBConnection $dbConn |
||
| 66 | * @param ILogger $logger |
||
| 67 | * @param IConfig $config |
||
| 68 | */ |
||
| 69 | public function __construct( |
||
| 78 | |||
| 79 | /** |
||
| 80 | * converts data base data into PHP native, proper types as defined by |
||
| 81 | * IComment interface. |
||
| 82 | * |
||
| 83 | * @param array $data |
||
| 84 | * @return array |
||
| 85 | */ |
||
| 86 | protected function normalizeDatabaseData(array $data) { |
||
| 97 | |||
| 98 | /** |
||
| 99 | * prepares a comment for an insert or update operation after making sure |
||
| 100 | * all necessary fields have a value assigned. |
||
| 101 | * |
||
| 102 | * @param IComment $comment |
||
| 103 | * @return IComment returns the same updated IComment instance as provided |
||
| 104 | * by parameter for convenience |
||
| 105 | * @throws \UnexpectedValueException |
||
| 106 | */ |
||
| 107 | protected function prepareCommentForDatabaseWrite(IComment $comment) { |
||
| 137 | |||
| 138 | /** |
||
| 139 | * returns the topmost parent id of a given comment identified by ID |
||
| 140 | * |
||
| 141 | * @param string $id |
||
| 142 | * @return string |
||
| 143 | * @throws NotFoundException |
||
| 144 | */ |
||
| 145 | protected function determineTopmostParentId($id) { |
||
| 153 | |||
| 154 | /** |
||
| 155 | * updates child information of a comment |
||
| 156 | * |
||
| 157 | * @param string $id |
||
| 158 | * @param \DateTime $cDateTime the date time of the most recent child |
||
| 159 | * @throws NotFoundException |
||
| 160 | */ |
||
| 161 | protected function updateChildrenInformation($id, \DateTime $cDateTime) { |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Tests whether actor or object type and id parameters are acceptable. |
||
| 181 | * Throws exception if not. |
||
| 182 | * |
||
| 183 | * @param string $role |
||
| 184 | * @param string $type |
||
| 185 | * @param string $id |
||
| 186 | * @throws \InvalidArgumentException |
||
| 187 | */ |
||
| 188 | protected function checkRoleParameters($role, $type, $id) { |
||
| 196 | |||
| 197 | /** |
||
| 198 | * run-time caches a comment |
||
| 199 | * |
||
| 200 | * @param IComment $comment |
||
| 201 | */ |
||
| 202 | protected function cache(IComment $comment) { |
||
| 209 | |||
| 210 | /** |
||
| 211 | * removes an entry from the comments run time cache |
||
| 212 | * |
||
| 213 | * @param mixed $id the comment's id |
||
| 214 | */ |
||
| 215 | protected function uncache($id) { |
||
| 221 | |||
| 222 | /** |
||
| 223 | * returns a comment instance |
||
| 224 | * |
||
| 225 | * @param string $id the ID of the comment |
||
| 226 | * @return IComment |
||
| 227 | * @throws NotFoundException |
||
| 228 | * @throws \InvalidArgumentException |
||
| 229 | * @since 9.0.0 |
||
| 230 | */ |
||
| 231 | public function get($id) { |
||
| 257 | |||
| 258 | /** |
||
| 259 | * returns the comment specified by the id and all it's child comments. |
||
| 260 | * At this point of time, we do only support one level depth. |
||
| 261 | * |
||
| 262 | * @param string $id |
||
| 263 | * @param int $limit max number of entries to return, 0 returns all |
||
| 264 | * @param int $offset the start entry |
||
| 265 | * @return array |
||
| 266 | * @since 9.0.0 |
||
| 267 | * |
||
| 268 | * The return array looks like this |
||
| 269 | * [ |
||
| 270 | * 'comment' => IComment, // root comment |
||
| 271 | * 'replies' => |
||
| 272 | * [ |
||
| 273 | * 0 => |
||
| 274 | * [ |
||
| 275 | * 'comment' => IComment, |
||
| 276 | * 'replies' => [] |
||
| 277 | * ] |
||
| 278 | * 1 => |
||
| 279 | * [ |
||
| 280 | * 'comment' => IComment, |
||
| 281 | * 'replies'=> [] |
||
| 282 | * ], |
||
| 283 | * … |
||
| 284 | * ] |
||
| 285 | * ] |
||
| 286 | */ |
||
| 287 | public function getTree($id, $limit = 0, $offset = 0) { |
||
| 319 | |||
| 320 | /** |
||
| 321 | * returns comments for a specific object (e.g. a file). |
||
| 322 | * |
||
| 323 | * The sort order is always newest to oldest. |
||
| 324 | * |
||
| 325 | * @param string $objectType the object type, e.g. 'files' |
||
| 326 | * @param string $objectId the id of the object |
||
| 327 | * @param int $limit optional, number of maximum comments to be returned. if |
||
| 328 | * not specified, all comments are returned. |
||
| 329 | * @param int $offset optional, starting point |
||
| 330 | * @param \DateTime $notOlderThan optional, timestamp of the oldest comments |
||
| 331 | * that may be returned |
||
| 332 | * @return IComment[] |
||
| 333 | * @since 9.0.0 |
||
| 334 | */ |
||
| 335 | public function getForObject( |
||
| 375 | |||
| 376 | /** |
||
| 377 | * @param $objectType string the object type, e.g. 'files' |
||
| 378 | * @param $objectId string the id of the object |
||
| 379 | * @param \DateTime $notOlderThan optional, timestamp of the oldest comments |
||
| 380 | * that may be returned |
||
| 381 | * @return Int |
||
| 382 | * @since 9.0.0 |
||
| 383 | */ |
||
| 384 | public function getNumberOfCommentsForObject($objectType, $objectId, \DateTime $notOlderThan = null) { |
||
| 404 | |||
| 405 | /** |
||
| 406 | * Get the number of unread comments for all files in a folder |
||
| 407 | * |
||
| 408 | * @param int $folderId |
||
| 409 | * @param IUser $user |
||
| 410 | * @return array [$fileId => $unreadCount] |
||
| 411 | */ |
||
| 412 | public function getNumberOfUnreadCommentsForFolder($folderId, IUser $user) { |
||
| 445 | |||
| 446 | /** |
||
| 447 | * creates a new comment and returns it. At this point of time, it is not |
||
| 448 | * saved in the used data storage. Use save() after setting other fields |
||
| 449 | * of the comment (e.g. message or verb). |
||
| 450 | * |
||
| 451 | * @param string $actorType the actor type (e.g. 'users') |
||
| 452 | * @param string $actorId a user id |
||
| 453 | * @param string $objectType the object type the comment is attached to |
||
| 454 | * @param string $objectId the object id the comment is attached to |
||
| 455 | * @return IComment |
||
| 456 | * @since 9.0.0 |
||
| 457 | */ |
||
| 458 | public function create($actorType, $actorId, $objectType, $objectId) { |
||
| 465 | |||
| 466 | /** |
||
| 467 | * permanently deletes the comment specified by the ID |
||
| 468 | * |
||
| 469 | * When the comment has child comments, their parent ID will be changed to |
||
| 470 | * the parent ID of the item that is to be deleted. |
||
| 471 | * |
||
| 472 | * @param string $id |
||
| 473 | * @return bool |
||
| 474 | * @throws \InvalidArgumentException |
||
| 475 | * @since 9.0.0 |
||
| 476 | */ |
||
| 477 | public function delete($id) { |
||
| 508 | |||
| 509 | /** |
||
| 510 | * saves the comment permanently |
||
| 511 | * |
||
| 512 | * if the supplied comment has an empty ID, a new entry comment will be |
||
| 513 | * saved and the instance updated with the new ID. |
||
| 514 | * |
||
| 515 | * Otherwise, an existing comment will be updated. |
||
| 516 | * |
||
| 517 | * Throws NotFoundException when a comment that is to be updated does not |
||
| 518 | * exist anymore at this point of time. |
||
| 519 | * |
||
| 520 | * @param IComment $comment |
||
| 521 | * @return bool |
||
| 522 | * @throws NotFoundException |
||
| 523 | * @since 9.0.0 |
||
| 524 | */ |
||
| 525 | public function save(IComment $comment) { |
||
| 542 | |||
| 543 | /** |
||
| 544 | * inserts the provided comment in the database |
||
| 545 | * |
||
| 546 | * @param IComment $comment |
||
| 547 | * @return bool |
||
| 548 | */ |
||
| 549 | protected function insert(IComment &$comment) { |
||
| 575 | |||
| 576 | /** |
||
| 577 | * updates a Comment data row |
||
| 578 | * |
||
| 579 | * @param IComment $comment |
||
| 580 | * @return bool |
||
| 581 | * @throws NotFoundException |
||
| 582 | */ |
||
| 583 | protected function update(IComment $comment) { |
||
| 616 | |||
| 617 | /** |
||
| 618 | * removes references to specific actor (e.g. on user delete) of a comment. |
||
| 619 | * The comment itself must not get lost/deleted. |
||
| 620 | * |
||
| 621 | * @param string $actorType the actor type (e.g. 'users') |
||
| 622 | * @param string $actorId a user id |
||
| 623 | * @return boolean |
||
| 624 | * @since 9.0.0 |
||
| 625 | */ |
||
| 626 | public function deleteReferencesOfActor($actorType, $actorId) { |
||
| 644 | |||
| 645 | /** |
||
| 646 | * deletes all comments made of a specific object (e.g. on file delete) |
||
| 647 | * |
||
| 648 | * @param string $objectType the object type (e.g. 'files') |
||
| 649 | * @param string $objectId e.g. the file id |
||
| 650 | * @return boolean |
||
| 651 | * @since 9.0.0 |
||
| 652 | */ |
||
| 653 | public function deleteCommentsAtObject($objectType, $objectId) { |
||
| 669 | |||
| 670 | /** |
||
| 671 | * deletes the read markers for the specified user |
||
| 672 | * |
||
| 673 | * @param \OCP\IUser $user |
||
| 674 | * @return bool |
||
| 675 | * @since 9.0.0 |
||
| 676 | */ |
||
| 677 | public function deleteReadMarksFromUser(IUser $user) { |
||
| 691 | |||
| 692 | /** |
||
| 693 | * sets the read marker for a given file to the specified date for the |
||
| 694 | * provided user |
||
| 695 | * |
||
| 696 | * @param string $objectType |
||
| 697 | * @param string $objectId |
||
| 698 | * @param \DateTime $dateTime |
||
| 699 | * @param IUser $user |
||
| 700 | * @since 9.0.0 |
||
| 701 | * @suppress SqlInjectionChecker |
||
| 702 | */ |
||
| 703 | public function setReadMark($objectType, $objectId, \DateTime $dateTime, IUser $user) { |
||
| 737 | |||
| 738 | /** |
||
| 739 | * returns the read marker for a given file to the specified date for the |
||
| 740 | * provided user. It returns null, when the marker is not present, i.e. |
||
| 741 | * no comments were marked as read. |
||
| 742 | * |
||
| 743 | * @param string $objectType |
||
| 744 | * @param string $objectId |
||
| 745 | * @param IUser $user |
||
| 746 | * @return \DateTime|null |
||
| 747 | * @since 9.0.0 |
||
| 748 | */ |
||
| 749 | public function getReadMark($objectType, $objectId, IUser $user) { |
||
| 769 | |||
| 770 | /** |
||
| 771 | * deletes the read markers on the specified object |
||
| 772 | * |
||
| 773 | * @param string $objectType |
||
| 774 | * @param string $objectId |
||
| 775 | * @return bool |
||
| 776 | * @since 9.0.0 |
||
| 777 | */ |
||
| 778 | public function deleteReadMarksOnObject($objectType, $objectId) { |
||
| 796 | |||
| 797 | /** |
||
| 798 | * registers an Entity to the manager, so event notifications can be send |
||
| 799 | * to consumers of the comments infrastructure |
||
| 800 | * |
||
| 801 | * @param \Closure $closure |
||
| 802 | */ |
||
| 803 | public function registerEventHandler(\Closure $closure) { |
||
| 807 | |||
| 808 | /** |
||
| 809 | * registers a method that resolves an ID to a display name for a given type |
||
| 810 | * |
||
| 811 | * @param string $type |
||
| 812 | * @param \Closure $closure |
||
| 813 | * @throws \OutOfBoundsException |
||
| 814 | * @since 11.0.0 |
||
| 815 | * |
||
| 816 | * Only one resolver shall be registered per type. Otherwise a |
||
| 817 | * \OutOfBoundsException has to thrown. |
||
| 818 | */ |
||
| 819 | View Code Duplication | public function registerDisplayNameResolver($type, \Closure $closure) { |
|
| 828 | |||
| 829 | /** |
||
| 830 | * resolves a given ID of a given Type to a display name. |
||
| 831 | * |
||
| 832 | * @param string $type |
||
| 833 | * @param string $id |
||
| 834 | * @return string |
||
| 835 | * @throws \OutOfBoundsException |
||
| 836 | * @since 11.0.0 |
||
| 837 | * |
||
| 838 | * If a provided type was not registered, an \OutOfBoundsException shall |
||
| 839 | * be thrown. It is upon the resolver discretion what to return of the |
||
| 840 | * provided ID is unknown. It must be ensured that a string is returned. |
||
| 841 | */ |
||
| 842 | View Code Duplication | public function resolveDisplayName($type, $id) { |
|
| 851 | |||
| 852 | /** |
||
| 853 | * returns valid, registered entities |
||
| 854 | * |
||
| 855 | * @return \OCP\Comments\ICommentsEventHandler[] |
||
| 856 | */ |
||
| 857 | View Code Duplication | private function getEventHandlers() { |
|
| 873 | |||
| 874 | /** |
||
| 875 | * sends notifications to the registered entities |
||
| 876 | * |
||
| 877 | * @param $eventType |
||
| 878 | * @param IComment $comment |
||
| 879 | */ |
||
| 880 | private function sendEvent($eventType, IComment $comment) { |
||
| 887 | } |
||
| 888 |
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: