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 CommentQueryManager 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 CommentQueryManager, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | class CommentQueryManager extends Manager{ |
||
15 | |||
16 | private $repository; |
||
17 | private $comment_class; |
||
18 | private $depth; |
||
19 | private $depth_reached; |
||
20 | |||
21 | public function __construct(ManagerRegistry $managerRegistry, $class, $depth) |
||
27 | |||
28 | |||
29 | /** |
||
30 | * Find one comment |
||
31 | * @param $id |
||
32 | * @param null $model_name |
||
33 | * @param int $model_id |
||
34 | * @return mixed |
||
35 | */ |
||
36 | public function findOneComment($id, $model_name=null,$model_id=0) |
||
43 | |||
44 | |||
45 | /** |
||
46 | * Find a comment by model referer |
||
47 | * @param $id |
||
48 | * @param IsCommentable $model |
||
49 | * @return mixed |
||
50 | */ |
||
51 | public function findOneCommentByReferer($id,IsCommentable $model) |
||
60 | |||
61 | /** |
||
62 | * Find all comments for an entity |
||
63 | * @param IsCommentable $model |
||
64 | * @param bool $get_by_id |
||
65 | * @return mixed |
||
66 | */ |
||
67 | public function findComments(IsCommentable $model, $get_by_id=false) |
||
79 | |||
80 | |||
81 | /** |
||
82 | * Find all comments for an entity |
||
83 | * @param $model_name |
||
84 | * @param $model_id |
||
85 | * @param bool $get_by_id |
||
86 | * @return mixed |
||
87 | */ |
||
88 | public function findCommentsByModelAndId($model_name, $model_id, $get_by_id=false) |
||
99 | |||
100 | |||
101 | /** |
||
102 | * Build Comment Structure |
||
103 | * @param $comments |
||
104 | * @param bool $get_by_id |
||
105 | * @return mixed |
||
106 | */ |
||
107 | private function buildCommentStructure($comments, $get_by_id=false) |
||
112 | |||
113 | |||
114 | private function build($comments,$comments_by_id, $get_by_id) |
||
158 | |||
159 | /** |
||
160 | * config depth of comments |
||
161 | * @param $comments_by_id |
||
162 | * @param $comment_id |
||
163 | * @return mixed |
||
164 | */ |
||
165 | private function depthComment($comments_by_id,$comment_id) |
||
189 | |||
190 | |||
191 | /** |
||
192 | * Retrieve all comments |
||
193 | * @param array $criteria |
||
194 | * @param null $orderBy |
||
195 | * @param null $limit |
||
196 | * @param null $offset |
||
197 | * @return array |
||
198 | */ |
||
199 | public function findAllComments(array $criteria=[],$orderBy=null,$limit=null,$offset=null) |
||
208 | |||
209 | |||
210 | /** |
||
211 | * @param $qb |
||
212 | * @param $model_name |
||
213 | * @param $model_id |
||
214 | * @param $is_join |
||
215 | * @return mixed |
||
216 | */ |
||
217 | private function commentQueryResult($qb, $model_name, $model_id, $is_join) |
||
237 | |||
238 | private function oneCommentQueryResult($qb, $id, $model_name, $model_id, $is_join) |
||
266 | |||
267 | |||
268 | /** |
||
269 | * @param $ids |
||
270 | * @return mixed |
||
271 | */ |
||
272 | View Code Duplication | public function deleteByCommentIds($ids) |
|
285 | |||
286 | |||
287 | /** |
||
288 | * Delete a comment by id |
||
289 | * @param $id |
||
290 | * @return mixed |
||
291 | */ |
||
292 | View Code Duplication | public function deleteComment($id) |
|
305 | |||
306 | |||
307 | |||
308 | /** |
||
309 | * Remove ccomment children |
||
310 | * @param $comment |
||
311 | * @return array |
||
312 | */ |
||
313 | public function getChildren($comment) |
||
327 | |||
328 | /** |
||
329 | * Pre delete comment |
||
330 | * @param IsCommentable $referer |
||
331 | * @return bool |
||
332 | */ |
||
333 | public function preDeleteComment(IsCommentable $referer) |
||
345 | } |
||
346 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.