Completed
Pull Request — master (#7)
by Harry
01:43
created

FlatContainer::has()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 7
Ratio 100 %

Importance

Changes 0
Metric Value
dl 7
loc 7
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
/*
3
 * This file is part of Graze DataStructure
4
 *
5
 * Copyright (c) 2017 Nature Delivered Ltd. <http://graze.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @see  http://github.com/graze/data-structure/blob/master/LICENSE
11
 * @link http://github.com/graze/data-structure
12
 */
13
14
namespace Graze\DataStructure\Container;
15
16
use ArrayAccess;
17
18
/**
19
 * FlatContainer accepts key access using a delimiter to represent child arrays
20
 *
21
 * ## Examples
22
 *
23
 * ```php
24
 * $container = new FlatContainer(['first' => 'value', 'second' => ['child' => 1, 'other' => 2]]);
25
 *
26
 * $container->get('second.child')
27
 * // 1
28
 *
29
 * ($container->has('first.child'))
30
 * // false
31
 *
32
 * $container->set('second.third', 3);
33
 * $container->getAll();
34
 * // ['first' => 'value', 'second' => ['child' => 1, 'other' => 2, 'third'=> 3]]
35
 *
36
 * $container->remove('second.other');
37
 * $container->getAll();
38
 * // ['first' => 'value', 'second' => ['child' => 1, 'third'=> 3]]
39
 * ```
40
 */
41
class FlatContainer extends Container
42
{
43
    const DELIMITER = '.';
44
45
    /**
46
     * @param string $key
47
     *
48
     * @return mixed|null
49
     */
50
    private function getChild($key)
51
    {
52
        $top = $this->params;
53
54
        foreach (explode(static::DELIMITER, $key) as $node) {
55
            if (!isset($top[$node])) {
56
                return null;
57
            }
58
            $top = $top[$node];
59
        }
60
        return $top;
61
    }
62
63
    /**
64
     * @param string $key
65
     *
66
     * @return string[] split the key into everything up to the last delimiter, and the last key
67
     */
68
    private function splitToLast($key)
69
    {
70
        $split = explode(static::DELIMITER, $key);
71
        $key = implode(static::DELIMITER, array_slice($split, 0, -1));
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
72
        $last = end($split);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
73
74
        return [$key, $last];
75
    }
76
77
    /**
78
     * @param string $key The key to access, supports delimiter based child access (e.g. `parent.child.node`)
79
     *
80
     * @return bool
81
     */
82 View Code Duplication
    public function has($key)
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...
83
    {
84
        if (mb_strpos($key, static::DELIMITER) !== false) {
85
            return (!is_null($this->getChild($key)));
86
        }
87
        return parent::has($key);
88
    }
89
90
    /**
91
     * @param string $key The key to access, supports delimiter based child access (e.g. `parent.child.node`)
92
     *
93
     * @return mixed|null
94
     */
95 View Code Duplication
    public function get($key)
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...
96
    {
97
        if (mb_strpos($key, static::DELIMITER) !== false) {
98
            return $this->getChild($key);
99
        }
100
101
        return parent::get($key);
102
    }
103
104
    /**
105
     * @param string $key The key to access, supports delimiter based child access (e.g. `parent.child.node`)
106
     * @param mixed  $value
107
     *
108
     * @return ContainerInterface
109
     */
110
    public function set($key, $value)
111
    {
112
        return $this->doSet($key, $value);
113
    }
114
115
    /**
116
     * @param string $key
117
     * @param mixed  $value
118
     *
119
     * @return ContainerInterface
120
     */
121
    protected function doSet($key, $value)
122
    {
123
        if (mb_strpos($key, static::DELIMITER) !== false) {
124
            list($key, $last) = $this->splitToLast($key);
125
126
            $top = $this->get($key);
127 View Code Duplication
            if (is_null($top) || (!is_array($top) && !($top instanceof ArrayAccess))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
128
                $top = [];
129
            }
130
131
            if ($top instanceof ContainerInterface) {
132
                $top = $top->set($last, $value);
133
            } else {
134
                $top[$last] = $value;
135
            }
136
137
            return $this->doSet($key, $top);
138
        }
139
140
        return parent::set($key, $value);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (set() instead of doSet()). Are you sure this is correct? If so, you might want to change this to $this->set().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
141
    }
142
143
    /**
144
     * @param string $key The key to access, supports delimiter based child access (e.g. `parent.child.node`)
145
     *
146
     * @return ContainerInterface
147
     */
148
    public function remove($key)
149
    {
150
        return $this->doRemove($key);
151
    }
152
153
    /**
154
     * @param string $key
155
     *
156
     * @return ContainerInterface
157
     */
158
    protected function doRemove($key)
159
    {
160
        if (mb_strpos($key, static::DELIMITER) !== false) {
161
            list($key, $last) = $this->splitToLast($key);
162
163
            $top = $this->get($key);
164 View Code Duplication
            if (is_null($top) || (!is_array($top) && !($top instanceof ArrayAccess))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
165
                return $this;
166
            }
167
168
            if ($top instanceof ContainerInterface) {
169
                $top = $top->remove($last);
170
            } else {
171
                unset($top[$last]);
172
            }
173
174
            return $this->doSet($key, $top);
175
        }
176
177
        return parent::remove($key);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (remove() instead of doRemove()). Are you sure this is correct? If so, you might want to change this to $this->remove().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
178
    }
179
}
180