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 |
||
33 | class UserSettings { |
||
34 | /** @var IManager */ |
||
35 | protected $manager; |
||
36 | |||
37 | /** @var IConfig */ |
||
38 | protected $config; |
||
39 | |||
40 | /** @var Data */ |
||
41 | protected $data; |
||
42 | |||
43 | const EMAIL_SEND_HOURLY = 0; |
||
44 | const EMAIL_SEND_DAILY = 1; |
||
45 | const EMAIL_SEND_WEEKLY = 2; |
||
46 | const EMAIL_SEND_ASAP = 3; |
||
47 | |||
48 | /** |
||
49 | * @param IManager $manager |
||
50 | * @param IConfig $config |
||
51 | */ |
||
52 | 25 | public function __construct(IManager $manager, IConfig $config) { |
|
56 | |||
57 | /** |
||
58 | * Get a setting for a user |
||
59 | * |
||
60 | * Falls back to some good default values if the user does not have a preference |
||
61 | * |
||
62 | * @param string $user |
||
63 | * @param string $method Should be one of 'stream', 'email' or 'setting' |
||
64 | * @param string $type One of the activity types, 'batchtime' or 'self' |
||
65 | * @return bool|int |
||
66 | */ |
||
67 | 5 | public function getUserSetting($user, $method, $type) { |
|
68 | 5 | if ($method === 'email' && $this->config->getAppValue('activity', 'enable_email', 'yes') === 'no') { |
|
69 | return false; |
||
70 | } |
||
71 | |||
72 | 5 | $defaultSetting = $this->getDefaultFromSetting($method, $type); |
|
73 | 5 | View Code Duplication | if (is_bool($defaultSetting)) { |
|
|||
74 | 5 | return (bool) $this->config->getUserValue( |
|
75 | 5 | $user, |
|
76 | 5 | 'activity', |
|
77 | 5 | 'notify_' . $method . '_' . $type, |
|
78 | $defaultSetting |
||
79 | ); |
||
80 | } |
||
81 | |||
82 | return (int) $this->config->getUserValue( |
||
83 | $user, |
||
84 | 'activity', |
||
85 | 'notify_' . $method . '_' . $type, |
||
86 | $defaultSetting |
||
87 | ); |
||
88 | } |
||
89 | |||
90 | /** |
||
91 | * @param string $method |
||
92 | * @param string $type |
||
93 | * @return bool|int |
||
94 | */ |
||
95 | public function getConfigSetting($method, $type) { |
||
96 | $defaultSetting = $this->getDefaultFromSetting($method, $type); |
||
97 | View Code Duplication | if (is_bool($defaultSetting)) { |
|
98 | return (bool) $this->config->getAppValue( |
||
99 | 'activity', |
||
100 | 'notify_' . $method . '_' . $type, |
||
101 | $defaultSetting |
||
102 | ); |
||
103 | } |
||
104 | |||
105 | return (int) $this->config->getAppValue( |
||
106 | 'activity', |
||
107 | 'notify_' . $method . '_' . $type, |
||
108 | $defaultSetting |
||
109 | ); |
||
110 | } |
||
111 | |||
112 | /** |
||
113 | * Get a good default setting for a preference |
||
114 | * |
||
115 | * @param string $method Should be one of 'stream', 'email' or 'setting' |
||
116 | * @param string $type One of the activity types, 'batchtime', 'self' or 'selfemail' |
||
117 | * @return bool|int |
||
118 | */ |
||
119 | 11 | protected function getDefaultFromSetting($method, $type) { |
|
143 | |||
144 | /** |
||
145 | * Get a list with enabled notification types for a user |
||
146 | * |
||
147 | * @param string $user Name of the user |
||
148 | * @param string $method Should be one of 'stream' or 'email' |
||
149 | * @return array |
||
150 | */ |
||
151 | 5 | public function getNotificationTypes($user, $method) { |
|
163 | |||
164 | /** |
||
165 | * Filters the given user array by their notification setting |
||
166 | * |
||
167 | * @param array $users |
||
168 | * @param string $method |
||
169 | * @param string $type |
||
170 | * @return array Returns a "username => b:true" Map for method = stream |
||
171 | * Returns a "username => i:batchtime" Map for method = email |
||
172 | */ |
||
173 | public function filterUsersBySetting($users, $method, $type) { |
||
217 | } |
||
218 |
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.