Passed
Pull Request — master (#28)
by Enjoys
21:42 queued 16:27
created

Options   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Test Coverage

Coverage 45%

Importance

Changes 3
Bugs 1 Features 1
Metric Value
wmc 10
eloc 18
c 3
b 1
f 1
dl 0
loc 51
ccs 9
cts 20
cp 0.45
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setOption() 0 10 3
A getOption() 0 11 4
A setOptions() 0 6 2
A getOptions() 0 3 1
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