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