Conditions | 14 |
Paths | 160 |
Total Lines | 67 |
Code Lines | 39 |
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 |
||
87 | public function generator(Request $request, ?LabelProfile $profile = null): Response |
||
88 | { |
||
89 | $this->denyAccessUnlessGranted('@labels.create_labels'); |
||
90 | |||
91 | //If we inherit a LabelProfile, the user need to have access to it... |
||
92 | if (null !== $profile) { |
||
93 | $this->denyAccessUnlessGranted('read', $profile); |
||
94 | } |
||
95 | |||
96 | if ($profile) { |
||
97 | $label_options = $profile->getOptions(); |
||
98 | } else { |
||
99 | $label_options = new LabelOptions(); |
||
100 | } |
||
101 | |||
102 | //We have to disable the options, if twig mode is selected and user is not allowed to use it. |
||
103 | $disable_options = 'twig' === $label_options->getLinesMode() && !$this->isGranted('@labels.use_twig'); |
||
104 | |||
105 | $form = $this->createForm(LabelDialogType::class, null, [ |
||
106 | 'disable_options' => $disable_options, |
||
107 | ]); |
||
108 | |||
109 | //Try to parse given target_type and target_id |
||
110 | $target_type = $request->query->get('target_type', null); |
||
111 | $target_id = $request->query->get('target_id', null); |
||
112 | $generate = $request->query->getBoolean('generate', false); |
||
113 | |||
114 | if (null === $profile && is_string($target_type)) { |
||
115 | $label_options->setSupportedElement($target_type); |
||
116 | } |
||
117 | if (is_string($target_id)) { |
||
118 | $form['target_id']->setData($target_id); |
||
119 | } |
||
120 | |||
121 | $form['options']->setData($label_options); |
||
122 | $form->handleRequest($request); |
||
123 | |||
124 | /** @var LabelOptions $form_options */ |
||
125 | $form_options = $form['options']->getData(); |
||
126 | |||
127 | $pdf_data = null; |
||
128 | $filename = 'invalid.pdf'; |
||
129 | |||
130 | //Generate PDF either when the form is submitted and valid, or the form was not submit yet, and generate is set |
||
131 | if (($form->isSubmitted() && $form->isValid()) || ($generate && !$form->isSubmitted() && null !== $profile)) { |
||
132 | $target_id = (string) $form->get('target_id')->getData(); |
||
133 | $targets = $this->findObjects($form_options->getSupportedElement(), $target_id); |
||
134 | if (!empty($targets)) { |
||
135 | try { |
||
136 | $pdf_data = $this->labelGenerator->generateLabel($form_options, $targets); |
||
137 | $filename = $this->getLabelName($targets[0], $profile); |
||
138 | } catch (TwigModeException $exception) { |
||
139 | $form->get('options')->get('lines')->addError(new FormError($exception->getMessage())); |
||
140 | } |
||
141 | } else { |
||
142 | //$this->addFlash('warning', 'label_generator.no_entities_found'); |
||
143 | $form->get('target_id')->addError( |
||
144 | new FormError($this->translator->trans('label_generator.no_entities_found')) |
||
145 | ); |
||
146 | } |
||
147 | } |
||
148 | |||
149 | return $this->renderForm('label_system/dialog.html.twig', [ |
||
150 | 'form' => $form, |
||
151 | 'pdf_data' => $pdf_data, |
||
152 | 'filename' => $filename, |
||
153 | 'profile' => $profile, |
||
154 | ]); |
||
179 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.