Conditions | 13 |
Paths | 14 |
Total Lines | 79 |
Code Lines | 47 |
Lines | 0 |
Ratio | 0 % |
Changes | 8 | ||
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 |
||
47 | public function report(array $collectionData): void |
||
48 | { |
||
49 | $endpoint = $this->getPreparedEndpoint(); |
||
50 | |||
51 | $client = new Client(); |
||
52 | |||
53 | $payload = [ |
||
54 | 'userId' => $this->userId, |
||
55 | 'data' => $collectionData |
||
56 | ]; |
||
57 | |||
58 | try { |
||
59 | $response = $client->post($endpoint, [ |
||
60 | RequestOptions::JSON => $payload, |
||
61 | RequestOptions::TIMEOUT => 5, |
||
62 | RequestOptions::CONNECT_TIMEOUT => 2 |
||
63 | ]); |
||
64 | } catch (ConnectException $e) { |
||
65 | throw new RuntimeException('Unable to connect to ' . $endpoint . '. Message: ' . $e->getMessage()); |
||
66 | } catch (ServerException $e) { |
||
67 | var_dump($e->getResponse()->getBody()->getContents()); |
||
|
|||
68 | throw new RuntimeException('Unable to connect to ' . $endpoint . ' (ServerException). Message: ' . $e->getMessage()); |
||
69 | } catch (Exception $e) { |
||
70 | // var_dump($e->getMessage()); |
||
71 | var_dump('ahh'); |
||
72 | throw $e; |
||
73 | } |
||
74 | |||
75 | $result = json_decode((string)$response->getBody(), true); |
||
76 | |||
77 | if (!is_array($result) || !array_key_exists('status', $result)) { |
||
78 | throw new RuntimeException('Unknown error.'); |
||
79 | } |
||
80 | |||
81 | if ($result['status'] !== 'SUCCESS') { |
||
82 | throw new RuntimeException($result['message']); |
||
83 | } |
||
84 | |||
85 | $table = new Table($this->output); |
||
86 | $table->setHeaders(['Severity', 'Name', 'Description', 'Assets']); |
||
87 | |||
88 | $hints = $result['data']['hints']; |
||
89 | $lastIndex = count($hints) - 1; |
||
90 | |||
91 | foreach ($hints as $i => $hint) { |
||
92 | $row = [ |
||
93 | 'severity' => self::SEVERITIES[$hint['definition']['severity']], |
||
94 | 'name' => $hint['definition']['name'], |
||
95 | 'description' => wordwrap($hint['definition']['description'], 40) |
||
96 | ]; |
||
97 | |||
98 | $assets = []; |
||
99 | |||
100 | if (array_key_exists('files', $hint['issue']['parameters'])) { |
||
101 | $assets = array_keys($hint['issue']['parameters']['files']); |
||
102 | foreach ($assets as $key => $asset) { |
||
103 | $assets[$key] = '- ' . $asset; |
||
104 | } |
||
105 | } |
||
106 | |||
107 | if (array_key_exists('websites', $hint['issue']['parameters'])) { |
||
108 | $assets = $hint['issue']['parameters']['websites']; |
||
109 | foreach ($assets as $key => $asset) { |
||
110 | $assets[$key] = '- https://' . $asset; |
||
111 | } |
||
112 | } |
||
113 | |||
114 | $row['assets'] = implode("\n", $assets); |
||
115 | |||
116 | $table->addRow($row); |
||
117 | |||
118 | if ($i < $lastIndex) { |
||
119 | $table->addRow(new TableSeparator()); |
||
120 | } |
||
121 | } |
||
122 | |||
123 | $table->render(); |
||
124 | |||
125 | $duration = time() - $this->startTime; |
||
126 | |||
144 |