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 declare(strict_types=1); |
||
51 | class GlobalScaleService { |
||
52 | |||
53 | |||
54 | use TNC21Request; |
||
55 | use TStringTools; |
||
56 | |||
57 | |||
58 | /** @var IURLGenerator */ |
||
59 | private $urlGenerator; |
||
60 | |||
61 | /** @var IUserManager */ |
||
62 | private $userManager; |
||
63 | |||
64 | /** @var IUserSession */ |
||
65 | private $userSession; |
||
66 | |||
67 | /** @var Signer */ |
||
68 | private $signer; |
||
69 | |||
70 | /** @var RemoteWrapperRequest */ |
||
71 | private $remoteWrapperRequest; |
||
72 | |||
73 | /** @var ConfigService */ |
||
74 | private $configService; |
||
75 | |||
76 | /** @var MiscService */ |
||
77 | private $miscService; |
||
78 | |||
79 | |||
80 | /** |
||
81 | * GlobalScaleService constructor. |
||
82 | * |
||
83 | * @param IURLGenerator $urlGenerator |
||
84 | * @param IUserManager $userManager |
||
85 | * @param IUserSession $userSession |
||
86 | * @param Signer $signer |
||
87 | * @param RemoteWrapperRequest $remoteWrapperRequest |
||
88 | * @param ConfigService $configService |
||
89 | * @param MiscService $miscService |
||
90 | */ |
||
91 | View Code Duplication | public function __construct( |
|
108 | |||
109 | |||
110 | /** |
||
111 | * @return array |
||
112 | * @throws GSStatusException |
||
113 | */ |
||
114 | public function getGlobalScaleInstances(): array { |
||
132 | |||
133 | |||
134 | |||
135 | // /** |
||
136 | // * @param GSEvent $event |
||
137 | // * |
||
138 | // * @return string |
||
139 | // */ |
||
140 | // public function asyncBroadcast(GSEvent $event): string { |
||
141 | // $wrapper = new GSWrapper(); |
||
142 | // $wrapper->setEvent($event); |
||
143 | // $wrapper->setToken($this->uuid()); |
||
144 | // $wrapper->setCreation(time()); |
||
145 | // $wrapper->setSeverity($event->getSeverity()); |
||
146 | // |
||
147 | // foreach ($this->getInstances($event->isAsync()) as $instance) { |
||
148 | // $wrapper->setInstance($instance); |
||
149 | // $wrapper = $this->remoteWrapperRequest->create($wrapper); |
||
150 | // } |
||
151 | // |
||
152 | // $request = new NC21Request('', Request::TYPE_POST); |
||
153 | // $this->configService->configureRequest( |
||
154 | // $request, 'circles.RemoteWrapper.asyncBroadcast', ['token' => $wrapper->getToken()] |
||
155 | // ); |
||
156 | // |
||
157 | // try { |
||
158 | // $this->doRequest($request); |
||
159 | // } catch (RequestContentException | RequestNetworkException | RequestResultSizeException | RequestServerException $e) { |
||
160 | // $this->miscService->e($e); |
||
161 | // } |
||
162 | // |
||
163 | // return $wrapper->getToken(); |
||
164 | // } |
||
165 | // |
||
166 | // |
||
167 | // /** |
||
168 | // * @param GSEvent $event |
||
169 | // * |
||
170 | // * @return AGlobalScaleEvent |
||
171 | // * @throws GlobalScaleEventException |
||
172 | // */ |
||
173 | // public function getGlobalScaleEvent(GSEvent $event): AGlobalScaleEvent { |
||
174 | // $class = $this->getClassNameFromEvent($event); |
||
175 | // try { |
||
176 | // $gs = OC::$server->query($class); |
||
177 | // if (!$gs instanceof AGlobalScaleEvent) { |
||
178 | // throw new GlobalScaleEventException($class . ' not an AGlobalScaleEvent'); |
||
179 | // } |
||
180 | // |
||
181 | // return $gs; |
||
182 | // } catch (QueryException $e) { |
||
183 | // throw new GlobalScaleEventException('AGlobalScaleEvent ' . $class . ' not found'); |
||
184 | // } |
||
185 | // } |
||
186 | // |
||
187 | // |
||
188 | // /** |
||
189 | // * @return string |
||
190 | // */ |
||
191 | // public function getKey(): string { |
||
192 | // try { |
||
193 | // $key = $this->configService->getGSStatus(ConfigService::GS_KEY); |
||
194 | // } catch (GSStatusException $e) { |
||
195 | // $key = $this->configService->getAppValue(ConfigService::CIRCLES_LOCAL_GSKEY); |
||
196 | // if ($key === '') { |
||
197 | // $key = $this->token(31); |
||
198 | // $this->configService->setAppValue(ConfigService::CIRCLES_LOCAL_GSKEY, $key); |
||
199 | // } |
||
200 | // } |
||
201 | // |
||
202 | // return md5('gskey:' . $key); |
||
203 | // } |
||
204 | // |
||
205 | // /** |
||
206 | // * @param string $key |
||
207 | // * |
||
208 | // * @throws GSKeyException |
||
209 | // */ |
||
210 | // public function checkKey(string $key) { |
||
211 | // if ($key !== $this->getKey()) { |
||
212 | // throw new GSKeyException('invalid key'); |
||
213 | // } |
||
214 | // } |
||
215 | // |
||
216 | // |
||
217 | // /** |
||
218 | // * @param GSEvent $event |
||
219 | // * |
||
220 | // * @throws GSKeyException |
||
221 | // */ |
||
222 | // public function checkEvent(GSEvent $event): void { |
||
223 | // $this->checkKey($event->getKey()); |
||
224 | // } |
||
225 | // |
||
226 | // |
||
227 | // /** |
||
228 | // * @param GSEvent $event |
||
229 | // * |
||
230 | // * @return string |
||
231 | // * @throws GlobalScaleEventException |
||
232 | // */ |
||
233 | // private function getClassNameFromEvent(GSEvent $event): string { |
||
234 | // $className = $event->getType(); |
||
235 | // if (substr($className, 0, 25) !== '\OCA\Circles\GlobalScale\\' || strpos($className, '.')) { |
||
236 | // throw new GlobalScaleEventException( |
||
237 | // $className . ' does not seems to be a secured AGlobalScaleEvent' |
||
238 | // ); |
||
239 | // } |
||
240 | // |
||
241 | // return $className; |
||
242 | // } |
||
243 | // |
||
244 | // |
||
245 | // /** |
||
246 | // * @return IUser |
||
247 | // * @throws NoUserException |
||
248 | // */ |
||
249 | // private function getRandomUser(): IUser { |
||
250 | // $user = $this->userSession->getUser(); |
||
251 | // if ($user !== null) { |
||
252 | // return $user; |
||
253 | // } |
||
254 | // |
||
255 | // $random = $this->userManager->search('', 1); |
||
256 | // if (sizeof($random) > 0) { |
||
257 | // return array_shift($random); |
||
258 | // } |
||
259 | // |
||
260 | // throw new NoUserException(); |
||
261 | // } |
||
262 | |||
263 | } |
||
264 | |||
265 |
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.