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 namespace Cornford\Notifier; |
||
8 | abstract class NotifierAbstract { |
||
9 | |||
10 | const JS_JQUERY_CDN = '//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.min.js'; |
||
11 | const JS_JQUERY_LOCAL = 'packages/cornford/notifier/assets/js/jquery.min.js'; |
||
12 | |||
13 | const JS_NOTIFY_CDN = '//cdnjs.cloudflare.com/ajax/libs/notify/0.3.2/notify.min.js'; |
||
14 | const JS_NOTIFY_LOCAL = 'packages/cornford/notifier/assets/js/notify.min.js'; |
||
15 | |||
16 | const JS_NOTIFYER_CDN = 'packages/cornford/notifier/assets/js/notifier.min.js'; |
||
17 | const JS_NOTIFYER_LOCAL = 'packages/cornford/notifier/assets/js/notifier.min.js'; |
||
18 | |||
19 | /** |
||
20 | * View instance. |
||
21 | * |
||
22 | * @var \Illuminate\View\Factory |
||
23 | */ |
||
24 | protected $view; |
||
25 | |||
26 | /** |
||
27 | * Session instance. |
||
28 | * |
||
29 | * @var \Illuminate\Session\Store |
||
30 | */ |
||
31 | protected $session; |
||
32 | |||
33 | /** |
||
34 | * Notification Id. |
||
35 | * |
||
36 | * @var integer |
||
37 | */ |
||
38 | protected static $notificationId = 0; |
||
39 | |||
40 | /** |
||
41 | * Options. |
||
42 | * |
||
43 | * @var array |
||
44 | */ |
||
45 | protected $options; |
||
46 | |||
47 | /** |
||
48 | * Notifications array. |
||
49 | * |
||
50 | * @var array |
||
51 | */ |
||
52 | protected static $notifications = []; |
||
53 | |||
54 | /** |
||
55 | * Construct Notifier. |
||
56 | * |
||
57 | * @param View $view |
||
58 | * @param Session $session |
||
59 | * @param array $options |
||
60 | */ |
||
61 | public function __construct(View $view, Session $session, array $options = []) |
||
67 | |||
68 | /** |
||
69 | * Create a notification. |
||
70 | * |
||
71 | * @param string $message |
||
72 | * @param string $type |
||
73 | * @param DateTime|integer $expiry |
||
74 | * |
||
75 | * @return void |
||
76 | */ |
||
77 | public function notification($message, $type, $expiry = 0) |
||
83 | |||
84 | /** |
||
85 | * Create a default notification. |
||
86 | * |
||
87 | * @param string $message |
||
88 | * @param DateTime|integer $expiry |
||
89 | * |
||
90 | * @return void |
||
91 | */ |
||
92 | public function none($message, $expiry = 0) |
||
96 | |||
97 | /** |
||
98 | * Create an info notification. |
||
99 | * |
||
100 | * @param string $message |
||
101 | * @param DateTime|integer $expiry |
||
102 | * |
||
103 | * @return void |
||
104 | */ |
||
105 | public function info($message, $expiry = 0) |
||
109 | |||
110 | /** |
||
111 | * Create a success notification. |
||
112 | * |
||
113 | * @param string $message |
||
114 | * @param DateTime|integer $expiry |
||
115 | * |
||
116 | * @return void |
||
117 | */ |
||
118 | public function success($message, $expiry = 0) |
||
122 | |||
123 | /** |
||
124 | * Create a warning notification. |
||
125 | * |
||
126 | * @param string $message |
||
127 | * @param DateTime|integer $expiry |
||
128 | * |
||
129 | * @return void |
||
130 | */ |
||
131 | public function warning($message, $expiry = 0) |
||
135 | |||
136 | /** |
||
137 | * Create a danger notification. |
||
138 | * |
||
139 | * @param string $message |
||
140 | * @param DateTime|integer $expiry |
||
141 | * |
||
142 | * @return void |
||
143 | */ |
||
144 | public function danger($message, $expiry = 0) |
||
148 | |||
149 | /** |
||
150 | * Set options. |
||
151 | * |
||
152 | * @param array $value |
||
153 | * |
||
154 | * @return void |
||
155 | */ |
||
156 | public function setOptions(array $value) |
||
160 | |||
161 | /** |
||
162 | * Get options. |
||
163 | * |
||
164 | * @return array |
||
165 | */ |
||
166 | public function getOptions() |
||
170 | |||
171 | /** |
||
172 | * Set notifications. |
||
173 | * |
||
174 | * @param array $value |
||
175 | * |
||
176 | * @return void |
||
177 | */ |
||
178 | public function setNotifications(array $value) |
||
182 | |||
183 | /** |
||
184 | * Get notifications. |
||
185 | * |
||
186 | * @return array |
||
187 | */ |
||
188 | public function getNotifications() |
||
192 | |||
193 | /** |
||
194 | * Include the CDN JS / Local JS files. |
||
195 | * |
||
196 | * @param string $type |
||
197 | * |
||
198 | * @return array |
||
199 | */ |
||
200 | protected function js($type = 'local') |
||
219 | |||
220 | /** |
||
221 | * Render assets. |
||
222 | * |
||
223 | * @param string $type |
||
224 | * |
||
225 | * @return string |
||
226 | */ |
||
227 | public function assets($type = 'local') |
||
234 | |||
235 | } |
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.