Passed
Branch master (41990d)
by Gabor
03:31
created

AbstractFormElementContainer::getBaseClassName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * WebHemi.
4
 *
5
 * PHP version 5.6
6
 *
7
 * @copyright 2012 - 2016 Gixx-web (http://www.gixx-web.com)
8
 * @license   https://opensource.org/licenses/MIT The MIT License (MIT)
9
 *
10
 * @link      http://www.gixx-web.com
11
 */
12
namespace WebHemi\Form\Element;
13
14
use RuntimeException;
15
16
/**
17
 * Class AbstractFormElementContainer
18
 */
19
abstract class AbstractFormElementContainer implements FormElementContainerInterface
20
{
21
    /** @var FormElementInterface */
22
    protected $formElementPrototypes;
23
24
    /**
25
     * FormElementContainer constructor.
26
     *
27
     * @param FormElementInterface[] ...$formElementPrototypes
28
     */
29 12
    public function __construct(FormElementInterface ...$formElementPrototypes)
30
    {
31 12
        foreach ($formElementPrototypes as $formElement) {
32 4
            $this->formElementPrototypes[$this->getBaseClassName($formElement)] = $formElement;
0 ignored issues
show
Documentation introduced by
$formElement is of type array<integer,object<Web...\FormElementInterface>>, but the function expects a object<WebHemi\Form\Element\FormElementInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
33 12
        }
34 12
    }
35
36
    /**
37
     * Searches for suitable form element and returns a cloned instance.
38
     *
39
     * @param string $name
40
     * @param array  $arguments
41
     *
42
     * @return FormElementInterface
43
     */
44 3
    public function __call($name, $arguments)
45
    {
46 3
        $classBaseName = substr($name, 3);
47
48 3
        if (!isset($this->formElementPrototypes[$classBaseName])) {
49 1
            throw new RuntimeException(sprintf('%s is not a valid form element.', $classBaseName));
50
        }
51
52
        /** @var FormElementInterface $formElement */
53 3
        $formElement = clone $this->formElementPrototypes[$classBaseName];
54
55
        // We have to be able to set the constructor parameters when we get the object via this container.
56 3
        if (isset($arguments[0])) {
57 3
            $formElement->setName($arguments[0]);
58 3
        }
59
60 3
        if (isset($arguments[1])) {
61 1
            $formElement->setLabel($arguments[1]);
62 1
        }
63
64 3
        if (isset($arguments[2])) {
65 1
            $formElement->setValue($arguments[2]);
66 1
        }
67
68 3
        return $formElement;
69
    }
70
71
    /**
72
     * Returns the base name of the class (no namespace).
73
     *
74
     * @param FormElementInterface $formElement
75
     *
76
     * @return string
77
     */
78 4
    protected function getBaseClassName(FormElementInterface $formElement)
79
    {
80 4
        $fullClassName = get_class($formElement);
81 4
        $namespaces = explode('\\', $fullClassName);
82 4
        return array_pop($namespaces);
83
    }
84
}
85