Completed
Push — master ( 787b04...b8c3a6 )
by Miguel
01:15
created

FeatureManager::setMasterSwitch()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace M1guelpf\Feature;
4
5
class FeatureManager
6
{
7
    /** @var bool|null */
8
    protected static $masterSwitch = null;
9
10
    /**
11
     * Create a new FeatureManager Instance.
12
     *
13
     * @param array $features Features to load
14
     *
15
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
16
     */
17
    public function __construct(array $features = [])
18
    {
19
        $this->features = $features;
0 ignored issues
show
Bug introduced by
The property features 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...
20
    }
21
22
    /**
23
     * Check if a feature is enabled.
24
     *
25
     * @param string $name Name of the feature to check
26
     * @param string $function Check for a specific function
27
     *
28
     * @return bool
29
     */
30
    public function enabled(string $name, string $function = '') : bool
31
    {
32
        if (! is_null(static::$masterSwitch)) {
33
            return static::$masterSwitch;
34
        }
35
36
        $feature = $this->feature($name);
37
38
        if (! empty(trim($function))) {
39
            return is_array($feature) ? value(array_get($feature, $function, false)) : (bool) value($feature);
40
        }
41
42
        return (bool) value($feature);
43
    }
44
45
    /**
46
     * Get feature options.
47
     *
48
     * @param string $name Name of the feature to check
49
     *
50
     * @return bool
51
     */
52
    public function feature(string $name)
53
    {
54
        return array_get($this->features, $name, false);
55
    }
56
57
    /**
58
     * Set the master switch.
59
     *
60
     * @param bool $value Value to set the master switch to
61
     *
62
     * @return self
63
     */
64
    public static function setMasterSwitch(bool $value)
65
    {
66
        static::$masterSwitch = $value;
67
68
        return $this;
0 ignored issues
show
Bug introduced by
The variable $this does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
69
    }
70
}
71