Conditions | 9 |
Paths | 40 |
Total Lines | 69 |
Code Lines | 45 |
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 |
||
128 | public function addPart(Request $request, EntityManagerInterface $entityManager, ?Project $project): Response |
||
129 | { |
||
130 | if($project) { |
||
131 | $this->denyAccessUnlessGranted('edit', $project); |
||
132 | } else { |
||
133 | $this->denyAccessUnlessGranted('@projects.edit'); |
||
134 | } |
||
135 | |||
136 | $builder = $this->createFormBuilder(); |
||
137 | $builder->add('project', StructuralEntityType::class, [ |
||
138 | 'class' => Project::class, |
||
139 | 'required' => true, |
||
140 | 'disabled' => $project !== null, //If a project is given, disable the field |
||
141 | 'data' => $project, |
||
142 | 'constraints' => [ |
||
143 | new NotNull() |
||
144 | ] |
||
145 | ]); |
||
146 | $builder->add('bom_entries', ProjectBOMEntryCollectionType::class); |
||
147 | $builder->add('submit', SubmitType::class, ['label' => 'save']); |
||
148 | $form = $builder->getForm(); |
||
149 | |||
150 | //Preset the BOM entries with the selected parts, when the form was not submitted yet |
||
151 | $preset_data = new ArrayCollection(); |
||
152 | foreach (explode(',', $request->get('parts', '')) as $part_id) { |
||
153 | $part = $entityManager->getRepository(Part::class)->find($part_id); |
||
154 | if (null !== $part) { |
||
155 | //If there is already a BOM entry for this part, we use this one (we edit it then) |
||
156 | $bom_entry = $entityManager->getRepository(ProjectBOMEntry::class)->findOneBy([ |
||
157 | 'project' => $project, |
||
158 | 'part' => $part |
||
159 | ]); |
||
160 | if ($bom_entry) { |
||
161 | $preset_data->add($bom_entry); |
||
162 | } else { //Otherwise create an empty one |
||
163 | $entry = new ProjectBOMEntry(); |
||
164 | $entry->setProject($project); |
||
165 | $entry->setPart($part); |
||
166 | $preset_data->add($entry); |
||
167 | } |
||
168 | } |
||
169 | } |
||
170 | $form['bom_entries']->setData($preset_data); |
||
171 | |||
172 | $form->handleRequest($request); |
||
173 | if ($form->isSubmitted() && $form->isValid()) { |
||
174 | $target_project = $project ?? $form->get('project')->getData(); |
||
175 | |||
176 | //Ensure that we really have acces to the selected project |
||
177 | $this->denyAccessUnlessGranted('edit', $target_project); |
||
178 | |||
179 | $data = $form->getData(); |
||
180 | $bom_entries = $data['bom_entries']; |
||
181 | foreach ($bom_entries as $bom_entry){ |
||
182 | $target_project->addBOMEntry($bom_entry); |
||
183 | } |
||
184 | $entityManager->flush(); |
||
185 | |||
186 | //If a redirect query parameter is set, redirect to this page |
||
187 | if ($request->query->get('_redirect')) { |
||
188 | return $this->redirect($request->query->get('_redirect')); |
||
189 | } |
||
190 | //Otherwise just show the project info page |
||
191 | return $this->redirectToRoute('project_info', ['id' => $target_project->getID()]); |
||
192 | } |
||
193 | |||
194 | return $this->renderForm('Projects/add_parts.html.twig', [ |
||
195 | 'project' => $project, |
||
196 | 'form' => $form, |
||
197 | ]); |
||
199 | } |