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 |
||
29 | class Application extends App { |
||
30 | |||
31 | /** |
||
32 | * @param array $params |
||
33 | */ |
||
34 | public function __construct($params=[]) { |
||
35 | parent::__construct('calendar', $params); |
||
36 | $container = $this->getContainer(); |
||
37 | |||
38 | $container->registerService('ContactController', function(IAppContainer $c) { |
||
39 | $request = $c->query('Request'); |
||
40 | $contacts = $c->getServer()->getContactsManager(); |
||
41 | |||
42 | return new Controller\ContactController($c->getAppName(), $request, $contacts); |
||
43 | }); |
||
44 | |||
45 | View Code Duplication | $container->registerService('EmailController', function(IAppContainer $c) { |
|
|
|||
46 | $request = $c->query('Request'); |
||
47 | $userSession = $c->getServer()->getUserSession(); |
||
48 | $config = $c->getServer()->getConfig(); |
||
49 | $mailer = $c->getServer()->getMailer(); |
||
50 | $l10n = $c->getServer()->getL10N($c->query('AppName')); |
||
51 | $defaults = new \OCP\Defaults(); |
||
52 | |||
53 | return new Controller\EmailController($c->getAppName(), $request, $userSession, $config, $mailer, $l10n, $defaults); |
||
54 | }); |
||
55 | |||
56 | View Code Duplication | $container->registerService('ProxyController', function(IAppContainer $c) { |
|
57 | $request = $c->query('Request'); |
||
58 | $client = $c->getServer()->getHTTPClientService(); |
||
59 | $l10n = $c->getServer()->getL10N($c->query('AppName')); |
||
60 | $logger = $c->getServer()->getLogger(); |
||
61 | $config = $c->getServer()->getConfig(); |
||
62 | |||
63 | return new Controller\ProxyController($c->getAppName(), $request, $client, $l10n, $logger, $config); |
||
64 | }); |
||
65 | |||
66 | $container->registerService('SettingsController', function(IAppContainer $c) { |
||
67 | $request = $c->query('Request'); |
||
68 | $config = $c->getServer()->getConfig(); |
||
69 | $userSession = $c->getServer()->getUserSession(); |
||
70 | |||
71 | return new Controller\SettingsController($c->getAppName(), $request, $userSession, $config); |
||
72 | }); |
||
73 | |||
74 | $container->registerService('TimezoneController', function(IAppContainer $c) { |
||
75 | $request = $c->query('Request'); |
||
76 | |||
77 | return new Controller\TimezoneController($c->getAppName(), $request); |
||
78 | }); |
||
79 | |||
80 | $container->registerService('ViewController', function(IAppContainer $c) { |
||
81 | $request = $c->query('Request'); |
||
82 | $userSession = $c->getServer()->getUserSession(); |
||
83 | $config = $c->getServer()->getConfig(); |
||
84 | $urlGenerator = $c->getServer()->getURLGenerator(); |
||
85 | |||
86 | return new Controller\ViewController($c->getAppName(), $request, $userSession, $config, $urlGenerator); |
||
87 | }); |
||
88 | } |
||
89 | |||
90 | /** |
||
91 | * register navigation entry |
||
92 | */ |
||
93 | public function registerNavigation() { |
||
109 | } |
||
110 |
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.