Passed
Push — master ( 10a192...087136 )
by Aleksei
07:38 queued 05:42
created

OptionsTrait   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 11
eloc 20
dl 0
loc 56
c 0
b 0
f 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A hasOption() 0 17 5
A getOption() 0 21 6
1
<?php
2
3
/**
4
 * Spiral Framework.
5
 *
6
 * @license MIT
7
 * @author  Anton Titov (Wolfy-J)
8
 */
9
10
declare(strict_types=1);
11
12
namespace Cycle\Migrations\Operation\Traits;
13
14
/**
15
 * Consumer must define property "aliases".
16
 */
17
trait OptionsTrait
18
{
19
    /** @var array */
20
    protected $options = [];
21
22
    /**
23
     * @param string $name
24
     *
25
     * @return bool
26
     */
27
    protected function hasOption(string $name): bool
28
    {
29
        if (array_key_exists($name, $this->options)) {
30
            return true;
31
        }
32
33
        if (!isset($this->aliases[$name])) {
34
            return false;
35
        }
36
37
        foreach ($this->aliases as $source => $aliases) {
38
            if (in_array($name, $aliases, true)) {
39
                return true;
40
            }
41
        }
42
43
        return false;
44
    }
45
46
    /**
47
     * @param string $name
48
     * @param mixed  $default
49
     *
50
     * @return mixed
51
     */
52
    protected function getOption(string $name, $default = null)
53
    {
54
        if (!$this->hasOption($name)) {
55
            return $default;
56
        }
57
58
        if (array_key_exists($name, $this->options)) {
59
            return $this->options[$name];
60
        }
61
62
        if (!isset($this->aliases[$name])) {
63
            return $default;
64
        }
65
66
        foreach ($this->aliases as $source => $aliases) {
67
            if (in_array($name, $aliases, true)) {
68
                return $this->getOption($source);
69
            }
70
        }
71
72
        return false;
73
    }
74
}
75