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) |
||
59 | |||
60 | /** |
||
61 | * Render all the notifications' script to insert into script tag |
||
62 | * |
||
63 | * @return string |
||
64 | * |
||
65 | */ |
||
66 | public function render(): string |
||
79 | |||
80 | /** |
||
81 | * Add a notification |
||
82 | * |
||
83 | * @param string $type Could be error, info, success, or warning. |
||
84 | * @param string $text The notification's message |
||
85 | * @param string $title The notification's title |
||
|
|||
86 | * @param array $options |
||
87 | * @param bool $onlyNextRequest if true(default), se the notification in session only for the next request |
||
88 | * |
||
89 | */ |
||
90 | public function add($theme, $timeout, $type, $layout, $text, $sounds = null, $soundsVolume = null) |
||
116 | |||
117 | public function addForNextRequest($theme, $timeout, $type, $layout, $text, $sounds = null, $soundsVolume = null) |
||
150 | |||
151 | /** |
||
152 | * Shortcut for adding an info notification |
||
153 | * |
||
154 | * @param string $message The notification's message |
||
155 | * @param string $title The notification's title |
||
156 | * @param array $options |
||
157 | */ |
||
158 | View Code Duplication | public function info($text, $onlyNextRequest = true, array $options = []) |
|
166 | |||
167 | /** |
||
168 | * Shortcut for adding an error notification |
||
169 | * |
||
170 | * @param string $message The notification's message |
||
171 | * @param string $title The notification's title |
||
172 | * @param array $options |
||
173 | */ |
||
174 | View Code Duplication | public function error($text, $onlyNextRequest = true, array $options = []) |
|
182 | |||
183 | /** |
||
184 | * Shortcut for adding a warning notification |
||
185 | * |
||
186 | * @param string $message The notification's message |
||
187 | * @param string $title The notification's title |
||
188 | * @param array $options |
||
189 | */ |
||
190 | View Code Duplication | public function warning($text, $onlyNextRequest = true, array $options = []) |
|
198 | |||
199 | /** |
||
200 | * Shortcut for adding a success notification |
||
201 | * |
||
202 | * @param string $message The notification's message |
||
203 | * @param string $title The notification's title |
||
204 | * @param array $options |
||
205 | */ |
||
206 | View Code Duplication | public function success($text, $onlyNextRequest = true, array $options = []) |
|
213 | |||
214 | |||
215 | /** |
||
216 | * Clear all notifications |
||
217 | * |
||
218 | * @param bool $withSession if true (default) clean notifications in session too. |
||
219 | */ |
||
220 | public function clear() |
||
224 | |||
225 | public function clearFlashed(bool $withSession = true) |
||
233 | |||
234 | public function clearAll(bool $withSession = true) |
||
243 | } |
||
244 |
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.