Completed
Pull Request — master (#7)
by Harry
04:22
created

FlatContainer::set()   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 2
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 bool
49
     */
50 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...
51
    {
52
        if (mb_strpos($key, static::DELIMITER) !== false) {
53
            $top = $this->getAll();
54
55
            foreach (explode(static::DELIMITER, $key) as $node) {
56
                if (!isset($top[$node])) {
57
                    return false;
58
                }
59
                $top = $top[$node];
60
            }
61
            return true;
62
        }
63
        return parent::has($key);
64
    }
65
66
    /**
67
     * @param string $key
68
     *
69
     * @return mixed|null
70
     */
71 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...
72
    {
73
        if (mb_strpos($key, static::DELIMITER) !== false) {
74
            $top = $this->getAll();
75
76
            foreach (explode(static::DELIMITER, $key) as $node) {
77
                if (!isset($top[$node])) {
78
                    return null;
79
                }
80
                $top = $top[$node];
81
            }
82
            return $top;
83
        }
84
85
        return parent::get($key);
86
    }
87
88
    /**
89
     * @param string $key
90
     * @param mixed  $value
91
     *
92
     * @return ContainerInterface
93
     */
94
    public function set($key, $value)
95
    {
96
        return $this->doSet($key, $value);
97
    }
98
99
    /**
100
     * @param string $key
101
     * @param mixed  $value
102
     *
103
     * @return ContainerInterface
104
     */
105
    protected function doSet($key, $value)
106
    {
107
        if (mb_strpos($key, static::DELIMITER) !== false) {
108
            $split = explode(static::DELIMITER, $key);
109
            $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...
110
            $top = $this->get($key);
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...
111 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...
112
                $top = [];
113
            }
114
115
            $last = $split[count($split) - 1];
116
            if ($top instanceof ContainerInterface) {
117
                $top = $top->set($last, $value);
118
            } else {
119
                $top[$last] = $value;
120
            }
121
122
            return $this->doSet($key, $top);
123
        }
124
125
        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...
126
    }
127
128
    /**
129
     * @param string $key
130
     *
131
     * @return ContainerInterface
132
     */
133
    public function remove($key)
134
    {
135
        return $this->doRemove($key);
136
    }
137
138
    /**
139
     * @param string $key
140
     *
141
     * @return ContainerInterface
142
     */
143
    protected function doRemove($key)
144
    {
145
        if (mb_strpos($key, static::DELIMITER) !== false) {
146
            $split = explode(static::DELIMITER, $key);
147
            $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...
148
            $top = $this->get($key);
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...
149 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...
150
                return $this;
151
            }
152
153
            $last = $split[count($split) - 1];
154
            if ($top instanceof ContainerInterface) {
155
                $top = $top->remove($last);
156
            } else {
157
                unset($top[$last]);
158
            }
159
160
            return $this->doSet($key, $top);
161
        }
162
163
        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...
164
    }
165
}
166