Completed
Pull Request — master (#7)
by Harry
02:07
created

CollapsedContainer::remove()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 22
Code Lines 14

Duplication

Lines 3
Ratio 13.64 %

Importance

Changes 0
Metric Value
dl 3
loc 22
rs 8.6737
c 0
b 0
f 0
cc 6
eloc 14
nc 4
nop 1
1
<?php
2
3
namespace Graze\DataStructure\Container;
4
5
use ArrayAccess;
6
7
/**
8
 * CollapsedContainer accepts key access using a delimiter to represent child arrays
9
 *
10
 * ## Examples
11
 *
12
 * ```php
13
 * $container = new CollapsedContainer(['first' => 'value', 'second' => ['child' => 1, 'other' => 2]]);
14
 *
15
 * ($container->get('second.child') === 1)
16
 * // true
17
 *
18
 * ($container->has('first.child'))
19
 * // false
20
 *
21
 * $container->set('second.third', 3);
22
 * $container->getAll();
23
 * // ['first' => 'value', 'second' => ['child' => 1, 'other' => 2, 'third'=> 3]]
24
 *
25
 * $container->remove('second.other');
26
 * $container->getAll();
27
 * // ['first' => 'value', 'second' => ['child' => 1, 'third'=> 3]]
28
 * ```
29
 */
30
class CollapsedContainer extends Container
31
{
32
    const DELIMITER = '.';
33
34
    /**
35
     * @param string $key
36
     *
37
     * @return mixed|null
38
     */
39 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...
40
    {
41
        if (mb_strpos($key, static::DELIMITER) !== false) {
42
            $top = $this->params;
43
44
            foreach (explode(static::DELIMITER, $key) as $node) {
45
                if (!isset($top[$node])) {
46
                    return null;
47
                }
48
                $top = $top[$node];
49
            }
50
            return $top;
51
        }
52
53
        return parent::get($key);
54
    }
55
56
    /**
57
     * @param string $key
58
     * @param mixed  $value
59
     *
60
     * @return $this|ContainerInterface
61
     */
62
    public function set($key, $value)
63
    {
64
        if (mb_strpos($key, static::DELIMITER) !== false) {
65
            $split = explode(static::DELIMITER, $key);
66
            $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...
67
            $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...
68 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...
69
                $top = [];
70
            }
71
72
            $last = $split[count($split) - 1];
73
            if ($top instanceof ContainerInterface) {
74
                $top = $top->set($last, $value);
75
            } else {
76
                $top[$last] = $value;
77
            }
78
79
            return $this->set($key, $top);
80
        }
81
82
        return parent::set($key, $value);
83
    }
84
85
    /**
86
     * @param string $key
87
     *
88
     * @return bool
89
     */
90 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...
91
    {
92
        if (mb_strpos($key, static::DELIMITER) !== false) {
93
            $top = $this->params;
94
95
            foreach (explode(static::DELIMITER, $key) as $node) {
96
                if (!isset($top[$node])) {
97
                    return false;
98
                }
99
                $top = $top[$node];
100
            }
101
            return true;
102
        }
103
        return parent::has($key);
104
    }
105
106
    /**
107
     * @param string $key
108
     *
109
     * @return $this|ContainerInterface
110
     */
111
    public function remove($key)
112
    {
113
        if (mb_strpos($key, static::DELIMITER) !== false) {
114
            $split = explode(static::DELIMITER, $key);
115
            $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...
116
            $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...
117 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...
118
                return $this;
119
            }
120
121
            $last = $split[count($split) - 1];
122
            if ($top instanceof ContainerInterface) {
123
                $top = $top->remove($last);
124
            } else {
125
                unset($top[$last]);
126
            }
127
128
            return $this->set($key, $top);
129
        }
130
131
        return parent::remove($key);
132
    }
133
}
134