HasSelectOptions::getOptions()   B
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 27
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 27
ccs 0
cts 26
cp 0
rs 8.5806
cc 4
eloc 19
nc 4
nop 0
crap 20
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