Passed
Push — develop ( 2b301a...ddf000 )
by Kevin
10:40 queued 01:33
created

MagiumRenderer::__invoke()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 28
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 28
c 0
b 0
f 0
ccs 0
cts 21
cp 0
rs 8.5806
cc 4
eloc 21
nc 6
nop 4
crap 20
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: kschr
5
 * Date: 3/20/2017
6
 * Time: 4:54 PM
7
 */
8
9
namespace Magium\Configuration\View;
10
11
use Zend\Form\Element;
12
use Zend\Form\View\Helper\FormElement;
13
use Zend\Form\View\Helper\FormInput;
14
use Zend\View\Helper\AbstractHelper;
15
16
class MagiumRenderer extends AbstractHelper
17
{
18
    function __invoke($section, $group, $id, $options)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
19
    {
20
        $type = 'text';
21
        $options = ['value' => $options['value']];
22
        if (isset($options['type'])) {
23
            $type = $options['type'];
24
        }
25
        $viewClass = 'Zend\Form\View\Helper\Form' . ucfirst(strtolower($type));
26
        $formClass = 'Zend\Form\Element\\' . ucfirst(strtolower($type));
27
        $reflectionClass = new \ReflectionClass($viewClass);
28
        if (!$reflectionClass->isSubclassOf(AbstractHelper::class)) {
29
            throw new InvalidViewConfigurationException('Invalid setting input type');
30
        }
31
32
        $instance = $reflectionClass->newInstance();
33
        if ($instance instanceof FormInput) {
34
            $instance->setView($this->getView());
0 ignored issues
show
Bug introduced by
It seems like $this->getView() can be null; however, 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...
35
            $formInstance = new \ReflectionClass($formClass);
36
            $name = sprintf('%s_%s_%s', $section, $group, $id);
37
            $formElement = $formInstance->newInstance($name, $options);
38
            /* @var $formElement Element */
39
            $formElement->setValue($options['value']);
40
            $formElement->setAttribute('id', $id);
41
            $output = $instance->render($formElement);
42
            return $output;
43
        }
44
        throw new InvalidViewConfigurationException('Invalid form input type');
45
    }
46
47
48
}
49