OptionsTrait   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Test Coverage

Coverage 65%

Importance

Changes 0
Metric Value
wmc 11
eloc 20
dl 0
loc 44
ccs 13
cts 20
cp 0.65
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A hasOption() 0 17 5
A getOption() 0 21 6
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