Conditions | 17 |
Paths | 2360 |
Total Lines | 157 |
Lines | 42 |
Ratio | 26.75 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
160 | private function requestInstance(string $host): void { |
||
161 | $remoteType = $this->getRemoteType(); |
||
162 | |||
163 | $webfinger = $this->getWebfinger($host, Application::APP_SUBJECT); |
||
164 | View Code Duplication | if ($this->input->getOption('all')) { |
|
165 | $this->output->writeln('- Webfinger on <info>' . $host . '</info>'); |
||
166 | $this->output->writeln(json_encode($webfinger, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)); |
||
167 | $this->output->writeln(''); |
||
168 | } |
||
169 | |||
170 | View Code Duplication | if ($this->input->getOption('all')) { |
|
171 | $circleLink = $this->extractLink(Application::APP_REL, $webfinger); |
||
172 | $this->output->writeln('- Information about Circles app on <info>' . $host . '</info>'); |
||
173 | $this->output->writeln(json_encode($circleLink, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)); |
||
174 | $this->output->writeln(''); |
||
175 | } |
||
176 | |||
177 | $this->output->writeln('- Available services on <info>' . $host . '</info>'); |
||
178 | foreach ($webfinger->getLinks() as $link) { |
||
179 | $app = $link->getProperty('name'); |
||
180 | $ver = $link->getProperty('version'); |
||
181 | if ($app !== '') { |
||
182 | $app .= ' '; |
||
183 | } |
||
184 | if ($ver !== '') { |
||
185 | $ver = 'v' . $ver; |
||
186 | } |
||
187 | |||
188 | $this->output->writeln(' * ' . $link->getRel() . ' ' . $app . $ver); |
||
189 | } |
||
190 | $this->output->writeln(''); |
||
191 | |||
192 | $this->output->writeln('- Resources related to Circles on <info>' . $host . '</info>'); |
||
193 | $resource = $this->getResourceData($host, Application::APP_SUBJECT, Application::APP_REL); |
||
194 | $this->output->writeln(json_encode($resource, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)); |
||
195 | $this->output->writeln(''); |
||
196 | |||
197 | |||
198 | $tempUid = $resource->g('uid'); |
||
199 | $this->output->writeln( |
||
200 | '- Confirming UID=' . $tempUid . ' from parsed Signatory at <info>' . $host . '</info>' |
||
201 | ); |
||
202 | |||
203 | try { |
||
204 | $remoteSignatory = $this->remoteStreamService->retrieveSignatory($resource->g('id'), true); |
||
205 | $this->output->writeln(' * No SignatureException: <info>Identity authed</info>'); |
||
206 | } catch (SignatureException $e) { |
||
207 | $this->output->writeln( |
||
208 | '<error>' . $host . ' cannot auth its identity: ' . $e->getMessage() . '</error>' |
||
209 | ); |
||
210 | |||
211 | return; |
||
212 | } |
||
213 | |||
214 | $this->output->writeln(' * Found <info>' . $remoteSignatory->getUid() . '</info>'); |
||
215 | if ($remoteSignatory->getUid(true) !== $tempUid) { |
||
216 | $this->output->writeln('<error>looks like ' . $host . ' is faking its identity'); |
||
217 | |||
218 | return; |
||
219 | } |
||
220 | |||
221 | $this->output->writeln(''); |
||
222 | |||
223 | $testUrl = $resource->g('test'); |
||
224 | $this->output->writeln('- Testing signed payload on <info>' . $testUrl . '</info>'); |
||
225 | |||
226 | try { |
||
227 | $localSignatory = $this->remoteStreamService->getAppSignatory(); |
||
228 | } catch (SignatoryException $e) { |
||
229 | $this->output->writeln( |
||
230 | '<error>Federated Circles not enabled locally. Please run ./occ circles:remote:init</error>' |
||
231 | ); |
||
232 | |||
233 | return; |
||
234 | } |
||
235 | |||
236 | $payload = [ |
||
237 | 'test' => 42, |
||
238 | 'token' => $this->uuid() |
||
239 | ]; |
||
240 | $signedRequest = $this->outgoingTest($testUrl, $payload); |
||
241 | $this->output->writeln(' * Payload: '); |
||
242 | $this->output->writeln(json_encode($payload, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)); |
||
243 | $this->output->writeln(''); |
||
244 | |||
245 | $this->output->writeln(' * Clear Signature: '); |
||
246 | $this->output->writeln('<comment>' . $signedRequest->getClearSignature() . '</comment>'); |
||
247 | $this->output->writeln(''); |
||
248 | |||
249 | $this->output->writeln(' * Signed Signature (base64 encoded): '); |
||
250 | $this->output->writeln( |
||
251 | '<comment>' . base64_encode($signedRequest->getSignedSignature()) . '</comment>' |
||
252 | ); |
||
253 | $this->output->writeln(''); |
||
254 | |||
255 | $result = $signedRequest->getOutgoingRequest()->getResult(); |
||
256 | $code = $result->getStatusCode(); |
||
257 | $this->output->writeln(' * Result: ' . (($code === 200) ? '<info>' . $code . '</info>' : $code)); |
||
258 | $this->output->writeln( |
||
259 | json_encode(json_decode($result->getContent(), true), JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) |
||
260 | ); |
||
261 | $this->output->writeln(''); |
||
262 | |||
263 | View Code Duplication | if ($this->input->getOption('all')) { |
|
264 | $this->output->writeln(''); |
||
265 | $this->output->writeln('<info>### Complete report ###</info>'); |
||
266 | $this->output->writeln(json_encode($signedRequest, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)); |
||
267 | $this->output->writeln(''); |
||
268 | } |
||
269 | |||
270 | if ($remoteSignatory->getUid() !== $localSignatory->getUid()) { |
||
271 | $remoteSignatory->setInstance($host); |
||
272 | $remoteSignatory->setType($remoteType); |
||
273 | |||
274 | try { |
||
275 | $stored = new RemoteInstance(); |
||
276 | $this->remoteStreamService->confirmValidRemote($remoteSignatory, $stored); |
||
277 | $this->output->writeln( |
||
278 | '<info>The remote instance ' . $host |
||
279 | . ' is already known with this current identity</info>' |
||
280 | ); |
||
281 | |||
282 | View Code Duplication | if ($remoteSignatory->getType() !== $stored->getType()) { |
|
283 | $this->output->writeln( |
||
284 | '- updating type from ' . $stored->getType() . ' to ' |
||
285 | . $remoteSignatory->getType() |
||
286 | ); |
||
287 | $this->remoteStreamService->update( |
||
288 | $remoteSignatory, RemoteStreamService::UPDATE_TYPE |
||
289 | ); |
||
290 | } |
||
291 | |||
292 | View Code Duplication | if ($remoteSignatory->getInstance() !== $stored->getInstance()) { |
|
293 | $this->output->writeln( |
||
294 | '- updating host from ' . $stored->getInstance() . ' to ' |
||
295 | . $remoteSignatory->getInstance() |
||
296 | ); |
||
297 | $this->remoteStreamService->update( |
||
298 | $remoteSignatory, RemoteStreamService::UPDATE_INSTANCE |
||
299 | ); |
||
300 | } |
||
301 | View Code Duplication | if ($remoteSignatory->getId() !== $stored->getId()) { |
|
302 | $this->output->writeln( |
||
303 | '- updating href/Id from ' . $stored->getId() . ' to ' |
||
304 | . $remoteSignatory->getId() |
||
305 | ); |
||
306 | $this->remoteStreamService->update($remoteSignatory, RemoteStreamService::UPDATE_HREF); |
||
307 | } |
||
308 | |||
309 | } catch (RemoteUidException $e) { |
||
310 | $this->updateRemote($remoteSignatory); |
||
311 | } catch (RemoteNotFoundException $e) { |
||
312 | $this->saveRemote($remoteSignatory); |
||
313 | } |
||
314 | } |
||
315 | |||
316 | } |
||
317 | |||
492 |
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.