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 |
||
34 | 1 | class FlashNotifier |
|
35 | { |
||
36 | /** |
||
37 | * Implement nette smart magic |
||
38 | */ |
||
39 | 1 | use Nette\SmartObject; |
|
40 | |||
41 | /** |
||
42 | * @var Storage\IStorage |
||
43 | */ |
||
44 | protected $storage; |
||
45 | |||
46 | /** |
||
47 | * @var bool |
||
48 | */ |
||
49 | protected $useTranslator; |
||
50 | |||
51 | /** |
||
52 | * @var Localization\ITranslator|NULL |
||
53 | */ |
||
54 | protected $translator; |
||
55 | |||
56 | /** |
||
57 | * @param bool $useTranslator |
||
58 | * @param Storage\IStorage $storage |
||
59 | * @param Localization\ITranslator|NULL $translator |
||
60 | */ |
||
61 | public function __construct( |
||
62 | bool $useTranslator = TRUE, |
||
63 | Storage\IStorage $storage, |
||
64 | Localization\ITranslator $translator = NULL |
||
65 | ) { |
||
66 | 1 | $this->storage = $storage; |
|
67 | 1 | $this->translator = $translator; |
|
68 | 1 | $this->useTranslator = $useTranslator; |
|
69 | 1 | } |
|
70 | |||
71 | /** |
||
72 | * Flash a success message |
||
73 | * |
||
74 | * @param string $message |
||
75 | * @param string|NULL $title |
||
76 | * |
||
77 | * @return Entities\IMessage |
||
78 | */ |
||
79 | View Code Duplication | public function success($message, $title = NULL) : Entities\IMessage |
|
86 | |||
87 | /** |
||
88 | * Flash an information message |
||
89 | * |
||
90 | * @param string $message |
||
91 | * @param string|NULL $title |
||
92 | * |
||
93 | * @return Entities\IMessage |
||
94 | */ |
||
95 | View Code Duplication | public function info($message, $title = NULL) : Entities\IMessage |
|
96 | { |
||
97 | 1 | $args = func_get_args(); |
|
98 | 1 | array_splice($args, 1, 0, [Entities\IMessage::LEVEL_INFO]); |
|
99 | |||
100 | 1 | return call_user_func_array([$this, 'setMessage'], $args); |
|
101 | } |
||
102 | |||
103 | /** |
||
104 | * Flash a warning message |
||
105 | * |
||
106 | * @param string $message |
||
107 | * @param string|NULL $title |
||
108 | * |
||
109 | * @return Entities\IMessage |
||
110 | */ |
||
111 | View Code Duplication | public function warning($message, $title = NULL) : Entities\IMessage |
|
118 | |||
119 | /** |
||
120 | * Flash an error message |
||
121 | * |
||
122 | * @param string $message |
||
123 | * @param string|NULL $title |
||
124 | * |
||
125 | * @return Entities\IMessage |
||
126 | */ |
||
127 | View Code Duplication | public function error($message, $title = NULL) : Entities\IMessage |
|
134 | |||
135 | /** |
||
136 | * Add an "important" flash to the session |
||
137 | * |
||
138 | * @return void |
||
139 | */ |
||
140 | public function important() : void |
||
144 | |||
145 | /** |
||
146 | * Flash an overlay modal |
||
147 | * |
||
148 | * @param string $message |
||
149 | * @param string|NULL $title |
||
150 | * |
||
151 | * @return Entities\IMessage |
||
152 | */ |
||
153 | public function overlay($message, $title = NULL) : Entities\IMessage |
||
169 | |||
170 | /** |
||
171 | * @param string $message |
||
172 | * @param string $level |
||
173 | * @param string|NULL $title |
||
174 | * @param bool $overlay |
||
175 | * @param int|NULL $count |
||
176 | * @param array $parameters |
||
177 | * |
||
178 | * @return Entities\IMessage |
||
179 | */ |
||
180 | public function message($message, $level = Entities\IMessage::LEVEL_INFO, $title = NULL, $overlay = FALSE, $count = NULL, array $parameters = []) : Entities\IMessage |
||
184 | |||
185 | /** |
||
186 | * Flash a general message |
||
187 | * |
||
188 | * @param string $message |
||
189 | * @param string $level |
||
190 | * @param string|NULL $title |
||
191 | * @param bool $overlay |
||
192 | * @param int|NULL $count |
||
193 | * @param array $parameters |
||
194 | * |
||
195 | * @return Entities\IMessage |
||
196 | */ |
||
197 | public function setMessage($message, $level = Entities\IMessage::LEVEL_INFO, $title = NULL, $overlay = FALSE, $count = NULL, array $parameters = []) : Entities\IMessage |
||
252 | |||
253 | /** |
||
254 | * @param Entities\IMessage $flash |
||
255 | * @param Entities\IMessage[] $messages |
||
256 | * |
||
257 | * @return bool |
||
258 | */ |
||
259 | private function checkUnique(Entities\IMessage $flash, array $messages) : bool |
||
269 | |||
270 | /** |
||
271 | * @param array $attributes |
||
272 | * @param string $type |
||
273 | * @param mixed $default |
||
274 | * |
||
275 | * @return mixed |
||
276 | */ |
||
277 | private function checkForAttribute(array $attributes, string $type, $default) |
||
311 | } |
||
312 |
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.