Completed
Pull Request — master (#158)
by
unknown
01:17
created

Select::name()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Spatie\Html\Elements;
4
5
use Illuminate\Support\Collection;
6
use Illuminate\Support\Str;
7
use Spatie\Html\BaseElement;
8
use Spatie\Html\Elements\Attributes\Autofocus;
9
use Spatie\Html\Elements\Attributes\Disabled;
10
use Spatie\Html\Elements\Attributes\Name;
11
use Spatie\Html\Elements\Attributes\Required;
12
use Spatie\Html\Selectable;
13
14
class Select extends BaseElement
15
{
16
    use Autofocus;
17
    use Disabled;
18
    use Name;
19
    use Required;
20
21
    /** @var string */
22
    protected $tag = 'select';
23
24
    /** @var array */
25
    protected $options = [];
26
27
    /** @var string|iterable */
28
    protected $value = '';
29
30
    /**
31
     * @return static
32
     */
33
    public function multiple()
34
    {
35
        $element = clone $this;
36
37
        $element = $element->attribute('multiple');
38
39
        $name = $element->getAttribute('name');
40
41
        if ($name && ! Str::endsWith($name, '[]')) {
42
            $element = $element->name($name.'[]');
43
        }
44
45
        $element->applyValueToOptions();
46
47
        return $element;
48
    }
49
50
    /**
51
     * @param iterable $options
52
     *
53
     * @return static
54
     */
55 View Code Duplication
    public function options($options)
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...
56
    {
57
        return $this->addChildren($options, function ($text, $value) {
58
            if (is_array($text)) {
59
                return $this->optgroup($value, $text);
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...
60
            }
61
62
            return Option::create()
63
                ->value($value)
64
                ->text($text)
65
                ->selectedIf($value === $this->value);
66
        });
67
    }
68
69
    /**
70
     * @param string $label
71
     * @param iterable $options
72
     *
73
     * @return static
74
     */
75 View Code Duplication
    public function optgroup($label, $options)
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) {
80
                return Option::create()
81
                    ->value($value)
82
                    ->text($text)
83
                    ->selectedIf($value === $this->value);
84
            });
85
86
        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...
87
    }
88
89
    /**
90
     * @param string|null $text
91
     *
92
     * @return static
93
     */
94
    public function placeholder($text)
95
    {
96
        return $this->prependChild(
97
            Option::create()
98
                ->value(null)
99
                ->text($text)
100
                ->selectedIf(! $this->hasSelection())
101
        );
102
    }
103
104
    /**
105
     * @param string|iterable $value
106
     *
107
     * @return static
108
     */
109
    public function value($value = null)
110
    {
111
        $element = clone $this;
112
113
        $element->value = $value;
114
115
        $element->applyValueToOptions();
116
117
        return $element;
118
    }
119
120
    protected function hasSelection()
121
    {
122
        return $this->children->contains->hasAttribute('selected');
123
    }
124
125
    protected function applyValueToOptions()
126
    {
127
        $value = Collection::make($this->value);
128
129
        if (! $this->hasAttribute('multiple')) {
130
            $value = $value->take(1);
131
        }
132
133
        $this->children = $this->applyValueToElements($value, $this->children);
134
    }
135
136
    protected function applyValueToElements($value, Collection $children)
137
    {
138
        return $children->map(function ($child) use ($value) {
139
            if ($child instanceof Optgroup) {
140
                return $child->setChildren($this->applyValueToElements($value, $child->children));
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...alue, $child->children) 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...
141
            }
142
143
            if ($child instanceof Selectable) {
144
                return $child->selectedIf($value->contains($child->getAttribute('value')));
145
            }
146
147
            return $child;
148
        });
149
    }
150
}
151