Conditions | 25 |
Paths | 900 |
Total Lines | 212 |
Code Lines | 125 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 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 |
||
76 | public function settingsAction(Request $request) |
||
77 | { |
||
78 | $this->denyAccessUnlessGranted('ROLE_ADMIN'); |
||
79 | |||
80 | $requiredResetText = 'THIS DELETES ALL TICKETS'; |
||
81 | |||
82 | $settingKeys = [ |
||
83 | 'freeze' => false, |
||
84 | 'freezeMessage' => '', |
||
85 | 'remotesUrl' => '/', |
||
86 | 'upcomingCount' => 3, |
||
87 | 'songInPreview' => false |
||
88 | ]; |
||
89 | |||
90 | $formDefaults = $settingKeys; |
||
91 | |||
92 | $dataStore = $this->getDataStore(); |
||
93 | |||
94 | foreach ($settingKeys as $key => $default) { |
||
95 | $value = $dataStore->fetchSetting($key); |
||
96 | if (is_null($value)) { |
||
97 | $value = $default; |
||
98 | } else { |
||
99 | switch (gettype($default)) { |
||
100 | case 'integer': |
||
101 | $value = (int)$value; |
||
102 | break; |
||
103 | case 'boolean': |
||
104 | $value = (bool)$value; |
||
105 | break; |
||
106 | } |
||
107 | } |
||
108 | $formDefaults[$key] = $value; // fixme handle type better |
||
109 | } |
||
110 | |||
111 | $formFactory = Forms::createFormFactoryBuilder() |
||
112 | ->addExtension(new HttpFoundationExtension()) |
||
113 | ->getFormFactory(); |
||
114 | |||
115 | $settingsSubmit = 'Save Settings'; |
||
116 | $settingsForm = $formFactory->createNamedBuilder('settingsForm', FormType::class, $formDefaults) |
||
117 | ->add('freeze', CheckboxType::class, ['label' => 'Display "Queue Frozen" message']) |
||
118 | ->add('freezeMessage', TextType::class, ['label' => 'Customise "Queue Frozen" message']) |
||
119 | ->add('remotesUrl', TextType::class, ['label' => 'URL to display on remote screens']) |
||
120 | ->add('upcomingCount', NumberType::class, ['label' => 'Upcoming songs to display']) |
||
121 | ->add('songInPreview', CheckboxType::class, ['label' => 'Display song titles on public queue']) |
||
122 | ->add($settingsSubmit, SubmitType::class) |
||
123 | ->getForm(); |
||
124 | |||
125 | $settingsFormSaved = false; |
||
126 | |||
127 | if ($request->request->has('settingsForm')) { |
||
128 | $settingsForm->handleRequest($request); |
||
129 | |||
130 | /** |
||
131 | * @noinspection PhpUndefinedMethodInspection |
||
132 | */ // isClicked on Submit |
||
133 | if ($settingsForm->isSubmitted() |
||
134 | && $settingsForm->isValid() |
||
135 | && $settingsForm->get($settingsSubmit)->isClicked() |
||
136 | ) { |
||
137 | $data = $settingsForm->getData(); |
||
138 | foreach ($data as $key => $default) { |
||
139 | $dataStore->updateSetting($key, $default); |
||
140 | } |
||
141 | $settingsFormSaved = true; |
||
142 | } |
||
143 | } |
||
144 | |||
145 | // ---------------- |
||
146 | |||
147 | $resetSubmit = 'Reset all'; |
||
148 | $resetForm = $formFactory->createNamedBuilder('resetForm', FormType::class) |
||
149 | ->add('resetMessage', TextType::class) |
||
150 | ->add($resetSubmit, SubmitType::class) |
||
151 | ->getForm(); |
||
152 | |||
153 | |||
154 | $resetFormSaved = false; |
||
155 | |||
156 | if ($request->request->has('resetForm')) { |
||
157 | $resetForm->handleRequest($request); |
||
158 | |||
159 | /** |
||
160 | * @noinspection PhpUndefinedMethodInspection |
||
161 | */ // isClicked on Submit |
||
162 | if ($resetForm->isSubmitted() |
||
163 | && $resetForm->isValid() |
||
164 | && $resetForm->get($resetSubmit)->isClicked() |
||
165 | ) { |
||
166 | $data = $resetForm->getData(); |
||
167 | // var_dump($data); |
||
|
|||
168 | // die(); |
||
169 | if (trim($data['resetMessage']) === $requiredResetText) { |
||
170 | $dataStore->resetAllSessionData(); |
||
171 | $resetFormSaved = true; |
||
172 | } |
||
173 | } |
||
174 | } |
||
175 | |||
176 | // ------------------- |
||
177 | |||
178 | $songListSubmit = 'Upload song list'; |
||
179 | $songListForm = $formFactory->createNamedBuilder('songListForm', FormType::class) |
||
180 | ->add('songListFile', FileType::class) |
||
181 | ->add($songListSubmit, SubmitType::class) |
||
182 | ->getForm(); |
||
183 | |||
184 | $songFormSaved = false; |
||
185 | $songsLoaded = false; |
||
186 | |||
187 | if ($request->request->has('songListForm')) { |
||
188 | $songListForm->handleRequest($request); |
||
189 | |||
190 | /** |
||
191 | * @noinspection PhpUndefinedMethodInspection |
||
192 | */ // isClicked on Submit |
||
193 | if ($songListForm->isSubmitted() |
||
194 | && $songListForm->isValid() |
||
195 | && $songListForm->get($songListSubmit)->isClicked() |
||
196 | ) { |
||
197 | $data = $songListForm->getData(); |
||
198 | |||
199 | $file = $data['songListFile']; |
||
200 | /** |
||
201 | * @var UploadedFile $file |
||
202 | */ |
||
203 | |||
204 | $loader = new SongLoader(); |
||
205 | |||
206 | $songsLoaded = $loader->run($file->getPathname(), $this->get('database_connection')); |
||
207 | |||
208 | $songFormSaved = true; |
||
209 | } |
||
210 | } |
||
211 | |||
212 | // ------------------- |
||
213 | |||
214 | $defaults = ['customCss' => $dataStore->fetchSetting('customCss')]; |
||
215 | |||
216 | $stylingSubmit = 'Update styles'; |
||
217 | $stylingForm = $formFactory->createNamedBuilder('stylingForm', FormType::class, $defaults) |
||
218 | ->add('backgroundImageFile', FileType::class, ['label' => 'New background image']) |
||
219 | ->add('customCss', TextareaType::class, ['attr' => ['rows' => 8, 'cols' => 60, 'label' => 'Custom CSS']]) |
||
220 | ->add($stylingSubmit, SubmitType::class) |
||
221 | ->getForm(); |
||
222 | |||
223 | $styleFormSaved = false; |
||
224 | $backgroundUpdated = false; |
||
225 | |||
226 | if ($request->request->has('stylingForm')) { |
||
227 | $stylingForm->handleRequest($request); |
||
228 | |||
229 | /** |
||
230 | * @noinspection PhpUndefinedMethodInspection |
||
231 | */ // isClicked on Submit |
||
232 | if ($stylingForm->isSubmitted() |
||
233 | && $stylingForm->isValid() |
||
234 | && $stylingForm->get($stylingSubmit)->isClicked() |
||
235 | ) { |
||
236 | $data = $stylingForm->getData(); |
||
237 | |||
238 | $file = $data['backgroundImageFile']; |
||
239 | if ($file) { |
||
240 | $mimeType = $file->getMimeType(); |
||
241 | $pathName = $file->getPathName(); |
||
242 | /** |
||
243 | * @var UploadedFile $file |
||
244 | */ |
||
245 | $suffixByMimeType = [ |
||
246 | 'image/jpeg' => 'jpg', |
||
247 | 'image/gif' => 'gif', |
||
248 | 'image/png' => 'png', |
||
249 | ]; |
||
250 | |||
251 | if (array_key_exists($mimeType, $suffixByMimeType)) { |
||
252 | $suffix = $suffixByMimeType[$mimeType]; |
||
253 | $targetFile = 'background.' . $suffix; |
||
254 | $destination = dirname($this->get('kernel')->getRootDir()) . '/web/uploads/' . $targetFile; |
||
255 | move_uploaded_file($pathName, $destination); |
||
256 | $dataStore->updateSetting('backgroundFilename', $targetFile); |
||
257 | } else { |
||
258 | throw new \UnexpectedValueException("Invalid mimetype '$mimeType'"); |
||
259 | } |
||
260 | $backgroundUpdated = true; |
||
261 | } |
||
262 | |||
263 | $dataStore->updateSetting('customCss', $data['customCss']); |
||
264 | |||
265 | $styleFormSaved = true; |
||
266 | } |
||
267 | } |
||
268 | |||
269 | // ------------------- |
||
270 | |||
271 | return $this->render( |
||
272 | ':admin:settings.html.twig', |
||
273 | [ |
||
274 | 'settingsFormSaved' => $settingsFormSaved, |
||
275 | 'settingsForm' => $settingsForm->createView(), |
||
276 | 'resetForm' => $resetForm->createView(), |
||
277 | 'resetFormSaved' => $resetFormSaved, |
||
278 | 'resetRequiredText' => $requiredResetText, |
||
279 | 'songListForm' => $songListForm->createView(), |
||
280 | 'songFormSaved' => $songFormSaved, |
||
281 | 'songsLoaded' => $songsLoaded, |
||
282 | 'stylingForm' => $stylingForm->createView(), |
||
283 | 'styleFormSaved' => $styleFormSaved, |
||
284 | 'backgroundUpdated' => $backgroundUpdated, |
||
285 | ] |
||
286 | ); |
||
287 | } |
||
288 | } |
||
289 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.