Completed
Push — master ( 0e678b...8a4e34 )
by Filipe
02:44
created

CommonDriverMethods::setValue()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 10

Duplication

Lines 14
Ratio 100 %

Importance

Changes 0
Metric Value
dl 14
loc 14
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 10
nc 3
nop 3
1
<?php
2
3
/**
4
 * This file is part of Configuration
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace Slick\Configuration\Driver;
11
12
use Slick\Configuration\Exception\FileNotFoundException;
13
14
/**
15
 * Common Driver Methods Trait
16
 *
17
 * @package Slick\Configuration\Driver
18
 */
19
trait CommonDriverMethods
20
{
21
22
    /**
23
     * @var array
24
     */
25
    protected $data = [];
26
27
    /**
28
     * Checks if provided file exists
29
     *
30
     * @param string $file
31
     *
32
     * @throws FileNotFoundException if provided file does not exists
33
     */
34
    protected function checkFile($file)
35
    {
36
        if (!is_file($file)) {
37
            throw new FileNotFoundException(
38
                "Configuration file {$file} could not be found."
39
            );
40
        }
41
    }
42
43
    /**
44
     * Returns the value store with provided key or the default value.
45
     *
46
     * @param string $key     The key used to store the value in configuration
47
     * @param mixed  $default The default value if no value was stored.
48
     *
49
     * @return mixed The stored value or the default value if key
50
     *  was not found.
51
     */
52
    public function get($key, $default = null)
53
    {
54
        return static::getValue($key, $default, $this->data);
55
    }
56
57
    /**
58
     * Set/Store the provided value with a given key.
59
     *
60
     * @param string $key   The key used to store the value in configuration.
61
     * @param mixed  $value The value to store under the provided key.
62
     *
63
     * @return CommonDriverMethods|self Self instance for method call chains.
0 ignored issues
show
Comprehensibility Bug introduced by
The return type CommonDriverMethods is a trait, and thus cannot be used for type-hinting in PHP. Maybe consider adding an interface and use that for type-hinting?

In PHP traits cannot be used for type-hinting as they do not define a well-defined structure. This is because any class that uses a trait can rename that trait’s methods.

If you would like to return an object that has a guaranteed set of methods, you could create a companion interface that lists these methods explicitly.

Loading history...
64
     */
65
    public function set($key, $value)
66
    {
67
        static::setValue($key, $value, $this->data);
68
        return $this;
69
    }
70
71
    /**
72
     * Recursive method to parse dot notation keys and retrieve the value
73
     *
74
     * @param string $key     The key/index to search
75
     * @param mixed  $default The value if key doesn't exists
76
     * @param array  $data    The data to search
77
     *
78
     * @return mixed The stored value or the default value if key
79
     *               or index was not found.
80
     */
81 View Code Duplication
    public static function getValue($key, $default, $data)
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...
82
    {
83
        $parts = explode('.', $key);
84
        $first = array_shift($parts);
85
        if (isset($data[$first])) {
86
            if (count($parts) > 0) {
87
                $newKey = implode('.', $parts);
88
                return static::getValue($newKey, $default, $data[$first]);
89
            }
90
            $default = $data[$first];
91
        }
92
        return $default;
93
    }
94
    /**
95
     * Recursive method to parse dot notation keys and set the value
96
     *
97
     * @param string $key   The key used to store the value in configuration.
98
     * @param mixed  $value The value to store under the provided key.
99
     * @param array  $data  The data to search
100
     */
101 View Code Duplication
    public static function setValue($key, $value, &$data)
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...
102
    {
103
        $parts = explode('.', $key);
104
        $first = array_shift($parts);
105
        if (count($parts) > 0) {
106
            $newKey = implode('.', $parts);
107
            if (!array_key_exists($first, $data)) {
108
                $data[$first] = array();
109
            }
110
            static::setValue($newKey, $value, $data[$first]);
111
            return;
112
        }
113
        $data[$first] = $value;
114
    }
115
}
116