Conditions | 29 |
Paths | 2642 |
Total Lines | 153 |
Code Lines | 91 |
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 |
||
338 | protected function matchTemplate(NodeList $nodes, FormInterface $form) |
||
339 | { |
||
340 | // todo injection |
||
341 | $translator = $this->formRow->getTranslator(); |
||
342 | |||
343 | $matched = []; |
||
344 | $toRemove = []; |
||
345 | $elements = $form->getElements(); |
||
346 | |||
347 | /* @var $node \WebinoDraw\Dom\Element */ |
||
348 | foreach ($nodes as $node) { |
||
349 | /** @var \WebinoDraw\Dom\Document $ownerDocument */ |
||
350 | $ownerDocument = $node->ownerDocument; |
||
351 | |||
352 | $nodePath = $node->getNodePath(); |
||
353 | $elementNodes = $ownerDocument->getXpath()->query('.//*[@name]', $node); |
||
354 | /* @var $elementNode \WebinoDraw\Dom\Element */ |
||
355 | foreach ($elementNodes as $elementNode) { |
||
356 | |||
357 | $elementName = $elementNode->getAttribute('name'); |
||
358 | $matched[$elementName] = true; |
||
359 | |||
360 | if (!$form->has($elementName)) { |
||
361 | // remove node of the missing form element |
||
362 | $parentNode = $elementNode->parentNode; |
||
363 | if ('label' === $parentNode->nodeName) { |
||
364 | $parentNode->parentNode->removeChild($parentNode); |
||
365 | } else { |
||
366 | $toRemove[$nodePath][] = $elementName; |
||
367 | $elementNode->parentNode->removeChild($elementNode); |
||
368 | } |
||
369 | continue; |
||
370 | } |
||
371 | |||
372 | $elementNode->setAttribute('id', $elementName); |
||
373 | |||
374 | /* @var $element \Zend\Form\Element */ |
||
375 | $element = $form->get($elementName); |
||
376 | $attributes = $element->getAttributes(); |
||
377 | |||
378 | // TODO remaining elements |
||
379 | if (isset($attributes['type'])) { |
||
380 | switch ($attributes['type']) { |
||
381 | case 'checkbox': |
||
382 | /* @var $element \Zend\Form\Element\Checkbox */ |
||
383 | $attributes['value'] = $element->getCheckedValue(); |
||
384 | // todo checkbox use hidden element |
||
385 | break; |
||
386 | case 'multi_checkbox': |
||
387 | $multiNode = $ownerDocument->createDocumentFragment(); |
||
388 | $multiNode->appendXml($this->formRow->__invoke($element)); |
||
389 | $elementNode = $elementNode->parentNode->replaceChild($multiNode, $elementNode); |
||
390 | unset($multiNode); |
||
391 | break; |
||
392 | case 'select': |
||
393 | $selectNode = $ownerDocument->createDocumentFragment(); |
||
394 | $selectNode->appendXml($this->formElement->__invoke($element)); |
||
395 | $newNode = $selectNode->firstChild; |
||
396 | $elementNode->parentNode->replaceChild($newNode, $elementNode); |
||
397 | $elementNode = $newNode; |
||
398 | unset($selectNode); |
||
399 | unset($newNode); |
||
400 | break; |
||
401 | |||
402 | case 'text': |
||
403 | case 'email': |
||
404 | case 'submit': |
||
405 | case 'reset': |
||
406 | case 'button': |
||
407 | |||
408 | // set element node value if available |
||
409 | $value = $elementNode->hasAttribute('value') |
||
410 | ? $elementNode->getAttribute('value') |
||
411 | : $element->getValue(); |
||
412 | |||
413 | $attributes['value'] = !empty($value) ? $translator->translate($value) : ''; |
||
414 | unset($value); |
||
415 | break; |
||
416 | } |
||
417 | } |
||
418 | |||
419 | // glue form & template element class |
||
420 | empty($attributes['class']) |
||
421 | or $attributes['class'] = trim($attributes['class'] . ' ' . $elementNode->getAttribute('class')); |
||
422 | |||
423 | $subElementNodes = $nodes->create([$elementNode]); |
||
424 | $subElementNodes->setAttribs($attributes); |
||
425 | |||
426 | // labels |
||
427 | $subElementNodes->each( |
||
428 | 'xpath=../span[name(..)="label"]|..//label[@for="' . $elementName . '"]', |
||
429 | function ($nodes) use ($element, $translator) { |
||
430 | $label = $translator->translate($element->getLabel()); |
||
431 | |||
432 | /* @var $subNode \WebinoDraw\Dom\Element */ |
||
433 | foreach ($nodes as $subNode) { |
||
434 | $subNode->nodeValue = !$subNode->isEmpty() |
||
435 | ? $translator->translate($subNode->nodeValue) |
||
436 | : $label; |
||
437 | } |
||
438 | } |
||
439 | ); |
||
440 | |||
441 | // errors |
||
442 | $messages = $element->getMessages(); |
||
443 | if (!empty($messages)) { |
||
444 | |||
445 | $errorNode = $ownerDocument->createDocumentFragment(); |
||
446 | $errorNode->appendXml($this->formElementErrors->__invoke($element)); |
||
447 | |||
448 | foreach ($subElementNodes as $subElementNode) { |
||
449 | if (empty($subElementNode->nextSibling)) { |
||
450 | $subElementNode->parentNode->appendChild($errorNode); |
||
451 | } else { |
||
452 | $subElementNode->parentNode->insertBefore($errorNode, $subElementNode); |
||
453 | } |
||
454 | } |
||
455 | } |
||
456 | } |
||
457 | |||
458 | // auto draw hidden nodes |
||
459 | /** @var \Zend\Form\Element $element */ |
||
460 | foreach ($elements as $element) { |
||
461 | $attributes = $element->getAttributes(); |
||
462 | if (empty($attributes['type']) |
||
463 | || 'hidden' !== $attributes['type'] |
||
464 | ) { |
||
465 | continue; |
||
466 | } |
||
467 | |||
468 | // skip matched elements |
||
469 | if (isset($matched[$attributes['name']])) { |
||
470 | continue; |
||
471 | } |
||
472 | |||
473 | $hiddenNode = $ownerDocument->createDocumentFragment(); |
||
474 | $xhtml = (string) $this->formRow->__invoke($element); |
||
475 | |||
476 | $hiddenNode->appendXml($xhtml); |
||
477 | if (!$hiddenNode->hasChildNodes()) { |
||
478 | throw new Exception\RuntimeException('Invalid XHTML ' . $xhtml); |
||
479 | } |
||
480 | $node->appendChild($hiddenNode); |
||
481 | } |
||
482 | } |
||
483 | |||
484 | // remove labels of missing elements |
||
485 | foreach ($toRemove as $nodePath => $elementNames) { |
||
486 | foreach ($elementNames as $elementName) { |
||
487 | $nodes->remove('xpath=' . $nodePath . '//label[@for="' . $elementName . '"]'); |
||
488 | } |
||
489 | } |
||
490 | } |
||
491 | } |
||
492 |
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.json
file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.json
to be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
require
orrequire-dev
section?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceof
checks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.