Conditions | 41 |
Paths | 244 |
Total Lines | 192 |
Code Lines | 125 |
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 |
||
260 | public function purifierconfig( |
||
261 | Request $request, |
||
262 | PurifierHelper $purifierHelper, |
||
263 | CacheClearer $cacheClearer, |
||
264 | string $reset = null |
||
265 | ) { |
||
266 | if (Request::METHOD_POST === $request->getMethod()) { |
||
267 | // Load HTMLPurifier Classes |
||
268 | $purifier = $purifierHelper->getPurifier(); |
||
269 | |||
270 | // Update module variables. |
||
271 | $config = $request->request->get('purifierConfig'); |
||
272 | $config = HTMLPurifier_Config::prepareArrayFromForm($config, false, true, true, $purifier->config->def); |
||
273 | |||
274 | $allowed = HTMLPurifier_Config::getAllowedDirectivesForForm(true, $purifier->config->def); |
||
275 | foreach ($allowed as [$namespace, $directive]) { |
||
276 | $directiveKey = $namespace . '.' . $directive; |
||
277 | $def = $purifier->config->def->info[$directiveKey]; |
||
278 | |||
279 | if (isset($config[$namespace]) |
||
280 | && array_key_exists($directive, $config[$namespace]) |
||
281 | && null === $config[$namespace][$directive]) { |
||
282 | unset($config[$namespace][$directive]); |
||
283 | |||
284 | if (count($config[$namespace]) <= 0) { |
||
285 | unset($config[$namespace]); |
||
286 | } |
||
287 | } |
||
288 | |||
289 | if (isset($config[$namespace][$directive])) { |
||
290 | if (is_int($def)) { |
||
291 | $directiveType = abs($def); |
||
292 | } else { |
||
293 | $directiveType = $def->type ?? 0; |
||
294 | } |
||
295 | |||
296 | switch ($directiveType) { |
||
297 | case HTMLPurifier_VarParser::LOOKUP: |
||
298 | $value = explode(PHP_EOL, $config[$namespace][$directive]); |
||
299 | $config[$namespace][$directive] = []; |
||
300 | foreach ($value as $val) { |
||
301 | $val = trim($val); |
||
302 | if (!empty($val)) { |
||
303 | $config[$namespace][$directive][$val] = true; |
||
304 | } |
||
305 | } |
||
306 | if (empty($config[$namespace][$directive])) { |
||
307 | unset($config[$namespace][$directive]); |
||
308 | } |
||
309 | break; |
||
310 | case HTMLPurifier_VarParser::ALIST: |
||
311 | $value = explode(PHP_EOL, $config[$namespace][$directive]); |
||
312 | $config[$namespace][$directive] = []; |
||
313 | foreach ($value as $val) { |
||
314 | $val = trim($val); |
||
315 | if (!empty($val)) { |
||
316 | $config[$namespace][$directive][] = $val; |
||
317 | } |
||
318 | } |
||
319 | if (empty($config[$namespace][$directive])) { |
||
320 | unset($config[$namespace][$directive]); |
||
321 | } |
||
322 | break; |
||
323 | case HTMLPurifier_VarParser::HASH: |
||
324 | $value = explode(PHP_EOL, $config[$namespace][$directive]); |
||
325 | $config[$namespace][$directive] = []; |
||
326 | foreach ($value as $val) { |
||
327 | [$i, $v] = explode(':', $val); |
||
328 | $i = trim($i); |
||
329 | $v = trim($v); |
||
330 | if (!empty($i) && !empty($v)) { |
||
331 | $config[$namespace][$directive][$i] = $v; |
||
332 | } |
||
333 | } |
||
334 | if (empty($config[$namespace][$directive])) { |
||
335 | unset($config[$namespace][$directive]); |
||
336 | } |
||
337 | break; |
||
338 | } |
||
339 | } |
||
340 | |||
341 | if (isset($config[$namespace]) |
||
342 | && array_key_exists($directive, $config[$namespace]) |
||
343 | && null === $config[$namespace][$directive]) { |
||
344 | unset($config[$namespace][$directive]); |
||
345 | |||
346 | if (count($config[$namespace]) <= 0) { |
||
347 | unset($config[$namespace]); |
||
348 | } |
||
349 | } |
||
350 | } |
||
351 | |||
352 | $this->setVar('htmlpurifierConfig', serialize($config)); |
||
353 | |||
354 | // clear all cache and compile directories |
||
355 | $cacheClearer->clear('symfony'); |
||
356 | $cacheClearer->clear('legacy'); |
||
357 | |||
358 | // the module configuration has been updated successfuly |
||
359 | $this->addFlash('status', 'Done! Saved HTMLPurifier configuration.'); |
||
360 | |||
361 | return $this->redirectToRoute('zikulasecuritycentermodule_config_purifierconfig'); |
||
362 | } |
||
363 | |||
364 | // load the configuration page |
||
365 | |||
366 | if (isset($reset) && 'default' === $reset) { |
||
367 | $purifierConfig = $purifierHelper->getPurifierConfig(['forcedefault' => true]); |
||
368 | $this->addFlash('status', 'Default values for HTML Purifier were successfully loaded. Please store them using the "Save" button at the bottom of this page'); |
||
369 | } else { |
||
370 | $purifierConfig = $purifierHelper->getPurifierConfig(['forcedefault' => false]); |
||
371 | } |
||
372 | |||
373 | $purifier = new HTMLPurifier($purifierConfig); |
||
374 | |||
375 | $config = $purifier->config; |
||
376 | |||
377 | if (is_array($config) && isset($config[0])) { |
||
378 | $config = $config[1]; |
||
379 | } |
||
380 | |||
381 | $allowed = HTMLPurifier_Config::getAllowedDirectivesForForm(true, $config->def); |
||
382 | |||
383 | // list of excluded directives, format is $namespace_$directive |
||
384 | $excluded = ['Cache_SerializerPath']; |
||
385 | |||
386 | // Editing for only these types is supported |
||
387 | $editableTypes = [ |
||
388 | HTMLPurifier_VarParser::C_STRING, |
||
389 | HTMLPurifier_VarParser::ISTRING, |
||
390 | HTMLPurifier_VarParser::TEXT, |
||
391 | HTMLPurifier_VarParser::ITEXT, |
||
392 | HTMLPurifier_VarParser::C_INT, |
||
393 | HTMLPurifier_VarParser::C_FLOAT, |
||
394 | HTMLPurifier_VarParser::C_BOOL, |
||
395 | HTMLPurifier_VarParser::LOOKUP, |
||
396 | HTMLPurifier_VarParser::ALIST, |
||
397 | HTMLPurifier_VarParser::HASH |
||
398 | ]; |
||
399 | |||
400 | $purifierAllowed = []; |
||
401 | foreach ($allowed as [$namespace, $directive]) { |
||
402 | if (in_array($namespace . '_' . $directive, $excluded, true)) { |
||
403 | continue; |
||
404 | } |
||
405 | |||
406 | $directiveRec = []; |
||
407 | $directiveRec['key'] = $namespace . '.' . $directive; |
||
408 | $def = $config->def->info[$directiveRec['key']]; |
||
409 | $directiveRec['value'] = $config->get($directiveRec['key']); |
||
410 | if (is_int($def)) { |
||
411 | $directiveRec['allowNull'] = ($def < 0); |
||
412 | $directiveRec['type'] = abs($def); |
||
413 | } else { |
||
414 | $directiveRec['allowNull'] = (isset($def->allow_null) && $def->allow_null); |
||
415 | $directiveRec['type'] = ($def->type ?? 0); |
||
416 | if (isset($def->allowed)) { |
||
417 | $directiveRec['allowedValues'] = []; |
||
418 | foreach ($def->allowed as $val => $b) { |
||
419 | $directiveRec['allowedValues'][] = $val; |
||
420 | } |
||
421 | } |
||
422 | } |
||
423 | if (is_array($directiveRec['value'])) { |
||
424 | switch ($directiveRec['type']) { |
||
425 | case HTMLPurifier_VarParser::LOOKUP: |
||
426 | $value = []; |
||
427 | foreach ($directiveRec['value'] as $val => $b) { |
||
428 | $value[] = $val; |
||
429 | } |
||
430 | $directiveRec['value'] = implode(PHP_EOL, $value); |
||
431 | break; |
||
432 | case HTMLPurifier_VarParser::ALIST: |
||
433 | $directiveRec['value'] = implode(PHP_EOL, $directiveRec['value']); |
||
434 | break; |
||
435 | case HTMLPurifier_VarParser::HASH: |
||
436 | $directiveRec['value'] = json_encode($directiveRec['value']); |
||
437 | break; |
||
438 | default: |
||
439 | $directiveRec['value'] = ''; |
||
440 | } |
||
441 | } |
||
442 | |||
443 | $directiveRec['supported'] = in_array($directiveRec['type'], $editableTypes, true); |
||
444 | |||
445 | $purifierAllowed[$namespace][$directive] = $directiveRec; |
||
446 | } |
||
447 | |||
448 | return [ |
||
449 | 'purifier' => $purifier, |
||
450 | 'purifierTypes' => HTMLPurifier_VarParser::$types, |
||
451 | 'purifierAllowed' => $purifierAllowed |
||
452 | ]; |
||
507 |