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 | * @property Google_Service_Gmail $service |
||
11 | */ |
||
12 | trait Modifiable |
||
13 | { |
||
14 | |||
15 | //TODO search for a naming convention for all the methods |
||
16 | |||
17 | use ModifiesLabels { |
||
18 | ModifiesLabels::__construct as private __mlConstruct(); |
||
|
|||
19 | } |
||
20 | |||
21 | private $messageRequest; |
||
22 | |||
23 | public function __construct() |
||
24 | { |
||
25 | $this->__mlConstruct(); |
||
26 | } |
||
27 | |||
28 | /** |
||
29 | * Marks emails as "READ". Returns string of message if fail |
||
30 | * |
||
31 | * @return Mail|string |
||
32 | */ |
||
33 | public function markAsRead() |
||
34 | { |
||
35 | try { |
||
36 | return $this->removeSingleLabel( 'UNREAD' ); |
||
37 | } catch ( \Exception $e ) { |
||
38 | return "Couldn't mark email as read: {$e->getMessage()}"; |
||
39 | } |
||
40 | } |
||
41 | |||
42 | /** |
||
43 | * Marks emails as unread |
||
44 | * |
||
45 | * @return Mail|string |
||
46 | * @throws \Exception |
||
47 | */ |
||
48 | public function markAsUnread() |
||
49 | { |
||
50 | try { |
||
51 | return $this->addSingleLabel( 'UNREAD' ); |
||
52 | } catch ( \Exception $e ) { |
||
53 | throw new \Exception( "Couldn't mark email as unread: {$e->getMessage()}" ); |
||
54 | } |
||
55 | } |
||
56 | |||
57 | /** |
||
58 | * @return Mail|string |
||
59 | * @throws \Exception |
||
60 | */ |
||
61 | public function markAsImportant() |
||
62 | { |
||
63 | try { |
||
64 | return $this->addSingleLabel( 'IMPORTANT' ); |
||
65 | } catch ( \Exception $e ) { |
||
66 | throw new \Exception( "Couldn't remove mark email as important.: {$e->getMessage()}" ); |
||
67 | } |
||
68 | } |
||
69 | |||
70 | /** |
||
71 | * @return Mail|string |
||
72 | * @throws \Exception |
||
73 | */ |
||
74 | public function markAsNotImportant() |
||
75 | { |
||
76 | try { |
||
77 | return $this->removeSingleLabel( 'IMPORTANT' ); |
||
78 | } catch ( \Exception $e ) { |
||
79 | throw new \Exception( "Couldn't mark email as unread: {$e->getMessage()}" ); |
||
80 | } |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * @return Mail|string |
||
85 | * @throws \Exception |
||
86 | */ |
||
87 | public function star() |
||
131 | } |