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 |
||
28 | class Feed extends Controller { |
||
29 | const DEFAULT_PAGE_SIZE = 30; |
||
30 | |||
31 | /** @var \OCA\Activity\Data */ |
||
32 | protected $data; |
||
33 | |||
34 | /** @var \OCA\Activity\GroupHelper */ |
||
35 | protected $helper; |
||
36 | |||
37 | /** @var \OCA\Activity\UserSettings */ |
||
38 | protected $settings; |
||
39 | |||
40 | /** @var IURLGenerator */ |
||
41 | protected $urlGenerator; |
||
42 | |||
43 | /** @var IManager */ |
||
44 | protected $activityManager; |
||
45 | |||
46 | /** @var IConfig */ |
||
47 | protected $config; |
||
48 | |||
49 | /** @var IFactory */ |
||
50 | protected $l10nFactory; |
||
51 | |||
52 | /** @var IL10N */ |
||
53 | protected $l; |
||
54 | |||
55 | /** @var string */ |
||
56 | protected $user; |
||
57 | |||
58 | /** @var string */ |
||
59 | protected $tokenUser; |
||
60 | |||
61 | /** |
||
62 | * constructor of the controller |
||
63 | * |
||
64 | * @param string $appName |
||
65 | * @param IRequest $request |
||
66 | * @param Data $data |
||
67 | * @param GroupHelper $helper |
||
68 | * @param UserSettings $settings |
||
69 | * @param IURLGenerator $urlGenerator |
||
70 | * @param IManager $activityManager |
||
71 | * @param IFactory $l10nFactory |
||
72 | * @param IConfig $config |
||
73 | * @param string $user |
||
74 | */ |
||
75 | 5 | public function __construct($appName, |
|
95 | |||
96 | /** |
||
97 | * @PublicPage |
||
98 | * @NoCSRFRequired |
||
99 | * |
||
100 | * @return TemplateResponse |
||
101 | */ |
||
102 | 4 | public function show() { |
|
154 | |||
155 | /** |
||
156 | * Parse the parameters in the subject and message |
||
157 | * |
||
158 | * @param string $message |
||
159 | * @return string |
||
160 | */ |
||
161 | protected function parseMessage($message) { |
||
166 | |||
167 | /** |
||
168 | * Parse collections |
||
169 | * |
||
170 | * @param string $message |
||
171 | * @return string |
||
172 | */ |
||
173 | protected function parseCollections($message) { |
||
201 | |||
202 | /** |
||
203 | * Parse the parameters in the subject and message |
||
204 | * |
||
205 | * @param string $message |
||
206 | * @return string |
||
207 | */ |
||
208 | protected function parseParameters($message) { |
||
215 | |||
216 | /** |
||
217 | * Display the parameter value |
||
218 | * |
||
219 | * @param string $message |
||
220 | * @return string |
||
221 | */ |
||
222 | protected function parseUntypedParameters($message) { |
||
227 | |||
228 | /** |
||
229 | * Display the users display name |
||
230 | * |
||
231 | * @param string $message |
||
232 | * @return string |
||
233 | */ |
||
234 | protected function parseUserParameters($message) { |
||
239 | |||
240 | /** |
||
241 | * Display the full cloud id |
||
242 | * |
||
243 | * @param string $message |
||
244 | * @return string |
||
245 | */ |
||
246 | protected function parseFederatedCloudIDParameters($message) { |
||
251 | |||
252 | /** |
||
253 | * Display the path for files |
||
254 | * |
||
255 | * @param string $message |
||
256 | * @return string |
||
257 | */ |
||
258 | protected function parseFileParameters($message) { |
||
263 | } |
||
264 |
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.