| Conditions | 13 |
| Paths | 160 |
| Total Lines | 65 |
| Code Lines | 46 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| 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 |
||
| 67 | public function readData($option): array |
||
| 68 | { |
||
| 69 | $data = array(); |
||
| 70 | $header = array(); |
||
| 71 | $headerrow = 0; |
||
| 72 | $selectedColumns = array(); |
||
| 73 | |||
| 74 | $ch = curl_init(); |
||
| 75 | if ($ch !== false) { |
||
| 76 | curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); |
||
| 77 | curl_setopt($ch, CURLOPT_HEADER, false); |
||
| 78 | curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); |
||
| 79 | curl_setopt($ch, CURLOPT_URL, $option['link']); |
||
| 80 | curl_setopt($ch, CURLOPT_REFERER, $option['link']); |
||
| 81 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); |
||
| 82 | 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'); |
||
| 83 | $curlResult = curl_exec($ch); |
||
| 84 | $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
||
| 85 | curl_close($ch); |
||
| 86 | } else { |
||
| 87 | $curlResult = ''; |
||
| 88 | } |
||
| 89 | |||
| 90 | $rows = str_getcsv($curlResult, "\n"); |
||
|
|
|||
| 91 | |||
| 92 | // remove x number of rows from the beginning |
||
| 93 | if (isset($option['offset']) and is_numeric($option['offset'])) { |
||
| 94 | $rows = array_slice($rows, $option['offset']); |
||
| 95 | } |
||
| 96 | |||
| 97 | if (isset($option['columns']) && strlen($option['columns']) > 0) { |
||
| 98 | $selectedColumns = str_getcsv($option['columns'], ','); |
||
| 99 | } |
||
| 100 | $delimiter = $this->detectDelimiter($rows[0]); |
||
| 101 | |||
| 102 | foreach ($rows as &$row) { |
||
| 103 | $row = str_getcsv($row, $delimiter); |
||
| 104 | $rowMinimized = array(); |
||
| 105 | |||
| 106 | if (count($selectedColumns) !== 0) { |
||
| 107 | foreach ($selectedColumns as $selectedColumn) { |
||
| 108 | if (is_numeric($selectedColumn)) { |
||
| 109 | array_push($rowMinimized, $row[$selectedColumn - 1]); |
||
| 110 | } else { |
||
| 111 | array_push($rowMinimized, $selectedColumn); |
||
| 112 | } |
||
| 113 | } |
||
| 114 | } else { |
||
| 115 | $rowMinimized = $row; |
||
| 116 | } |
||
| 117 | |||
| 118 | if ($headerrow === 0) { |
||
| 119 | $header = $rowMinimized; |
||
| 120 | $headerrow = 1; |
||
| 121 | } else { |
||
| 122 | array_push($data, $rowMinimized); |
||
| 123 | } |
||
| 124 | } |
||
| 125 | |||
| 126 | return [ |
||
| 127 | 'header' => $header, |
||
| 128 | 'dimensions' => array_slice($header, 0, count($header) - 1), |
||
| 129 | 'data' => $data, |
||
| 130 | 'rawdata' => $curlResult, |
||
| 131 | 'error' => ($http_code>=200 && $http_code<300) ? 0 : 'HTTP response code: '.$http_code, |
||
| 132 | ]; |
||
| 150 | } |