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 |
||
33 | class CapabilitiesService { |
||
34 | |||
35 | /** @var IConfig */ |
||
36 | private $config; |
||
37 | /** @var IClientService */ |
||
38 | private $clientService; |
||
39 | /** @var ISimpleFolder */ |
||
40 | private $appData; |
||
41 | /** @var array */ |
||
42 | private $capabilities; |
||
43 | |||
44 | View Code Duplication | public function __construct(IConfig $config, IClientService $clientService, IAppData $appData) { |
|
53 | |||
54 | |||
55 | public function getCapabilities() { |
||
56 | if ($this->capabilities) { |
||
57 | $isCODEInstalled = $this->getContainer()->getServer()->getAppManager()->isEnabledForUser('richdocumentscode'); |
||
58 | $isCODEEnabled = strpos($this->config->getAppValue('richdocuments', 'wopi_url'), 'proxy.php?req=') !== false; |
||
59 | if($isCODEInstalled && $isCODEEnabled && count($this->capabilities) === 0) { |
||
60 | $this->refretch(); |
||
61 | } |
||
62 | return $this->capabilities; |
||
63 | } |
||
64 | try { |
||
65 | $file = $this->appData->getFile('capabilities.json'); |
||
66 | $decodedFile = \json_decode($file->getContent(), true); |
||
67 | } catch (NotFoundException $e) { |
||
68 | return []; |
||
69 | } |
||
70 | |||
71 | if (!is_array($decodedFile)) { |
||
72 | return []; |
||
73 | } |
||
74 | $this->capabilities = $decodedFile; |
||
75 | |||
76 | return $this->capabilities; |
||
77 | } |
||
78 | |||
79 | public function hasTemplateSaveAs() { |
||
80 | return $this->getCapabilities()['hasTemplateSaveAs'] ?? false; |
||
81 | } |
||
82 | |||
83 | public function hasTemplateSource() { |
||
86 | |||
87 | private function getFile() { |
||
88 | try { |
||
89 | $file = $this->appData->getFile('capabilities.json'); |
||
90 | } catch (NotFoundException $e) { |
||
91 | $file = $this->appData->newFile('capabilities.json'); |
||
92 | $file->putContent(json_encode([])); |
||
93 | } |
||
94 | |||
95 | return $file; |
||
96 | } |
||
97 | |||
98 | public function clear() { |
||
99 | $file = $this->getFile(); |
||
100 | $file->putContent(json_encode([])); |
||
101 | } |
||
102 | |||
103 | public function refretch() { |
||
111 | |||
112 | private function renewCapabilities() { |
||
113 | $remoteHost = $this->config->getAppValue('richdocuments', 'wopi_url'); |
||
114 | if ($remoteHost === '') { |
||
115 | return []; |
||
139 | |||
140 | } |
||
141 |
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.