Passed
Push — 6.x ( fe0c03 )
by Enjoys
07:37 queued 06:51
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
use Enjoys\Forms\Form;
8
9
trait Options
10
{
11
    /**
12
     *
13
     * @var array<string, mixed>
14
     */
15
    protected array $options = [];
16
17 90
    public function setOption(string $key, mixed $value, bool $useInternalMethods = true): self
18
    {
19 90
        $method = 'set' . ucfirst($key);
20 90
        if ($useInternalMethods === true && method_exists($this, $method)) {
21 1
            $this->$method($value);
22 1
            return $this;
23
        }
24
25 90
        $this->options[$key] = $value;
26 90
        return $this;
27
    }
28
29
    public function getOption(string $key, mixed $defaults = null, bool $useInternalMethods = true): mixed
30
    {
31
        $method = 'get' . ucfirst($key);
32
        if ($useInternalMethods === true && method_exists($this, $method)) {
33
            return $this->$method($defaults);
34
        }
35
36
        if (isset($this->options[$key])) {
37
            return $this->options[$key];
38
        }
39
        return $defaults;
40
    }
41
42
    /**
43
     * @param array<string, mixed> $options
44
     * @psalm-suppress MixedAssignment
45
     */
46
    public function setOptions(array $options = [], bool $useInternalMethods = true): self
47
    {
48
        foreach ($options as $key => $value) {
49
            $this->setOption($key, $value, $useInternalMethods);
50
        }
51
        return $this;
52
    }
53
54
    /**
55
     * @return array<string, mixed>
56
     */
57 90
    public function getOptions(): array
58
    {
59 90
        return $this->options;
60
    }
61
}
62