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

OptionsTrait::getOption()   A

Complexity

Conditions 6
Paths 6

Size

Total Lines 21
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 10
c 1
b 0
f 1
dl 0
loc 21
rs 9.2222
cc 6
nc 6
nop 2
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