ObjectSelect::setValue()   A
last analyzed

Complexity

Conditions 6
Paths 5

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 6.2163

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 9
cts 11
cp 0.8182
rs 9.0444
c 0
b 0
f 0
cc 6
nc 5
nop 1
crap 6.2163
1
<?php
2
3
declare(strict_types=1);
4
5
namespace DoctrineModule\Form\Element;
6
7
use Laminas\Form\Element\Select as SelectElement;
8
use Laminas\Stdlib\ArrayUtils;
9
use Traversable;
10
use function array_map;
11
use function is_array;
12
13
class ObjectSelect extends SelectElement
14
{
15
    /** @var Proxy */
16
    protected $proxy;
17
18 6
    public function getProxy() : Proxy
19
    {
20 6
        if ($this->proxy === null) {
21 6
            $this->proxy = new Proxy();
22
        }
23
24 6
        return $this->proxy;
25
    }
26
27
    /**
28
     * @param array|Traversable $options
29
     *
30
     * {@inheritDoc}
31
     */
32
    public function setOptions($options) : self
33
    {
34
        $this->getProxy()->setOptions($options);
0 ignored issues
show
Documentation introduced by
$options is of type array|object<Traversable>, but the function expects a array<integer,*>.

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...
35
36
        return parent::setOptions($options);
37
    }
38
39
    /**
40
     * @param mixed $value
41
     *
42
     * {@inheritDoc}
43
     */
44 1
    public function setOption($key, $value) : self
45
    {
46 1
        $this->getProxy()->setOptions([$key => $value]);
47
48 1
        return parent::setOption($key, $value);
49
    }
50
51
    /**
52
     * {@inheritDoc}
53
     */
54 3
    public function setValue($value)
55
    {
56 3
        $multiple = $this->getAttribute('multiple');
57
58 3
        if ($multiple === true || $multiple === 'multiple') {
59 2
            if ($value instanceof Traversable) {
60 1
                $value = ArrayUtils::iteratorToArray($value);
61 1
            } elseif ($value === null) {
62
                return parent::setValue([]);
63 1
            } elseif (! is_array($value)) {
64
                $value = (array) $value;
65
            }
66
67 2
            return parent::setValue(array_map([$this->getProxy(), 'getValue'], $value));
68
        }
69
70 1
        return parent::setValue($this->getProxy()->getValue($value));
71
    }
72
73
    /**
74
     * {@inheritDoc}
75
     */
76 2
    public function getValueOptions()
77
    {
78 2
        if (! empty($this->valueOptions)) {
79 1
            return $this->valueOptions;
80
        }
81
82 2
        $proxyValueOptions = $this->getProxy()->getValueOptions();
83
84 2
        if (! empty($proxyValueOptions)) {
85 1
            $this->setValueOptions($proxyValueOptions);
86
        }
87
88 2
        return $this->valueOptions;
89
    }
90
}
91