Completed
Push — master ( 8b8afe...5f2bbe )
by Greg
02:21
created

src/Config/Config.php (3 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
namespace Robo\Config;
3
4
use Consolidation\Config\Util\ConfigOverlay;
5
use Consolidation\Config\ConfigInterface;
6
7
class Config extends ConfigOverlay implements GlobalOptionDefaultValuesInterface
8
{
9
    const PROGRESS_BAR_AUTO_DISPLAY_INTERVAL = 'options.progress-delay';
10
    const DEFAULT_PROGRESS_DELAY = 2;
11
    const SIMULATE = 'options.simulate';
12
13
    // Read-only configuration properties; changing these has no effect.
14
    const INTERACTIVE = 'options.interactive';
15
    const DECORATED = 'options.decorated';
16
17
    /**
18
     * Create a new configuration object, and initialize it with
19
     * the provided nested array containing configuration data.
20
     */
21
    public function __construct(array $data = null)
22
    {
23
        parent::__construct();
24
25
        $this->import($data);
0 ignored issues
show
It seems like $data defined by parameter $data on line 21 can also be of type null; however, Robo\Config\Config::import() does only seem to accept array, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
26
        $this->defaults = $this->getGlobalOptionDefaultValues();
0 ignored issues
show
The property defaults 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...
27
    }
28
29
    /**
30
     * {@inheritdoc}
31
     */
32
    public function import($data)
33
    {
34
        return $this->replace($data);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->replace($data); (Robo\Config\Config) is incompatible with the return type declared by the interface Consolidation\Config\ConfigInterface::import of type Consolidation\Config\Config.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    public function replace($data)
41
    {
42
        $this->getContext(ConfigOverlay::DEFAULT_CONTEXT)->replace($data);
43
        return $this;
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    public function combine($data)
50
    {
51
        $this->getContext(ConfigOverlay::DEFAULT_CONTEXT)->combine($data);
52
        return $this;
53
    }
54
55
    /**
56
     * Return an associative array containing all of the global configuration
57
     * options and their default values.
58
     *
59
     * @return array
60
     */
61
    public function getGlobalOptionDefaultValues()
62
    {
63
        $globalOptions =
64
        [
65
            self::PROGRESS_BAR_AUTO_DISPLAY_INTERVAL => self::DEFAULT_PROGRESS_DELAY,
66
            self::SIMULATE => false,
67
        ];
68
        return $this->trimPrefixFromGlobalOptions($globalOptions);
69
    }
70
71
    /**
72
     * Remove the 'options.' prefix from the global options list.
73
     */
74
    protected function trimPrefixFromGlobalOptions($globalOptions)
75
    {
76
        $result = [];
77
        foreach ($globalOptions as $option => $value) {
78
            $option = str_replace('options.', '', $option);
79
            $result[$option] = $value;
80
        }
81
        return $result;
82
    }
83
84
    /**
85
     * @deprecated Use $config->get(Config::SIMULATE)
86
     *
87
     * @return bool
88
     */
89
    public function isSimulated()
90
    {
91
        return $this->get(self::SIMULATE);
92
    }
93
94
    /**
95
     * @deprecated Use $config->set(Config::SIMULATE, true)
96
     *
97
     * @param bool $simulated
98
     *
99
     * @return $this
100
     */
101
    public function setSimulated($simulated = true)
102
    {
103
        return $this->set(self::SIMULATE, $simulated);
104
    }
105
106
    /**
107
     * @deprecated Use $config->get(Config::INTERACTIVE)
108
     *
109
     * @return bool
110
     */
111
    public function isInteractive()
112
    {
113
        return $this->get(self::INTERACTIVE);
114
    }
115
116
    /**
117
     * @deprecated Use $config->set(Config::INTERACTIVE, true)
118
     *
119
     * @param bool $interactive
120
     *
121
     * @return $this
122
     */
123
    public function setInteractive($interactive = true)
124
    {
125
        return $this->set(self::INTERACTIVE, $interactive);
126
    }
127
128
    /**
129
     * @deprecated Use $config->get(Config::DECORATED)
130
     *
131
     * @return bool
132
     */
133
    public function isDecorated()
134
    {
135
        return $this->get(self::DECORATED);
136
    }
137
138
    /**
139
     * @deprecated Use $config->set(Config::DECORATED, true)
140
     *
141
     * @param bool $decorated
142
     *
143
     * @return $this
144
     */
145
    public function setDecorated($decorated = true)
146
    {
147
        return $this->set(self::DECORATED, $decorated);
148
    }
149
150
    /**
151
     * @deprecated Use $config->set(Config::PROGRESS_BAR_AUTO_DISPLAY_INTERVAL, $interval)
152
     *
153
     * @param int $interval
154
     *
155
     * @return $this
156
     */
157
    public function setProgressBarAutoDisplayInterval($interval)
158
    {
159
        return $this->set(self::PROGRESS_BAR_AUTO_DISPLAY_INTERVAL, $interval);
160
    }
161
}
162