Completed
Push — develop ( c74260...f7afa8 )
by
unknown
07:39
created

FormElement   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 2
dl 0
loc 35
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
C render() 0 26 7
1
<?php
2
/**
3
4
 */
5
6
namespace Core\Form\View\Helper;
7
8
use Core\Form\ViewPartialProviderInterface;
9
use Zend\Form\View\Helper\FormElement as ZendFormElement;
10
use Zend\Form\Element;
11
use Zend\Form\ElementInterface;
12
use Core\Form\Element\ViewHelperProviderInterface as CoreElementInterface;
13
use Zend\View\Helper\HelperInterface;
14
15
class FormElement extends ZendFormElement
16
{
17
18
    /**
19
     * @param ElementInterface $element
20
     *
21
     * @return string
22
     */
23
    public function render(ElementInterface $element, $ignoreViewPartial = false)
24
    {
25
        $renderer = $this->getView();
26
        if (!method_exists($renderer, 'plugin')) {
27
            // Bail early if renderer is not pluggable
28
            return '';
29
        }
30
31
        if ($element instanceof ViewPartialProviderInterface && !$ignoreViewPartial) {
32
            $partial = $element->getViewPartial();
33
            return $renderer->partial($partial, ['element' => $element]);
34
        }
35
36
        if ($element instanceof CoreElementInterface) {
37
            $helper = $element->getViewHelper();
38
            if (is_string($helper)) {
39
                $helper = $renderer->plugin($helper);
40
            }
41
            if ($helper instanceof HelperInterface) {
42
                $helper->setView($renderer);
0 ignored issues
show
Bug introduced by
It seems like $renderer defined by $this->getView() on line 25 can be null; however, Zend\View\Helper\HelperInterface::setView() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
43
            }
44
            return $helper($element);
45
        }
46
47
        return parent::render($element);
48
    }
49
}
50