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 |
||
36 | 1 | class FlashNotifier |
|
37 | { |
||
38 | /** |
||
39 | * Implement nette smart magic |
||
40 | */ |
||
41 | 1 | use Nette\SmartObject; |
|
42 | |||
43 | /** |
||
44 | * @var Storage\IStorage |
||
45 | */ |
||
46 | protected $storage; |
||
47 | |||
48 | /** |
||
49 | * @var bool |
||
50 | */ |
||
51 | protected $useTranslator; |
||
52 | |||
53 | /** |
||
54 | * @var Localization\ITranslator|NULL |
||
55 | */ |
||
56 | protected $translator; |
||
57 | |||
58 | /** |
||
59 | * @param bool $useTranslator |
||
60 | * @param Storage\IStorage $storage |
||
61 | * @param Localization\ITranslator|NULL $translator |
||
62 | */ |
||
63 | public function __construct( |
||
72 | |||
73 | /** |
||
74 | * Flash a success message |
||
75 | * |
||
76 | * @param string $message |
||
77 | * @param string|NULL $title |
||
78 | * |
||
79 | * @return Entities\IMessage |
||
80 | */ |
||
81 | View Code Duplication | public function success($message, $title = NULL) : Entities\IMessage |
|
88 | |||
89 | /** |
||
90 | * Flash an information message |
||
91 | * |
||
92 | * @param string $message |
||
93 | * @param string|NULL $title |
||
94 | * |
||
95 | * @return Entities\IMessage |
||
96 | */ |
||
97 | View Code Duplication | public function info($message, $title = NULL) : Entities\IMessage |
|
104 | |||
105 | /** |
||
106 | * Flash a warning message |
||
107 | * |
||
108 | * @param string $message |
||
109 | * @param string|NULL $title |
||
110 | * |
||
111 | * @return Entities\IMessage |
||
112 | */ |
||
113 | View Code Duplication | public function warning($message, $title = NULL) : Entities\IMessage |
|
120 | |||
121 | /** |
||
122 | * Flash an error message |
||
123 | * |
||
124 | * @param string $message |
||
125 | * @param string|NULL $title |
||
126 | * |
||
127 | * @return Entities\IMessage |
||
128 | */ |
||
129 | View Code Duplication | public function error($message, $title = NULL) : Entities\IMessage |
|
136 | |||
137 | /** |
||
138 | * Add an "important" flash to the session |
||
139 | * |
||
140 | * @return void |
||
141 | */ |
||
142 | public function important() : void |
||
146 | |||
147 | /** |
||
148 | * Flash an overlay modal |
||
149 | * |
||
150 | * @param string $message |
||
151 | * @param string|NULL $title |
||
152 | * |
||
153 | * @return Entities\IMessage |
||
154 | */ |
||
155 | public function overlay($message, $title = NULL) : Entities\IMessage |
||
171 | |||
172 | /** |
||
173 | * @param string $message |
||
174 | * @param string $level |
||
175 | * @param string|NULL $title |
||
176 | * @param bool $overlay |
||
177 | * @param int|NULL $count |
||
178 | * @param array $parameters |
||
179 | * |
||
180 | * @return Entities\IMessage |
||
181 | */ |
||
182 | public function message($message, $level = Entities\IMessage::LEVEL_INFO, $title = NULL, $overlay = FALSE, $count = NULL, array $parameters = []) : Entities\IMessage |
||
186 | |||
187 | /** |
||
188 | * Flash a general message |
||
189 | * |
||
190 | * @param string $message |
||
191 | * @param string $level |
||
192 | * @param string|NULL $title |
||
193 | * @param bool $overlay |
||
194 | * @param int|NULL $count |
||
195 | * @param array $parameters |
||
196 | * |
||
197 | * @return Entities\IMessage |
||
198 | */ |
||
199 | public function setMessage($message, $level = Entities\IMessage::LEVEL_INFO, $title = NULL, $overlay = FALSE, $count = NULL, array $parameters = []) : Entities\IMessage |
||
264 | |||
265 | /** |
||
266 | * @param Entities\IMessage $flash |
||
267 | * @param Entities\IMessage[] $messages |
||
268 | * |
||
269 | * @return bool |
||
270 | */ |
||
271 | private function checkUnique(Entities\IMessage $flash, array $messages) : bool |
||
281 | |||
282 | /** |
||
283 | * @param array $attributes |
||
284 | * @param string $type |
||
285 | * @param mixed $default |
||
286 | * |
||
287 | * @return mixed |
||
288 | */ |
||
289 | private function checkForAttribute(array $attributes, string $type, $default) |
||
323 | } |
||
324 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.