| Conditions | 8 |
| Paths | 13 |
| Total Lines | 52 |
| Code Lines | 36 |
| 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 |
||
| 120 | protected function readFile() |
||
| 121 | { |
||
| 122 | $this->outputToScreen(" |
||
| 123 | ================================================ |
||
| 124 | READING FILE ".ini_get('max_execution_time')." seconds available. ".(ini_get('memory_limit'))."MB available |
||
| 125 | ================================================"); |
||
| 126 | if (!$this->fileLocation) { |
||
| 127 | $this->outputToScreen("There is no file to import", "deleted"); |
||
| 128 | return; |
||
| 129 | } |
||
| 130 | $rowCount = 1; |
||
| 131 | $rows = array(); |
||
| 132 | |||
| 133 | if (strpos($this->fileLocation, Director::baseFolder()) === false) { |
||
| 134 | $fileLocation = Director::baseFolder()."/".$this->fileLocation; |
||
| 135 | } else { |
||
| 136 | $fileLocation = $this->fileLocation; |
||
| 137 | } |
||
| 138 | $this->outputToScreen("reading file <strong>$fileLocation </strong>", "altered"); |
||
| 139 | $replaceFromChars = array_keys($this->Config()->get('characters_to_replace')); |
||
| 140 | $replaceToChars = array_values($this->Config()->get('characters_to_replace')); |
||
| 141 | |||
| 142 | if (($handle = fopen($fileLocation, "r")) !== false) { |
||
| 143 | while (($data = fgetcsv($handle, 100000, $this->csvSeparator)) !== false) { |
||
| 144 | $cleanArray = array(); |
||
| 145 | foreach ($data as $key => $value) { |
||
| 146 | $value = str_replace($replaceFromChars, $replaceToChars, $value); |
||
| 147 | $cleanArray[trim($key)] = trim($value); |
||
| 148 | } |
||
| 149 | $rows[] = $cleanArray; |
||
| 150 | $rowCount++; |
||
| 151 | } |
||
| 152 | fclose($handle); |
||
| 153 | } |
||
| 154 | //$rows = str_getcsv(file_get_contents(, ",", '"'); |
||
| 155 | |||
| 156 | $header = array_shift($rows); |
||
| 157 | |||
| 158 | $this->csv = array(); |
||
| 159 | $rowCount = 1; |
||
| 160 | foreach ($rows as $row) { |
||
| 161 | if (count($header) != count($row)) { |
||
| 162 | $this->outputToScreen("I am trying to merge ".implode(", ", $header)." with ".implode(", ", $row)." but the column count does not match!", "deleted"); |
||
| 163 | die("STOPPED"); |
||
| 164 | } |
||
| 165 | $this->csv[] = array_combine($header, $row); |
||
| 166 | $rowCount++; |
||
| 167 | } |
||
| 168 | $this->outputToScreen("Imported ".count($this->csv)." rows with ".count($header)." cells each"); |
||
| 169 | $this->outputToScreen("Fields are: ".implode(", ", $header)); |
||
| 170 | $this->outputToScreen("================================================"); |
||
| 171 | } |
||
| 172 | |||
| 237 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.