| Conditions | 16 |
| Paths | 578 |
| Total Lines | 124 |
| 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 |
||
| 46 | public function parse($filepath=FALSE, $column_headers=FALSE, $detect_line_endings=FALSE, $initial_line=FALSE, $delimiter=FALSE) |
||
| 47 | { |
||
| 48 | // Raise memory limit (for big files) |
||
| 49 | ini_set('memory_limit', '128M'); |
||
| 50 | |||
| 51 | // File path |
||
| 52 | if(! $filepath) |
||
|
|
|||
| 53 | { |
||
| 54 | $filepath = $this->_get_filepath(); |
||
| 55 | } |
||
| 56 | else |
||
| 57 | { |
||
| 58 | // If filepath provided, set it |
||
| 59 | $this->_set_filepath($filepath); |
||
| 60 | } |
||
| 61 | |||
| 62 | // If file doesn't exists, return false |
||
| 63 | if(! file_exists($filepath)) |
||
| 64 | { |
||
| 65 | return FALSE; |
||
| 66 | } |
||
| 67 | |||
| 68 | // auto detect row endings |
||
| 69 | if(! $detect_line_endings) |
||
| 70 | { |
||
| 71 | $detect_line_endings = $this->_get_detect_line_endings(); |
||
| 72 | } |
||
| 73 | else |
||
| 74 | { |
||
| 75 | // If detect_line_endings provided, set it |
||
| 76 | $this->_set_detect_line_endings($detect_line_endings); |
||
| 77 | } |
||
| 78 | |||
| 79 | // If true, auto detect row endings |
||
| 80 | if($detect_line_endings) |
||
| 81 | { |
||
| 82 | ini_set("auto_detect_line_endings", TRUE); |
||
| 83 | } |
||
| 84 | |||
| 85 | // Parse from this line on |
||
| 86 | if(! $initial_line) |
||
| 87 | { |
||
| 88 | $initial_line = $this->_get_initial_line(); |
||
| 89 | } |
||
| 90 | else |
||
| 91 | { |
||
| 92 | $this->_set_initial_line($initial_line); |
||
| 93 | } |
||
| 94 | |||
| 95 | // Delimiter |
||
| 96 | if(! $delimiter) |
||
| 97 | { |
||
| 98 | $delimiter = $this->_get_delimiter(); |
||
| 99 | } |
||
| 100 | else |
||
| 101 | { |
||
| 102 | // If delimiter provided, set it |
||
| 103 | $this->_set_delimiter($delimiter); |
||
| 104 | } |
||
| 105 | |||
| 106 | // Column headers |
||
| 107 | if(! $column_headers) |
||
| 108 | { |
||
| 109 | $column_headers = $this->_get_column_headers(); |
||
| 110 | } |
||
| 111 | else |
||
| 112 | { |
||
| 113 | // If column headers provided, set them |
||
| 114 | $this->_set_column_headers($column_headers); |
||
| 115 | } |
||
| 116 | |||
| 117 | // Open the CSV for reading |
||
| 118 | $this->_get_handle(); |
||
| 119 | |||
| 120 | $row = 0; |
||
| 121 | |||
| 122 | while (($data = fgetcsv($this->handle, 0, $this->delimiter)) !== FALSE) |
||
| 123 | { |
||
| 124 | if ($data[0] != NULL) |
||
| 125 | { |
||
| 126 | if($row < $this->initial_line) |
||
| 127 | { |
||
| 128 | $row++; |
||
| 129 | continue; |
||
| 130 | } |
||
| 131 | |||
| 132 | // If first row, parse for column_headers |
||
| 133 | if($row == $this->initial_line) |
||
| 134 | { |
||
| 135 | // If column_headers already provided, use them |
||
| 136 | if($this->column_headers) |
||
| 137 | { |
||
| 138 | foreach ($this->column_headers as $key => $value) |
||
| 139 | { |
||
| 140 | $column_headers[$key] = trim($value); |
||
| 141 | } |
||
| 142 | } |
||
| 143 | else // Parse first row for column_headers to use |
||
| 144 | { |
||
| 145 | foreach ($data as $key => $value) |
||
| 146 | { |
||
| 147 | $column_headers[$key] = trim($value); |
||
| 148 | } |
||
| 149 | } |
||
| 150 | } |
||
| 151 | else |
||
| 152 | { |
||
| 153 | $new_row = $row - $this->initial_line - 1; // needed so that the returned array starts at 0 instead of 1 |
||
| 154 | foreach($column_headers as $key => $value) // assumes there are as many columns as their are title columns |
||
| 155 | { |
||
| 156 | $result[$new_row][$value] = utf8_encode(trim($data[$key])); |
||
| 157 | } |
||
| 158 | } |
||
| 159 | |||
| 160 | unset($data); |
||
| 161 | |||
| 162 | $row++; |
||
| 163 | } |
||
| 164 | } |
||
| 165 | |||
| 166 | $this->_close_csv(); |
||
| 167 | |||
| 168 | return $result; |
||
| 169 | } |
||
| 170 | |||
| 378 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: