Completed
Push — master ( 5ffbf7...8e1537 )
by Carlos
03:33 queued 01:30
created

Config::has()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of the overtrue/socialite.
5
 *
6
 * (c) overtrue <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Overtrue\Socialite;
13
14
use ArrayAccess;
15
use InvalidArgumentException;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Overtrue\Socialite\InvalidArgumentException.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
16
17
/**
18
 * Class Config.
19
 */
20
class Config implements ArrayAccess
21
{
22
    /**
23
     * @var array
24
     */
25
    protected $config;
26
27
    /**
28
     * Config constructor.
29
     *
30
     * @param array $config
31
     */
32
    public function __construct(array $config)
33
    {
34
        $this->config = $config;
35
    }
36
37
    /**
38
     * Get an item from an array using "dot" notation.
39
     *
40
     * @param string $key
41
     * @param mixed  $default
42
     *
43
     * @return mixed
44
     */
45 View Code Duplication
    public function get($key, $default = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
46
    {
47
        $config = $this->config;
48
49
        if (is_null($key)) {
50
            return $config;
51
        }
52
        if (isset($config[$key])) {
53
            return $config[$key];
54
        }
55
        foreach (explode('.', $key) as $segment) {
56
            if (!is_array($config) || !array_key_exists($segment, $config)) {
57
                return $default;
58
            }
59
            $config = $config[$segment];
60
        }
61
62
        return $config;
63
    }
64
65
    /**
66
     * Set an array item to a given value using "dot" notation.
67
     *
68
     * @param string $key
69
     * @param mixed  $value
70
     *
71
     * @return array
72
     */
73
    public function set($key, $value)
74
    {
75
        if (is_null($key)) {
76
            throw new InvalidArgumentException('Invalid config key.');
77
        }
78
79
        $keys = explode('.', $key);
80
81
        while (count($keys) > 1) {
82
            $key = array_shift($keys);
83
            if (!isset($this->config[$key]) || !is_array($this->config[$key])) {
84
                $this->config[$key] = [];
85
            }
86
            $this->config = &$this->config[$key];
87
        }
88
89
        $this->config[array_shift($keys)] = $value;
90
91
        return $this->config;
92
    }
93
94
    /**
95
     * Determine if the given configuration value exists.
96
     *
97
     * @param  string $key
98
     *
99
     * @return bool
100
     */
101
    public function has($key)
102
    {
103
        return (bool) $this->get($key);
104
    }
105
106
    /**
107
     * Whether a offset exists.
108
     *
109
     * @link  http://php.net/manual/en/arrayaccess.offsetexists.php
110
     *
111
     * @param mixed $offset <p>
112
     *                      An offset to check for.
113
     *                      </p>
114
     *
115
     * @return bool true on success or false on failure.
116
     *              </p>
117
     *              <p>
118
     *              The return value will be casted to boolean if non-boolean was returned
119
     *
120
     * @since 5.0.0
121
     */
122
    public function offsetExists($offset)
123
    {
124
        return array_key_exists($offset, $this->config);
125
    }
126
127
    /**
128
     * Offset to retrieve.
129
     *
130
     * @link  http://php.net/manual/en/arrayaccess.offsetget.php
131
     *
132
     * @param mixed $offset <p>
133
     *                      The offset to retrieve.
134
     *                      </p>
135
     *
136
     * @return mixed Can return all value types
137
     *
138
     * @since 5.0.0
139
     */
140
    public function offsetGet($offset)
141
    {
142
        return $this->get($offset);
143
    }
144
145
    /**
146
     * Offset to set.
147
     *
148
     * @link  http://php.net/manual/en/arrayaccess.offsetset.php
149
     *
150
     * @param mixed $offset <p>
151
     *                      The offset to assign the value to.
152
     *                      </p>
153
     * @param mixed $value  <p>
154
     *                      The value to set.
155
     *                      </p>
156
     *
157
     * @since 5.0.0
158
     */
159
    public function offsetSet($offset, $value)
160
    {
161
        $this->set($offset, $value);
162
    }
163
164
    /**
165
     * Offset to unset.
166
     *
167
     * @link  http://php.net/manual/en/arrayaccess.offsetunset.php
168
     *
169
     * @param mixed $offset <p>
170
     *                      The offset to unset.
171
     *                      </p>
172
     *
173
     * @since 5.0.0
174
     */
175
    public function offsetUnset($offset)
176
    {
177
        $this->set($offset, null);
178
    }
179
}
180