Conditions | 10 |
Paths | 10 |
Total Lines | 63 |
Code Lines | 35 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 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 |
||
37 | public function report(array $collectionData): void |
||
38 | { |
||
39 | $endpoint = $this->getPreparedEndpoint(); |
||
40 | |||
41 | $client = new Client(); |
||
42 | |||
43 | $payload = [ |
||
44 | 'userId' => $this->userId, |
||
45 | 'data' => $collectionData |
||
46 | ]; |
||
47 | |||
48 | try { |
||
49 | $response = $client->post($endpoint, [ |
||
50 | RequestOptions::JSON => $payload, |
||
51 | RequestOptions::TIMEOUT => 5, |
||
52 | RequestOptions::CONNECT_TIMEOUT => 2 |
||
53 | ]); |
||
54 | } catch (ConnectException $e) { |
||
55 | throw new RuntimeException('Unable to connect to ' . $endpoint . '. Message: ' . $e->getMessage()); |
||
56 | } catch (ServerException $e) { |
||
57 | throw new RuntimeException('Unable to connect to ' . $endpoint . ' (ServerException). Message: ' . $e->getMessage()); |
||
58 | } catch (Exception $e) { |
||
59 | // var_dump($e->getMessage()); |
||
60 | throw $e; |
||
61 | } |
||
62 | |||
63 | // var_dump((string)$response->getBody());die; |
||
64 | |||
65 | $result = json_decode((string)$response->getBody(), true); |
||
66 | |||
67 | if (!is_array($result) || !array_key_exists('status', $result)) { |
||
68 | throw new RuntimeException('Unknown error.'); |
||
69 | } |
||
70 | |||
71 | if ($result['status'] !== 'SUCCESS') { |
||
72 | throw new RuntimeException($result['message']); |
||
73 | } |
||
74 | |||
75 | $table = new Table($this->output); |
||
76 | $table->setHeaders(['Name', 'Description', 'Assets']); |
||
77 | |||
78 | foreach ($result['data']['hints'] as $hint) { |
||
79 | $row = [ |
||
80 | 'name' => $hint['definition']['name'], |
||
81 | 'description' => wordwrap($hint['definition']['description'], 40) |
||
82 | ]; |
||
83 | |||
84 | $assets = []; |
||
85 | |||
86 | if (array_key_exists('files', $hint['issue']['parameters'])) { |
||
87 | $assets = array_keys($hint['issue']['parameters']['files']); |
||
88 | } |
||
89 | |||
90 | if (array_key_exists('webistes', $hint['issue']['parameters'])) { |
||
91 | $assets = $hint['issue']['parameters']['files']; |
||
92 | } |
||
93 | |||
94 | $row['assets'] = implode("\n", $assets); |
||
95 | |||
96 | $table->addRow($row); |
||
97 | } |
||
98 | |||
99 | $table->render(); |
||
100 | } |
||
110 |