| Conditions | 6 |
| Paths | 9 |
| Total Lines | 51 |
| Code Lines | 34 |
| 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 |
||
| 53 | public function importFromFile($fileName, $userId) |
||
| 54 | { |
||
| 55 | $content = file_get_contents($fileName); |
||
| 56 | $content = mb_convert_encoding($content, 'UTF-8', 'UCS-2LE'); |
||
| 57 | $rows = ArrayUtil::trimExplode("\n", $content); |
||
| 58 | foreach ($rows as $row) { |
||
| 59 | $data = str_getcsv($row, ',', '"', '""'); |
||
| 60 | if (count($data) !== 4) { |
||
| 61 | throw new WrongFileFormatException( |
||
| 62 | $this->translator->trans('This file seems not to be a field notes file.') |
||
| 63 | ); |
||
| 64 | } |
||
| 65 | |||
| 66 | if (!array_key_exists($data[2], self::LOG_TYPE)) { |
||
| 67 | $this->addError( |
||
| 68 | $this->translator->trans('Log type "%type%" is not implemented', ['%type%' => $data[2]]) |
||
| 69 | ); |
||
| 70 | continue; |
||
| 71 | } |
||
| 72 | $type = self::LOG_TYPE[$data[2]]; |
||
| 73 | |||
| 74 | $geocache = $this->entityManager->getRepository('AppBundle:Geocache')->findOneBy(['wpOc' => $data[0]]); |
||
| 75 | if (!$geocache) { |
||
| 76 | $this->addError( |
||
| 77 | $this->translator->trans('Geocache "%code%" not found', ['%code%' => $data[0]]) |
||
| 78 | ); |
||
| 79 | continue; |
||
| 80 | } |
||
| 81 | |||
| 82 | $date = DateTime::createFromFormat( |
||
| 83 | self::FIELD_NOTE_DATETIME_FORMAT, |
||
| 84 | $data[1], |
||
| 85 | new DateTimeZone('UTC') |
||
| 86 | ); |
||
| 87 | |||
| 88 | $fieldNote = new FieldNote(); |
||
| 89 | $fieldNote->setUser($this->entityManager->getReference('AppBundle:User', $userId)); |
||
| 90 | $fieldNote->setGeocache($geocache); |
||
| 91 | $fieldNote->setDate($date); |
||
| 92 | $fieldNote->setType($type); |
||
| 93 | $fieldNote->setText($data[3]); |
||
| 94 | $this->entityManager->persist($fieldNote); |
||
| 95 | } |
||
| 96 | $this->entityManager->flush(); |
||
| 97 | |||
| 98 | if ($this->hasErrors()) { |
||
| 99 | return false; |
||
| 100 | } |
||
| 101 | |||
| 102 | return true; |
||
| 103 | } |
||
| 104 | } |
||
| 105 |