OptionsTrait::getOption()   A
last analyzed

Complexity

Conditions 6
Paths 6

Size

Total Lines 21
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 11.8436

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 21
ccs 5
cts 11
cp 0.4545
rs 9.2222
c 0
b 0
f 0
cc 6
nc 6
nop 2
crap 11.8436
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cycle\Migrations\Operation\Traits;
6
7
/**
8
 * Consumer must define property "aliases".
9
 */
10
trait OptionsTrait
11
{
12
    protected array $options = [];
13
14 264
    protected function hasOption(string $name): bool
15
    {
16 264
        if (array_key_exists($name, $this->options)) {
17 232
            return true;
18
        }
19
20 122
        if (!isset($this->aliases[$name])) {
21 120
            return false;
22
        }
23
24 122
        foreach ($this->aliases as $source => $aliases) {
25 122
            if (in_array($name, $aliases, true)) {
26
                return true;
27
            }
28
        }
29
30 122
        return false;
31
    }
32
33 264
    protected function getOption(string $name, mixed $default = null): mixed
34
    {
35 264
        if (!$this->hasOption($name)) {
36 120
            return $default;
37
        }
38
39 232
        if (array_key_exists($name, $this->options)) {
40 232
            return $this->options[$name];
41
        }
42
43
        if (!isset($this->aliases[$name])) {
44
            return $default;
45
        }
46
47
        foreach ($this->aliases as $source => $aliases) {
48
            if (in_array($name, $aliases, true)) {
49
                return $this->getOption($source);
50
            }
51
        }
52
53
        return false;
54
    }
55
}
56