| Conditions | 13 |
| Paths | 256 |
| Total Lines | 62 |
| Code Lines | 40 |
| 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 |
||
| 72 | public function readData($option): array |
||
| 73 | { |
||
| 74 | $url = htmlspecialchars_decode($option['link'], ENT_NOQUOTES); |
||
| 75 | $ch = curl_init(); |
||
| 76 | if ($ch !== false) { |
||
| 77 | curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); |
||
| 78 | curl_setopt($ch, CURLOPT_HEADER, false); |
||
| 79 | curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); |
||
| 80 | curl_setopt($ch, CURLOPT_URL, $url); |
||
| 81 | curl_setopt($ch, CURLOPT_REFERER, $url); |
||
| 82 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); |
||
| 83 | curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13'); |
||
| 84 | $curlResult = curl_exec($ch); |
||
| 85 | $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
||
| 86 | curl_close($ch); |
||
| 87 | } else { |
||
| 88 | $curlResult = ''; |
||
| 89 | } |
||
| 90 | |||
| 91 | $rows = str_getcsv($curlResult, "\n"); |
||
|
|
|||
| 92 | |||
| 93 | // remove x number of rows from the beginning |
||
| 94 | if (isset($option['offset']) and is_numeric($option['offset'])) { |
||
| 95 | $rows = array_slice($rows, $option['offset']); |
||
| 96 | } |
||
| 97 | |||
| 98 | $selectedColumns = array(); |
||
| 99 | if (isset($option['columns']) && strlen($option['columns']) > 0) { |
||
| 100 | $selectedColumns = str_getcsv($option['columns'], ','); |
||
| 101 | } |
||
| 102 | |||
| 103 | // get the delimiter by reading the first row |
||
| 104 | $delimiter = $this->detectDelimiter($rows[0]); |
||
| 105 | |||
| 106 | // the first row will define the column headers, even if it is not a real header |
||
| 107 | $header = str_getcsv($rows[0], $delimiter); |
||
| 108 | |||
| 109 | // if the data has a real header, remove the first row |
||
| 110 | if (!isset($option['hasHeader']) or $option['hasHeader'] !== 'false') { |
||
| 111 | $rows = array_slice($rows, 1); |
||
| 112 | } |
||
| 113 | |||
| 114 | $data = array(); |
||
| 115 | if (count($selectedColumns) !== 0) { |
||
| 116 | // if only a subset of columns or fixed column values are set, they are replaced here |
||
| 117 | $header = $this->minimizeRow($selectedColumns, $header); |
||
| 118 | foreach ($rows as $row) { |
||
| 119 | $data[] = $this->minimizeRow($selectedColumns, str_getcsv($row, $delimiter)); |
||
| 120 | } |
||
| 121 | } else { |
||
| 122 | foreach ($rows as $row) { |
||
| 123 | $data[] = str_getcsv($row, $delimiter); |
||
| 124 | } |
||
| 125 | } |
||
| 126 | unset($rows); |
||
| 127 | |||
| 128 | return [ |
||
| 129 | 'header' => $header, |
||
| 130 | 'dimensions' => array_slice($header, 0, count($header) - 1), |
||
| 131 | 'data' => $data, |
||
| 132 | 'rawdata' => $curlResult, |
||
| 133 | 'error' => ($http_code>=200 && $http_code<300) ? 0 : 'HTTP response code: '.$http_code, |
||
| 134 | ]; |
||
| 166 | } |