| Conditions | 21 |
| Paths | 1344 |
| Total Lines | 102 |
| Code Lines | 69 |
| Lines | 0 |
| Ratio | 0 % |
| 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 |
||
| 68 | public function readData($option): array |
||
| 69 | { |
||
| 70 | $url = htmlspecialchars_decode($option['url'], ENT_NOQUOTES); |
||
| 71 | $path = $option['path']; |
||
| 72 | $auth = $option['auth']; |
||
| 73 | $post = $option['method'] === 'POST'; |
||
| 74 | $contentType = ($option['content-type'] && $option['content-type'] !== '') ? $option['content-type'] : 'application/json'; |
||
| 75 | $data = array(); |
||
| 76 | $http_code = ''; |
||
| 77 | $headers = ($option['customHeaders'] && $option['customHeaders'] !== '') ? explode(",", $option['customHeaders']) : []; |
||
| 78 | $headers = array_map('trim', $headers); |
||
| 79 | $headers[] = 'OCS-APIRequest: true'; |
||
| 80 | $headers[] = 'Content-Type: ' . $contentType; |
||
| 81 | # VERITYHOST=0 to disable verification, 2 to enable. 1 is no longer a valid option. |
||
| 82 | $verifyHost = intval($option['insecure']); |
||
| 83 | |||
| 84 | $ch = curl_init(); |
||
| 85 | if ($ch !== false) { |
||
| 86 | curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); |
||
| 87 | curl_setopt($ch, CURLOPT_URL, $url); |
||
| 88 | curl_setopt($ch, CURLOPT_POST, $post); |
||
| 89 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); |
||
| 90 | curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); |
||
| 91 | curl_setopt($ch, CURLOPT_USERPWD, $auth); |
||
| 92 | curl_setopt($ch, CURLOPT_VERBOSE, true); |
||
| 93 | curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, $verifyHost); |
||
| 94 | if ($option['body'] && $option['body'] !== '') { |
||
| 95 | curl_setopt($ch, CURLOPT_POSTFIELDS, $option['body']); |
||
| 96 | } |
||
| 97 | $rawResult = curl_exec($ch); |
||
| 98 | $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
||
| 99 | curl_close($ch); |
||
| 100 | } else { |
||
| 101 | $rawResult = ''; |
||
| 102 | } |
||
| 103 | |||
| 104 | $json = json_decode($rawResult, true); |
||
| 105 | |||
| 106 | // check if a specific array of values should be extracted |
||
| 107 | // e.g. {BTC,tmsp,price} |
||
| 108 | preg_match_all("/(?<={).*(?=})/", $path, $matches); |
||
| 109 | if (count($matches[0]) > 0) { |
||
| 110 | // array extraction |
||
| 111 | |||
| 112 | // check if absolute path is in front of the array |
||
| 113 | // e.g. data/data{from,to,intensity/forecast} |
||
| 114 | $firstArray = strpos($path, '{'); |
||
| 115 | if ($firstArray && $firstArray !== 0) { |
||
| 116 | $singlePath = substr($path, 0, $firstArray); |
||
| 117 | $json = $this->get_nested_array_value($json, $singlePath); |
||
| 118 | } |
||
| 119 | |||
| 120 | // separate the fields of the array {BTC,tmsp,price} |
||
| 121 | $paths = explode(',', $matches[0][0]); |
||
| 122 | // fill up with dummies in case of missing columns |
||
| 123 | while (count($paths) < 3) { |
||
| 124 | array_unshift($paths, 'empty'); |
||
| 125 | } |
||
| 126 | foreach ($json as $rowArray) { |
||
| 127 | // get the array fields from the json |
||
| 128 | // if no match is not found, the field name will be used as a constant string |
||
| 129 | $dim1 = $this->get_nested_array_value($rowArray, $paths[0]) ?: $paths[0]; |
||
| 130 | $dim2 = $this->get_nested_array_value($rowArray, $paths[1]) ?: $paths[1]; |
||
| 131 | $val = $this->get_nested_array_value($rowArray, $paths[2]) ?: $paths[2]; |
||
| 132 | $data[] = [$dim1, $dim2, $val]; |
||
| 133 | } |
||
| 134 | } else { |
||
| 135 | // single value extraction |
||
| 136 | // e.g. data/currentHashrate,data/averageHashrate |
||
| 137 | $paths = explode(',', $path); |
||
| 138 | foreach ($paths as $singlePath) { |
||
| 139 | // e.g. data/currentHashrate |
||
| 140 | $array = $this->get_nested_array_value($json, $singlePath); |
||
| 141 | |||
| 142 | if (is_array($array)) { |
||
| 143 | // if the tartet is an array itself |
||
| 144 | foreach ($array as $key => $value) { |
||
| 145 | $pathArray = explode('/', $singlePath); |
||
| 146 | $group = end($pathArray); |
||
| 147 | $data[] = [$group, $key, $value]; |
||
| 148 | } |
||
| 149 | } else { |
||
| 150 | $pathArray = explode('/', $singlePath); |
||
| 151 | $key = end($pathArray); |
||
| 152 | $data[] = ['', $key, $array]; |
||
| 153 | } |
||
| 154 | } |
||
| 155 | } |
||
| 156 | |||
| 157 | $header = array(); |
||
| 158 | $header[0] = ''; |
||
| 159 | $header[1] = 'Key'; |
||
| 160 | $header[2] = 'Value'; |
||
| 161 | |||
| 162 | return [ |
||
| 163 | 'header' => $header, |
||
| 164 | 'dimensions' => array_slice($header, 0, count($header) - 1), |
||
| 165 | 'data' => $data, |
||
| 166 | 'rawdata' => $rawResult, |
||
| 167 | 'customHeaders' => $headers, |
||
| 168 | 'URL' => $url, |
||
| 169 | 'error' => ($http_code >= 200 && $http_code < 300) ? 0 : 'HTTP response code: ' . $http_code, |
||
| 170 | ]; |
||
| 190 | } |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths