Conditions | 9 |
Paths | 7 |
Total Lines | 62 |
Code Lines | 42 |
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 |
||
126 | public function importCsvAction(int $id, Request $request): Response |
||
127 | { |
||
128 | /** @var Game $game */ |
||
129 | $game = $this->admin->getSubject(); |
||
130 | $em = $this->admin->getModelManager()->getEntityManager($this->admin->getClass()); |
||
131 | |||
132 | $form = $this->createForm(ImportCsv::class); |
||
133 | $form->handleRequest($request); |
||
134 | if ($form->isSubmitted() && $form->isValid()) { |
||
135 | /** @var UploadedFile $csvFile */ |
||
136 | $csvFile = $form->get('csv')->getData(); |
||
137 | |||
138 | $rows = str_getcsv($csvFile->getContent(), "\n"); |
||
139 | |||
140 | /** @var Group $group */ |
||
141 | $group = null; |
||
142 | foreach ($rows as $index => $row) { |
||
143 | if ($index === 0) { |
||
144 | continue; |
||
145 | } |
||
146 | $data = str_getcsv($row, ";"); |
||
147 | if (count($data) !== 5) { |
||
148 | throw new \RuntimeException('Invalid number of rows.'); |
||
149 | } |
||
150 | |||
151 | if (!is_numeric($data[4])) { |
||
152 | throw new \RuntimeException('Column 5 must be numeric.'); |
||
153 | } |
||
154 | if ($group === null || $group->getLibGroupEn() !== $data[0]) { |
||
155 | $group = new Group(); |
||
156 | $group->setGame($game); |
||
157 | $group->setLibGroupEn($data[0]); |
||
158 | $group->setLibGroupFr($data[1]); |
||
159 | $em->persist($group); |
||
160 | } |
||
161 | |||
162 | $chart = new Chart(); |
||
163 | $chart->setGroup($group); |
||
164 | $chart->setLibChartEn($data[2]); |
||
165 | $chart->setLibChartFr($data[3]); |
||
166 | $em->persist($chart); |
||
167 | |||
168 | $chartLib = new ChartLib(); |
||
169 | $chartLib->setChart($chart); |
||
170 | $chartLib->setType($em->getReference(ChartType::class, $data[4])); |
||
171 | $em->persist($chartLib); |
||
172 | } |
||
173 | $em->flush(); |
||
174 | |||
175 | $this->addFlash('sonata_flash_success', 'CSV DATA successfully imported'); |
||
176 | return new RedirectResponse($this->admin->generateUrl('show', ['id' => $game->getId()])); |
||
177 | } |
||
178 | |||
179 | return $this->render( |
||
180 | '@VideoGamesRecordsCore/Admin/Form/form.default.html.twig', |
||
181 | [ |
||
182 | 'base_template' => '@SonataAdmin/standard_layout.html.twig', |
||
183 | 'admin' => $this->admin, |
||
184 | 'object' => $game, |
||
185 | 'form' => $form, |
||
186 | 'title' => $game->getName(), |
||
187 | 'action' => 'edit' |
||
188 | ] |
||
192 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.