Completed
Branch feature/pre-split (ea2bf3)
by Anton
03:13
created

OptionsTrait::hasOption()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 8
nc 4
nop 1
dl 0
loc 16
rs 8.8571
c 0
b 0
f 0
1
<?php
2
/**
3
 * Spiral Framework.
4
 *
5
 * @license   MIT
6
 * @author    Anton Titov (Wolfy-J)
7
 */
8
namespace Spiral\Migrations\Operations\Traits;
9
10
/**
11
 * Consumer must define property "aliases".
12
 */
13
trait OptionsTrait
14
{
15
    /**
16
     * @var array
17
     */
18
    protected $options = [];
19
20
    /**
21
     * @param string $name
22
     *
23
     * @return bool
24
     */
25
    protected function hasOption(string $name): bool
26
    {
27
        if (array_key_exists($name, $this->options)) {
28
            return true;
29
        }
30
31
        if (!isset($this->aliases) || !isset($this->aliases[$name])) {
1 ignored issue
show
Bug introduced by
The property aliases does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
32
            return false;
33
        }
34
35
        foreach ($this->aliases[$name] as $name) {
36
            return $this->hasOption($name);
37
        }
38
39
        return false;
40
    }
41
42
    /**
43
     * @param string $name
44
     * @param mixed  $default
45
     *
46
     * @return mixed
47
     */
48
    protected function getOption(string $name, $default = null)
49
    {
50
        if (!$this->hasOption($name)) {
51
            return $default;
52
        }
53
54
        if (array_key_exists($name, $this->options)) {
55
            return $this->options[$name];
56
        }
57
58
        if (!isset($this->aliases) || !isset($this->aliases[$name])) {
59
            return false;
60
        }
61
62
        foreach ($this->aliases[$name] as $name) {
63
            return $this->hasOption($name);
64
        }
65
66
        return false;
67
    }
68
}