| Conditions | 15 |
| Paths | 204 |
| Total Lines | 67 |
| 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 |
||
| 20 | protected function processAll($filepath, $preview = false) |
||
| 21 | { |
||
| 22 | $previousDetectLE = ini_get('auto_detect_line_endings'); |
||
| 23 | ini_set('auto_detect_line_endings', true); |
||
| 24 | |||
| 25 | $result = BulkLoader_Result::create(); |
||
| 26 | |||
| 27 | try { |
||
| 28 | $filepath = Director::getAbsFile($filepath); |
||
| 29 | $csvReader = CustomReader::createFromPath($filepath, 'r'); |
||
| 30 | |||
| 31 | $tabExtractor = function ($row, $rowOffset, $iterator) { |
||
| 32 | foreach ($row as &$item) { |
||
| 33 | // [SS-2017-007] Ensure all cells with leading tab and then [@=+] have the tab removed on import |
||
| 34 | if (preg_match("/^\t[\-@=\+]+.*/", $item)) { |
||
| 35 | $item = ltrim($item, "\t"); |
||
| 36 | } |
||
| 37 | } |
||
| 38 | return $row; |
||
| 39 | }; |
||
| 40 | |||
| 41 | if (isset($this->columnMap) && count($this->columnMap)) { |
||
| 42 | $headerMap = $this->getNormalisedColumnMap(); |
||
| 43 | $remapper = function ($row, $rowOffset, $iterator) use ($headerMap, $tabExtractor) { |
||
| 44 | $row = $tabExtractor($row, $rowOffset, $iterator); |
||
| 45 | foreach ($headerMap as $column => $renamedColumn) { |
||
| 46 | if ($column == $renamedColumn) { |
||
| 47 | continue; |
||
| 48 | } |
||
| 49 | if (array_key_exists($column, $row)) { |
||
| 50 | if (strpos($renamedColumn, '_ignore_') !== 0) { |
||
| 51 | $row[$renamedColumn] = $row[$column]; |
||
| 52 | } |
||
| 53 | unset($row[$column]); |
||
| 54 | } |
||
| 55 | } |
||
| 56 | return $row; |
||
| 57 | }; |
||
| 58 | } else { |
||
| 59 | $remapper = $tabExtractor; |
||
| 60 | } |
||
| 61 | |||
| 62 | $rows = null; |
||
| 63 | |||
| 64 | if ($this->hasHeaderRow) { |
||
| 65 | $rows = $csvReader->fetchAssoc(0, $remapper); |
||
| 66 | } elseif ($this->columnMap) { |
||
| 67 | $rows = $csvReader->fetchAssoc($headerMap, $remapper); |
||
| 68 | } |
||
| 69 | |||
| 70 | if (!empty($rows)) { |
||
| 71 | foreach ($rows as $row) { |
||
| 72 | $this->processRecord($row, $this->columnMap, $result, $preview); |
||
| 73 | } |
||
| 74 | } |
||
| 75 | } catch (\Exception $e) { |
||
| 76 | $failedMessage = sprintf("Failed to parse %s", $filepath); |
||
| 77 | if (Director::isDev()) { |
||
| 78 | $failedMessage = sprintf($failedMessage . " because %s", $e->getMessage()); |
||
| 79 | } |
||
| 80 | $result->addError($failedMessage); |
||
| 81 | } finally { |
||
| 82 | ini_set('auto_detect_line_endings', $previousDetectLE); |
||
| 83 | } |
||
| 84 | |||
| 85 | return $result; |
||
| 86 | } |
||
| 87 | |||
| 146 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.