AbstractContext   A
last analyzed

Complexity

Total Complexity 32

Size/Duplication

Total Lines 222
Duplicated Lines 2.25 %

Coupling/Cohesion

Components 2
Dependencies 1

Test Coverage

Coverage 95.89%

Importance

Changes 0
Metric Value
dl 5
loc 222
c 0
b 0
f 0
wmc 32
lcom 2
cbo 1
ccs 70
cts 73
cp 0.9589
rs 9.84

14 Methods

Rating   Name   Duplication   Size   Complexity  
A getParent() 0 4 1
A getTopParent() 0 8 2
A setParent() 0 6 1
A getSubContext() 0 15 3
A getSubContextByClass() 0 4 2
B addSubContext() 0 22 6
A removeSubContext() 0 11 2
A containsSubContext() 0 4 1
A clearSubContexts() 0 9 2
A getIterator() 0 4 1
A debugPrintTree() 5 20 4
A __toString() 0 4 1
A getPath() 0 20 5
A createSubContext() 0 6 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/*
4
 * This file is part of the LightSAML-Core package.
5
 *
6
 * (c) Milos Tomic <[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 LightSaml\Context;
13
14
abstract class AbstractContext implements ContextInterface
15
{
16
    /** @var ContextInterface|null */
17
    private $parent;
18
19
    /** @var ContextInterface[] */
20
    private $subContexts = array();
21
22
    /**
23
     * @return ContextInterface|null
24
     */
25 71
    public function getParent()
26
    {
27 71
        return $this->parent;
28
    }
29
30
    /**
31
     * @return ContextInterface
32
     */
33 63
    public function getTopParent()
34
    {
35 63
        if ($this->getParent()) {
36 3
            return $this->getParent()->getTopParent();
37
        }
38
39 63
        return $this;
40
    }
41
42
    /**
43
     * @param ContextInterface|null $parent
44
     *
45
     * @return ContextInterface
46
     */
47 130
    public function setParent(ContextInterface $parent = null)
48
    {
49 130
        $this->parent = $parent;
50
51 130
        return $this;
52
    }
53
54
    /**
55
     * @param string      $name
56
     * @param null|string $class
57
     *
58
     * @return ContextInterface|null
59
     */
60 134
    public function getSubContext($name, $class = null)
61
    {
62 134
        if (isset($this->subContexts[$name])) {
63 103
            return $this->subContexts[$name];
64
        }
65
66 131
        if ($class) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $class of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
67 129
            $result = $this->createSubContext($class);
68 129
            $this->addSubContext($name, $result);
69
70 129
            return $result;
71
        }
72
73 4
        return null;
74
    }
75
76
    /**
77
     * @param string $class
78
     * @param bool   $autoCreate
79
     *
80
     * @return ContextInterface|null
81
     */
82
    public function getSubContextByClass($class, $autoCreate)
83
    {
84
        return $this->getSubContext($class, $autoCreate ? $class : null);
85
    }
86
87
    /**
88
     * @param string                  $name
89
     * @param object|ContextInterface $subContext
90
     *
91
     * @return AbstractContext
92
     */
93 139
    public function addSubContext($name, $subContext)
94
    {
95 139
        if (false === is_object($subContext)) {
96 1
            throw new \InvalidArgumentException('Expected object or ContextInterface');
97
        }
98
99 138
        $existing = isset($this->subContexts[$name]) ? $this->subContexts[$name] : null;
100 138
        if ($existing === $subContext) {
101 1
            return $this;
102
        }
103
104 138
        $this->subContexts[$name] = $subContext;
105 138
        if ($subContext instanceof ContextInterface) {
106 130
            $subContext->setParent($this);
107
        }
108
109 138
        if ($existing instanceof ContextInterface) {
110 1
            $existing->setParent(null);
111
        }
112
113 138
        return $this;
114
    }
115
116
    /**
117
     * @param string $name
118
     *
119
     * @return ContextInterface
120
     */
121 2
    public function removeSubContext($name)
122
    {
123 2
        $subContext = $this->getSubContext($name, false);
0 ignored issues
show
Documentation introduced by
false is of type boolean, but the function expects a null|string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
124
125 2
        if ($subContext) {
126 2
            $subContext->setParent(null);
127 2
            unset($this->subContexts[$name]);
128
        }
129
130 2
        return $this;
131
    }
132
133
    /**
134
     * @param string $name
135
     *
136
     * @return bool
137
     */
138 2
    public function containsSubContext($name)
139
    {
140 2
        return isset($this->subContexts[$name]);
141
    }
142
143
    /**
144
     * @return ContextInterface
145
     */
146 1
    public function clearSubContexts()
147
    {
148 1
        foreach ($this->subContexts as $subContext) {
149 1
            $subContext->setParent(null);
150
        }
151 1
        $this->subContexts = array();
152
153 1
        return $this;
154
    }
155
156
    /**
157
     * @return \ArrayIterator
158
     */
159 6
    public function getIterator()
160
    {
161 6
        return new \ArrayIterator($this->subContexts);
162
    }
163
164
    /**
165
     * @param string $ownName
166
     *
167
     * @return array
168
     */
169 2
    public function debugPrintTree($ownName = 'root')
170
    {
171
        $result = array(
172 2
            $ownName => static::class,
173
        );
174
175 2
        if ($this->subContexts) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->subContexts of type LightSaml\Context\ContextInterface[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
176 2
            $arr = array();
177 2
            foreach ($this->subContexts as $name => $subContext) {
178 2 View Code Duplication
                if ($subContext instanceof ContextInterface) {
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...
179 2
                    $arr = array_merge($arr, $subContext->debugPrintTree($name));
180
                } else {
181 2
                    $arr = array_merge($arr, array($name => get_class($subContext)));
182
                }
183
            }
184 2
            $result[$ownName.'__children'] = $arr;
185
        }
186
187 2
        return $result;
188
    }
189
190
    /**
191
     * @return string
192
     */
193 1
    public function __toString()
194
    {
195 1
        return json_encode($this->debugPrintTree(), JSON_PRETTY_PRINT);
196
    }
197
198
    /**
199
     * @param string $path
200
     *
201
     * @return ContextInterface
202
     */
203 3
    public function getPath($path)
204
    {
205 3
        if (is_string($path)) {
206 3
            $path = explode('/', $path);
207 3
        } elseif (false === is_array($path)) {
208
            throw new \InvalidArgumentException('Expected string or array');
209
        }
210
211 3
        $name = array_shift($path);
212 3
        $subContext = $this->getSubContext($name);
213 3
        if (null == $subContext) {
214 2
            return null;
215
        }
216
217 3
        if (empty($path)) {
218 1
            return $subContext;
219
        } else {
220 3
            return $subContext->getPath($path);
0 ignored issues
show
Documentation introduced by
$path is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
221
        }
222
    }
223
224
    /**
225
     * @param string $class
226
     *
227
     * @return ContextInterface
228
     */
229 129
    protected function createSubContext($class)
230
    {
231 129
        $result = new $class();
232
233 129
        return $result;
234
    }
235
}
236