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 |
||
12 | class ReadableBehavior extends Behavior |
||
13 | { |
||
14 | |||
15 | public $readAttribute = 'read_at'; |
||
16 | |||
17 | /** |
||
18 | * Mark the notification as read. |
||
19 | * @throws \Exception |
||
20 | * @throws \Throwable |
||
21 | */ |
||
22 | View Code Duplication | public function markAsRead() |
|
30 | |||
31 | /** |
||
32 | * Mark the notification as unread. |
||
33 | * @return void |
||
34 | * @throws \Exception |
||
35 | * @throws \Throwable |
||
36 | */ |
||
37 | View Code Duplication | public function markAsUnread() |
|
45 | |||
46 | /** |
||
47 | * Determine if a notification has been read. |
||
48 | * |
||
49 | * @return bool |
||
50 | */ |
||
51 | public function isRead() |
||
55 | /** |
||
56 | * Determine if a notification has not been read. |
||
57 | * |
||
58 | * @return bool |
||
59 | */ |
||
60 | public function isUnread() |
||
64 | } |
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.