This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
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
|
|||
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
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);
![]() |
|||
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
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 ![]() |
|||
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
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. ![]() |
|||
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
$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);
![]() |
|||
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 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
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: