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) |
||
56 | |||
57 | /** |
||
58 | * Render all the notifications' script to insert into script tag |
||
59 | * |
||
60 | * @return string |
||
61 | * |
||
62 | */ |
||
63 | public function render(): string |
||
80 | |||
81 | /** |
||
82 | * Add a notification |
||
83 | * |
||
84 | * @param string $type Could be error, info, success, or warning. |
||
85 | * @param string $text The notification's message |
||
86 | * @param string $title The notification's title |
||
|
|||
87 | * @param array $options |
||
88 | * @param bool $onlyNextRequest if true(default), se the notification in session only for the next request |
||
89 | * |
||
90 | */ |
||
91 | public function add($theme, $timeout, $type, $layout, $text, $sounds = null, $soundsVolume = null, bool $onlyNextRequest = true) |
||
124 | |||
125 | /** |
||
126 | * Shortcut for adding an info notification |
||
127 | * |
||
128 | * @param string $message The notification's message |
||
129 | * @param string $title The notification's title |
||
130 | * @param array $options |
||
131 | */ |
||
132 | View Code Duplication | public function info($text, $onlyNextRequest = true, array $options = []) |
|
140 | |||
141 | /** |
||
142 | * Shortcut for adding an error notification |
||
143 | * |
||
144 | * @param string $message The notification's message |
||
145 | * @param string $title The notification's title |
||
146 | * @param array $options |
||
147 | */ |
||
148 | View Code Duplication | public function error($text, $onlyNextRequest = true, array $options = []) |
|
156 | |||
157 | /** |
||
158 | * Shortcut for adding a warning notification |
||
159 | * |
||
160 | * @param string $message The notification's message |
||
161 | * @param string $title The notification's title |
||
162 | * @param array $options |
||
163 | */ |
||
164 | View Code Duplication | public function warning($text, $onlyNextRequest = true, array $options = []) |
|
172 | |||
173 | /** |
||
174 | * Shortcut for adding a success notification |
||
175 | * |
||
176 | * @param string $message The notification's message |
||
177 | * @param string $title The notification's title |
||
178 | * @param array $options |
||
179 | */ |
||
180 | View Code Duplication | public function success($text, $onlyNextRequest = true, array $options = []) |
|
188 | |||
189 | |||
190 | /** |
||
191 | * Clear all notifications |
||
192 | * |
||
193 | * @param bool $withSession if true (default) clean notifications in session too. |
||
194 | */ |
||
195 | public function clear() |
||
199 | |||
200 | public function clearFlashed(bool $withSession = true) |
||
208 | |||
209 | public function clearAll(bool $withSession = true) |
||
218 | } |
||
219 |
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.