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:
1 | <?php |
||
10 | class SubscriptionEventRepository extends EntityRepository |
||
11 | { |
||
12 | public function add(SubscriptionEvent $entity): void |
||
16 | |||
17 | 4 | /** |
|
18 | * @return int |
||
19 | 4 | */ |
|
20 | public function getLastDayEventsCount(): int |
||
33 | |||
34 | /** |
||
35 | * Creates QueryBuilder object for pagination of user subscribers events |
||
36 | 3 | * |
|
37 | * @param User $user |
||
38 | 3 | * |
|
39 | * @return QueryBuilder |
||
40 | */ |
||
41 | 3 | View Code Duplication | public function createUserLastSubscribersEventsQuery(User $user): QueryBuilder |
53 | |||
54 | /** |
||
55 | * Get last user subscriber events |
||
56 | * |
||
57 | * @param User $user |
||
58 | * @param int $limit |
||
59 | * |
||
60 | * @return SubscriptionEvent[] |
||
61 | */ |
||
62 | public function getUserLastSubscribersEvents(User $user, int $limit = 20): array |
||
69 | |||
70 | 2 | /** |
|
71 | * Get last global subscriptions QueryBuilder for pagination |
||
72 | 2 | * |
|
73 | * @return QueryBuilder |
||
74 | */ |
||
75 | 2 | View Code Duplication | public function createLastSubscriptionEventsQuery(): QueryBuilder |
86 | |||
87 | /** |
||
88 | * Get last global subscription events |
||
89 | * |
||
90 | * @param int $limit |
||
91 | * |
||
92 | * @return SubscriptionEvent[] |
||
93 | */ |
||
94 | public function getLastSubscriptionEvents(int $limit = 20): array |
||
101 | |||
102 | /** |
||
103 | * @return SubscriptionEvent[] |
||
104 | */ |
||
105 | public function getLastEventsByDay(int $days = 30): array |
||
133 | } |
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.