Passed
Push — master ( fe0c03...8eed1b )
by Enjoys
12:27 queued 10:03
created

Options::getOption()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 11
ccs 0
cts 7
cp 0
rs 10
cc 4
nc 3
nop 3
crap 20
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Enjoys\Forms\Traits;
6
7
trait Options
8
{
9
    /**
10
     *
11
     * @var array<string, mixed>
12
     */
13
    protected array $options = [];
14
15 90
    public function setOption(string $key, mixed $value, bool $useInternalMethods = true): static
16
    {
17 90
        $method = 'set' . ucfirst($key);
18 90
        if ($useInternalMethods === true && method_exists($this, $method)) {
19 1
            $this->$method($value);
20 1
            return $this;
21
        }
22
23 90
        $this->options[$key] = $value;
24 90
        return $this;
25
    }
26
27
    public function getOption(string $key, mixed $defaults = null, bool $useInternalMethods = true): mixed
28
    {
29
        $method = 'get' . ucfirst($key);
30
        if ($useInternalMethods === true && method_exists($this, $method)) {
31
            return $this->$method($defaults);
32
        }
33
34
        if (isset($this->options[$key])) {
35
            return $this->options[$key];
36
        }
37
        return $defaults;
38
    }
39
40
    /**
41
     * @param array<string, mixed> $options
42
     * @psalm-suppress MixedAssignment
43
     */
44
    public function setOptions(array $options = [], bool $useInternalMethods = true): static
45
    {
46
        foreach ($options as $key => $value) {
47
            $this->setOption($key, $value, $useInternalMethods);
48
        }
49
        return $this;
50
    }
51
52
    /**
53
     * @return array<string, mixed>
54
     */
55 90
    public function getOptions(): array
56
    {
57 90
        return $this->options;
58
    }
59
}
60