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