Completed
Push — master ( 10405b...259f27 )
by wen
13:10
created

HasSelectOptions::setOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace Sco\Admin\Traits;
4
5
use InvalidArgumentException;
6
7
trait HasSelectOptions
8
{
9
    use SelectOptionsFromModel;
10
11
    protected $options;
12
13
    public function getOptions()
14
    {
15
        if ($this->options instanceof \Closure) {
16
            $options = ($this->options)();
17
        } elseif ($this->isOptionsModel()) {
18
            $options = $this->getOptionsFromModel();
19
        } elseif (is_array($this->options)) {
20
            $options = $this->options;
21
        } else {
22
            throw new InvalidArgumentException(
23
                sprintf(
24
                    "The %s element[%s] options must be return array(key=>value)",
25
                    $this->getType(),
0 ignored issues
show
Bug introduced by
It seems like getType() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
26
                    $this->getName()
0 ignored issues
show
Bug introduced by
It seems like getName() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
27
                )
28
            );
29
        }
30
31
        return collect($options)->mapWithKeys(function ($value, $key) {
32
            return [
33
                $key => [
34
                    'label' => $value,
35
                    'value' => (string) $key,
36
                ],
37
            ];
38
        })->values();
39
    }
40
41
    /**
42
     * @param mixed $options
43
     *
44
     * @return $this
45
     */
46
    public function setOptions($options)
47
    {
48
        $this->options = $options;
49
50
        return $this;
51
    }
52
}
53