| Conditions | 17 |
| Paths | 288 |
| Total Lines | 76 |
| Code Lines | 56 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 4 | ||
| 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 |
||
| 68 | public function readData($option): array |
||
| 69 | { |
||
| 70 | $string = $option['url']; |
||
| 71 | $path = $option['path']; |
||
| 72 | $auth = $option['auth']; |
||
| 73 | $post = ($option['method'] === 'POST') ? true : false; |
||
| 74 | $contentType = ($option['content-type'] && $option['content-type'] !== '') ? $option['content-type'] : 'application/json'; |
||
| 75 | $data = array(); |
||
| 76 | |||
| 77 | $ch = curl_init(); |
||
| 78 | if ($ch !== false) { |
||
| 79 | curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); |
||
| 80 | curl_setopt($ch, CURLOPT_URL, $string); |
||
| 81 | curl_setopt($ch, CURLOPT_POST, $post); |
||
| 82 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); |
||
| 83 | curl_setopt($ch, CURLOPT_HTTPHEADER, array( |
||
| 84 | 'OCS-APIRequest: true', |
||
| 85 | 'Content-Type: ' . $contentType |
||
| 86 | )); |
||
| 87 | curl_setopt($ch, CURLOPT_USERPWD, $auth); |
||
| 88 | curl_setopt($ch, CURLOPT_VERBOSE, true); |
||
| 89 | if ($option['body'] && $option['body'] !== '') { |
||
| 90 | curl_setopt($ch, CURLOPT_POSTFIELDS, $option['body']); |
||
| 91 | } |
||
| 92 | $curlResult = curl_exec($ch); |
||
| 93 | $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
||
| 94 | curl_close($ch); |
||
| 95 | } else { |
||
| 96 | $curlResult = ''; |
||
| 97 | } |
||
| 98 | |||
| 99 | $json = json_decode($curlResult, true); |
||
|
|
|||
| 100 | |||
| 101 | // check if an array of values should be extracted |
||
| 102 | preg_match_all("/(?<={).*(?=})/", $path, $matches); |
||
| 103 | if (count($matches[0]) > 0) { |
||
| 104 | // array extraction |
||
| 105 | $paths = explode(',', $matches[0][0]); |
||
| 106 | foreach ($json as $rowArray) { |
||
| 107 | $dim1 = $rowArray[$paths[0]] ?: $paths[0]; |
||
| 108 | $dim2 = $rowArray[$paths[1]] ?: $paths[1]; |
||
| 109 | $val = $rowArray[$paths[2]] ?: $paths[2]; |
||
| 110 | array_push($data, [$dim1, $dim2, $val]); |
||
| 111 | } |
||
| 112 | } else { |
||
| 113 | // single value extraction |
||
| 114 | $paths = explode(',', $path); |
||
| 115 | foreach ($paths as $singlePath) { |
||
| 116 | $array = $this->get_nested_array_value($json, $singlePath); |
||
| 117 | |||
| 118 | if (is_array($array)) { |
||
| 119 | foreach ($array as $key => $value) { |
||
| 120 | $pathArray = explode('/', $singlePath); |
||
| 121 | $group = end($pathArray); |
||
| 122 | array_push($data, [$group, $key, $value]); |
||
| 123 | } |
||
| 124 | } else { |
||
| 125 | $pathArray = explode('/', $singlePath); |
||
| 126 | $key = end($pathArray); |
||
| 127 | array_push($data, ['', $key, $array]); |
||
| 128 | } |
||
| 129 | } |
||
| 130 | } |
||
| 131 | |||
| 132 | |||
| 133 | $header = array(); |
||
| 134 | $header[0] = ''; |
||
| 135 | $header[1] = 'Key'; |
||
| 136 | $header[2] = 'Value'; |
||
| 137 | |||
| 138 | return [ |
||
| 139 | 'header' => $header, |
||
| 140 | 'dimensions' => array_slice($header, 0, count($header) - 1), |
||
| 141 | 'data' => $data, |
||
| 142 | 'rawdata' => $curlResult, |
||
| 143 | 'error' => ($http_code>=200 && $http_code<300) ? 0 : 'HTTP response code: '.$http_code, |
||
| 144 | ]; |
||
| 165 | } |