Enum::palette()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 4
eloc 8
nc 4
nop 2
dl 0
loc 17
ccs 0
cts 5
cp 0
crap 20
rs 10
c 2
b 0
f 1
1
<?php
2
3
namespace Terranet\Administrator\Field;
4
5
use Illuminate\Contracts\Support\Arrayable;
6
use Illuminate\Support\Arr;
7
use Terranet\Administrator\Exception;
8
9
class Enum extends Field
10
{
11
    /** @var array */
12
    protected $options = [];
13
14
    /** @var array */
15
    protected $colors = ['#777777', '#2574ab', '#259dab', '#5bc0de', '#e6ad5c', '#d9534f'];
16
17
    /** @var array */
18
    protected $palette = [];
19
20
    /** @var bool */
21
    protected $useColors = true;
22
23
    /**
24
     * @return $this
25
     */
26
    public function disableColors()
27
    {
28
        $this->useColors = false;
29
30
        return $this;
31
    }
32
33
    /**
34
     * @param  iterable  $options
35
     * @return self
36
     */
37
    public function setOptions(iterable $options): self
38
    {
39
        if ($options instanceof Arrayable) {
40
            $options = $options->toArray();
41
        }
42
43
        $i = 0;
44
        foreach ($options as $key => $value) {
45
            if (!Arr::has($this->palette, $key)) {
46
                $this->palette[$key] = $this->colors[$i % \count($this->colors)];
47
                ++$i;
48
            }
49
        }
50
        $this->options = $options;
0 ignored issues
show
Documentation Bug introduced by
It seems like $options can also be of type iterable. However, the property $options is declared as type array. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
51
52
        return $this;
53
    }
54
55
    /**
56
     * Set colors palette.
57
     *
58
     * @param  mixed  $color
59
     * @param  null|string  $value
60
     * @return Enum
61
     * @throws Exception
62
     */
63
    public function palette($color, string $value = null)
64
    {
65
        if (\is_array($color)) {
66
            foreach ($color as $name => $code) {
67
                $this->palette($name, $code);
68
            }
69
70
            return $this;
71
        }
72
73
        if (!array_key_exists($color, $this->options)) {
74
            throw new Exception("Unknown option {$color}");
75
        }
76
77
        $this->palette[$color] = $value;
78
79
        return $this;
80
    }
81
82
    /**
83
     * @return array
84
     */
85
    protected function onEdit()
86
    {
87
        return [
88
            'options' => $this->options ?: [],
89
            'color' => $this->useColors && $this->value() ? \Illuminate\Support\Arr::get($this->palette, $this->value()) : null,
90
        ];
91
    }
92
93
    /**
94
     * @return array
95
     */
96
    protected function onIndex()
97
    {
98
        return $this->onEdit();
99
    }
100
101
    /**
102
     * @return array
103
     */
104
    protected function onView()
105
    {
106
        return $this->onEdit();
107
    }
108
}
109