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