Select::applyValueToOptions()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Spatie\Html\Elements;
4
5
use Illuminate\Support\Str;
6
use Spatie\Html\Selectable;
7
use Spatie\Html\BaseElement;
8
use Illuminate\Support\Collection;
9
10
class Select extends BaseElement
11
{
12
    /** @var string */
13
    protected $tag = 'select';
14
15
    /** @var array */
16
    protected $options = [];
17
18
    /** @var string|iterable */
19
    protected $value = '';
20
21
    /**
22
     * @param bool $strict
23
     * @return static
24
     */
25
    public function multiple($strict = false)
26
    {
27
        $element = clone $this;
28
29
        $element = $element->attribute('multiple');
30
31
        $name = $element->getAttribute('name');
32
33
        if ($name && ! Str::endsWith($name, '[]')) {
34
            $element = $element->name($name.'[]');
35
        }
36
37
        $element->applyValueToOptions($strict);
38
39
        return $element;
40
    }
41
42
    /**
43
     * @param string|null $name
44
     *
45
     * @return static
46
     */
47
    public function name($name)
48
    {
49
        return $this->attribute('name', $name);
50
    }
51
52
    /**
53
     * @param iterable $options
54
     * @param bool $strict
55
     * @return static
56
     */
57 View Code Duplication
    public function options($options, $strict = false)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
58
    {
59
        return $this->addChildren($options, function ($text, $value) use ($strict) {
60
            if (is_array($text)) {
61
                return $this->optgroup($value, $text, $strict);
0 ignored issues
show
Documentation introduced by
$text is of type array, but the function expects a object<Spatie\Html\Elements\iterable>.

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...
62
            }
63
64
            return $this->createOption($text, $value, $strict);
65
        });
66
    }
67
68
    /**
69
     * @param string $label
70
     * @param iterable $options
71
     * @param bool $strict
72
     *
73
     * @return static
74
     */
75 View Code Duplication
    public function optgroup($label, $options, $strict = false)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
76
    {
77
        return Optgroup::create()
78
            ->label($label)
79
            ->addChildren($options, function ($text, $value) use ($strict) {
80
                return $this->createOption($text, $value, $strict);
81
            });
82
83
        return $this->addChild($optgroup);
0 ignored issues
show
Unused Code introduced by
return $this->addChild($optgroup); does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
84
    }
85
86
    /**
87
     * @param string|null $text
88
     *
89
     * @return static
90
     */
91
    public function placeholder($text)
92
    {
93
        return $this->prependChild(
94
            $this->createOption($text, null)
95
                ->selectedIf(! $this->hasSelection())
96
        );
97
    }
98
99
    /**
100
     * @return static
101
     */
102
    public function required()
103
    {
104
        return $this->attribute('required');
105
    }
106
107
    /**
108
     * @param string|iterable $value
109
     * @param bool $strict
110
     *
111
     * @return static
112
     */
113
    public function value($value = null, $strict = false)
114
    {
115
        $element = clone $this;
116
117
        $element->value = $value;
118
119
        $element->applyValueToOptions($strict);
120
121
        return $element;
122
    }
123
124
    protected function hasSelection()
125
    {
126
        return $this->children->contains->hasAttribute('selected');
127
    }
128
129
    protected function createOption($text, $value = null, $strict = false)
130
    {
131
        $selected = false;
132
        if (null !== $value) {
133
            $selected = $strict ?
134
                $value === $this->value :
135
                $value == $this->value;
136
        }
137
138
        return Option::create()
139
            ->value($value)
140
            ->text($text)
141
            ->selectedIf($selected);
142
    }
143
144
    protected function applyValueToOptions($strict = false)
145
    {
146
        $value = Collection::make($this->value);
147
148
        if (! $this->hasAttribute('multiple')) {
149
            $value = $value->take(1);
150
        }
151
152
        $this->children = $this->applyValueToElements($value, $this->children, $strict);
153
    }
154
155
    protected function applyValueToElements($value, Collection $children, $strict = false)
156
    {
157
        return $children->map(function ($child) use ($strict, $value) {
158
            if ($child instanceof Optgroup) {
159
                return $child->setChildren($this->applyValueToElements($value, $child->children, $strict));
0 ignored issues
show
Bug introduced by
The property children cannot be accessed from this context as it is declared protected in class Spatie\Html\BaseElement.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
Documentation introduced by
$this->applyValueToEleme...ild->children, $strict) is of type object<Illuminate\Support\Collection>, but the function expects a array<integer,object<Spatie\Html\HtmlElement>>.

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...
160
            }
161
162
            if ($child instanceof Selectable) {
163
                $attribute = $child->getAttribute('value');
164
                $contains = $strict ?
165
                    $value->containsStrict($attribute) :
166
                    $value->contains($attribute);
167
168
                return $child->selectedIf($contains);
169
            }
170
171
            return $child;
172
        });
173
    }
174
}
175