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:
Complex classes like Notifier often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Notifier, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
7 | class Notifier |
||
8 | { |
||
9 | |||
10 | /** |
||
11 | * Added notifications |
||
12 | * |
||
13 | * @var array |
||
14 | */ |
||
15 | protected $notifications = []; |
||
16 | |||
17 | /** |
||
18 | * Flashable added notifications |
||
19 | * |
||
20 | * @var array |
||
21 | */ |
||
22 | protected $flashedNotifications = []; |
||
23 | |||
24 | /** |
||
25 | * Added notifications |
||
26 | * |
||
27 | * @var array |
||
28 | */ |
||
29 | protected $options = []; |
||
30 | |||
31 | /** |
||
32 | * Illuminate Session |
||
33 | * |
||
34 | * @var \Illuminate\Session\SessionManager |
||
35 | */ |
||
36 | protected $session; |
||
37 | |||
38 | /** |
||
39 | * Constructor |
||
40 | * |
||
41 | * @param \Illuminate\Session\SessionManager $session |
||
42 | * |
||
43 | * @internal param \Illuminate\Session\SessionManager $session |
||
44 | */ |
||
45 | public function __construct(SessionManager $session) |
||
60 | |||
61 | /** |
||
62 | * Set all notification that are present in session |
||
63 | * |
||
64 | * @return void |
||
65 | */ |
||
66 | public function setNotifyFromSession(): void |
||
73 | |||
74 | /** |
||
75 | */ |
||
76 | protected function setNotifyErrorFromSession(): void |
||
80 | |||
81 | /** |
||
82 | */ |
||
83 | protected function setNotifyWarningFromSession(): void |
||
87 | |||
88 | /** |
||
89 | */ |
||
90 | protected function setNotifyInfoFromSession(): void |
||
94 | |||
95 | /** |
||
96 | */ |
||
97 | protected function setNotifySuccessFromSession(): void |
||
101 | /** |
||
102 | * Set all notification of a label type that are present in session |
||
103 | * |
||
104 | * @param string $label |
||
105 | * |
||
106 | * @return void |
||
107 | */ |
||
108 | protected function setNotifyFromSessionByLabel(string $label): void |
||
138 | |||
139 | /** |
||
140 | * Render all the notifications' script to insert into script tag |
||
141 | * |
||
142 | * @return string |
||
143 | * |
||
144 | */ |
||
145 | public function render(): string |
||
158 | |||
159 | /** |
||
160 | * Add a notification |
||
161 | * |
||
162 | * @param string $type Could be error, info, success, or warning. |
||
163 | * @param string $text The notification's message |
||
164 | * @param string $title The notification's title |
||
|
|||
165 | * @param array $options |
||
166 | * @param bool $onlyNextRequest if true(default), se the notification in session only for the next request |
||
167 | * |
||
168 | */ |
||
169 | public function add($theme, $timeout, $type, $layout, $text, $sounds = null, $soundsVolume = null) |
||
195 | |||
196 | public function addForNextRequest($theme, $timeout, $type, $layout, $text, $sounds = null, $soundsVolume = null) |
||
229 | |||
230 | /** |
||
231 | * Shortcut for adding an info notification |
||
232 | * |
||
233 | * @param string $message The notification's message |
||
234 | * @param string $title The notification's title |
||
235 | * @param array $options |
||
236 | */ |
||
237 | View Code Duplication | public function info($text, $onlyNextRequest = false, array $options = []) |
|
248 | |||
249 | /** |
||
250 | * Shortcut for adding an error notification |
||
251 | * |
||
252 | * @param string $message The notification's message |
||
253 | * @param string $title The notification's title |
||
254 | * @param array $options |
||
255 | */ |
||
256 | View Code Duplication | public function error($text, $onlyNextRequest = false, array $options = []) |
|
267 | |||
268 | /** |
||
269 | * Shortcut for adding a warning notification |
||
270 | * |
||
271 | * @param string $message The notification's message |
||
272 | * @param string $title The notification's title |
||
273 | * @param array $options |
||
274 | */ |
||
275 | View Code Duplication | public function warning($text, $onlyNextRequest = false, array $options = []) |
|
286 | |||
287 | /** |
||
288 | * Shortcut for adding a success notification |
||
289 | * |
||
290 | * @param string $message The notification's message |
||
291 | * @param string $title The notification's title |
||
292 | * @param array $options |
||
293 | */ |
||
294 | View Code Duplication | public function success($text, $onlyNextRequest = false, array $options = []) |
|
305 | |||
306 | |||
307 | /** |
||
308 | * Clear all notifications |
||
309 | * |
||
310 | * @param bool $withSession if true (default) clean notifications in session too. |
||
311 | */ |
||
312 | public function clear() |
||
316 | |||
317 | public function clearFlashed(bool $withSession = true) |
||
325 | |||
326 | public function clearAll(bool $withSession = true) |
||
335 | } |
||
336 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.