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 StackService 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 StackService, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
43 | class StackService { |
||
44 | |||
45 | private $stackMapper; |
||
46 | private $cardMapper; |
||
47 | private $boardMapper; |
||
48 | private $labelMapper; |
||
49 | private $permissionService; |
||
50 | private $boardService; |
||
51 | private $cardService; |
||
52 | private $assignedUsersMapper; |
||
53 | private $attachmentService; |
||
54 | private $activityManager; |
||
55 | /** @var EventDispatcherInterface */ |
||
56 | private $eventDispatcher; |
||
57 | private $changeHelper; |
||
58 | |||
59 | public function __construct( |
||
60 | StackMapper $stackMapper, |
||
61 | BoardMapper $boardMapper, |
||
62 | CardMapper $cardMapper, |
||
63 | LabelMapper $labelMapper, |
||
64 | PermissionService $permissionService, |
||
65 | BoardService $boardService, |
||
66 | CardService $cardService, |
||
67 | AssignedUsersMapper $assignedUsersMapper, |
||
68 | AttachmentService $attachmentService, |
||
69 | ActivityManager $activityManager, |
||
70 | EventDispatcherInterface $eventDispatcher, |
||
71 | ChangeHelper $changeHelper |
||
72 | ) { |
||
73 | $this->stackMapper = $stackMapper; |
||
74 | $this->boardMapper = $boardMapper; |
||
75 | $this->cardMapper = $cardMapper; |
||
76 | $this->labelMapper = $labelMapper; |
||
77 | $this->permissionService = $permissionService; |
||
78 | $this->boardService = $boardService; |
||
79 | $this->cardService = $cardService; |
||
80 | $this->assignedUsersMapper = $assignedUsersMapper; |
||
81 | $this->attachmentService = $attachmentService; |
||
82 | $this->activityManager = $activityManager; |
||
83 | $this->eventDispatcher = $eventDispatcher; |
||
84 | $this->changeHelper = $changeHelper; |
||
85 | } |
||
86 | |||
87 | private function enrichStackWithCards($stack, $since = -1) { |
||
100 | |||
101 | private function enrichStacksWithCards($stacks, $since = -1) { |
||
106 | |||
107 | /** |
||
108 | * @param $stackId |
||
109 | * |
||
110 | * @return \OCP\AppFramework\Db\Entity |
||
111 | * @throws \OCP\AppFramework\Db\DoesNotExistException |
||
112 | * @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException |
||
113 | * @throws BadRequestException |
||
114 | */ |
||
115 | public function find($stackId) { |
||
131 | |||
132 | /** |
||
133 | * @param $boardId |
||
134 | * |
||
135 | * @return array |
||
136 | * @throws \OCA\Deck\NoPermissionException |
||
137 | * @throws BadRequestException |
||
138 | */ |
||
139 | public function findAll($boardId, $since = -1) { |
||
150 | |||
151 | public function fetchDeleted($boardId) { |
||
160 | |||
161 | /** |
||
162 | * @param $boardId |
||
163 | * |
||
164 | * @return array |
||
165 | * @throws \OCA\Deck\NoPermissionException |
||
166 | * @throws BadRequestException |
||
167 | */ |
||
168 | public function findAllArchived($boardId) { |
||
189 | |||
190 | /** |
||
191 | * @param $title |
||
192 | * @param $boardId |
||
193 | * @param integer $order |
||
194 | * |
||
195 | * @return \OCP\AppFramework\Db\Entity |
||
196 | * @throws StatusException |
||
197 | * @throws \OCA\Deck\NoPermissionException |
||
198 | * @throws \OCP\AppFramework\Db\DoesNotExistException |
||
199 | * @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException |
||
200 | * @throws BadRequestException |
||
201 | */ |
||
202 | public function create($title, $boardId, $order) { |
||
237 | |||
238 | /** |
||
239 | * @param $id |
||
240 | * |
||
241 | * @return \OCP\AppFramework\Db\Entity |
||
242 | * @throws \OCA\Deck\NoPermissionException |
||
243 | * @throws \OCP\AppFramework\Db\DoesNotExistException |
||
244 | * @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException |
||
245 | * @throws BadRequestException |
||
246 | */ |
||
247 | View Code Duplication | public function delete($id) { |
|
271 | |||
272 | /** |
||
273 | * @param $id |
||
274 | * @param $title |
||
275 | * @param $boardId |
||
276 | * @param $order |
||
277 | * @param $deletedAt |
||
278 | * |
||
279 | * @return \OCP\AppFramework\Db\Entity |
||
280 | * @throws StatusException |
||
281 | * @throws \OCA\Deck\NoPermissionException |
||
282 | * @throws \OCP\AppFramework\Db\DoesNotExistException |
||
283 | * @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException |
||
284 | * @throws BadRequestException |
||
285 | */ |
||
286 | public function update($id, $title, $boardId, $order, $deletedAt) { |
||
327 | |||
328 | /** |
||
329 | * @param $id |
||
330 | * @param $order |
||
331 | * |
||
332 | * @return array |
||
333 | * @throws \OCA\Deck\NoPermissionException |
||
334 | * @throws \OCP\AppFramework\Db\DoesNotExistException |
||
335 | * @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException |
||
336 | * @throws BadRequestException |
||
337 | */ |
||
338 | public function reorder($id, $order) { |
||
372 | } |
||
373 |
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.