Conditions | 14 |
Paths | 15 |
Total Lines | 66 |
Code Lines | 47 |
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 |
||
25 | public function addFromCsv() |
||
26 | { |
||
27 | $files = array_diff(scandir($this->directory . '/in', SCANDIR_SORT_NONE), ['..', '.']); |
||
28 | foreach ($files as $file) { |
||
29 | if (0 !== strpos($file, 'game-add')) { |
||
30 | continue; |
||
31 | } |
||
32 | |||
33 | $fileIn = $this->directory . '/in/' . $file; |
||
34 | $fileOut = $this->directory . '/out/' . $file; |
||
35 | $handle = fopen($fileIn, 'rb'); |
||
36 | $idGame = substr($file, 8, -4); |
||
37 | |||
38 | if (!is_numeric($idGame)) { |
||
39 | continue; |
||
40 | } |
||
41 | |||
42 | /** @var \VideoGamesRecords\CoreBundle\Entity\Game $game */ |
||
43 | $game = $this->em->getReference(GameEntity::class, $idGame); |
||
44 | |||
45 | if ($game === null) { |
||
46 | continue; |
||
47 | } |
||
48 | |||
49 | $group = null; |
||
50 | $types = null; |
||
51 | while (($row = fgetcsv($handle, null, ';')) !== false) { |
||
|
|||
52 | list($type, $libEn, $libFr) = $row; |
||
53 | if (isset($row[3]) && null !== $row[3] && in_array($type, ['group', 'chart'])) { |
||
54 | $types = explode('|', $row[3]); |
||
55 | } |
||
56 | switch ($row[0]) { |
||
57 | case 'object': |
||
58 | // DO nohting |
||
59 | break; |
||
60 | case 'group': |
||
61 | $group = new Group(); |
||
62 | $group->translate('en', false)->setName($libEn); |
||
63 | $group->translate('fr', false)->setName($libFr); |
||
64 | $group->setGame($game); |
||
65 | $group->mergeNewTranslations(); |
||
66 | $this->em->persist($group); |
||
67 | break; |
||
68 | case 'chart': |
||
69 | $chart = new Chart(); |
||
70 | $chart->translate('en', false)->setName($libEn); |
||
71 | $chart->translate('fr', false)->setName($libFr); |
||
72 | $chart->setGroup($group); |
||
73 | $chart->mergeNewTranslations(); |
||
74 | |||
75 | if ($types !== null) { |
||
76 | foreach ($types as $idType) { |
||
77 | $chartLib = new ChartLib(); |
||
78 | $chartLib |
||
79 | ->setChart($chart) |
||
80 | ->setType($this->em->getReference(ChartType::class, $idType)); |
||
81 | $chart->addLib($chartLib); |
||
82 | } |
||
83 | } |
||
84 | |||
85 | $this->em->persist($chart); |
||
86 | break; |
||
87 | } |
||
88 | } |
||
89 | $this->em->flush(); |
||
90 | rename($fileIn, $fileOut); |
||
91 | } |
||
143 |