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 | namespace Weew\Container; |
||
4 | |||
5 | use Weew\Container\Definitions\ClassDefinition; |
||
6 | use Weew\Container\Definitions\InterfaceDefinition; |
||
7 | use Weew\Container\Definitions\WildcardDefinition; |
||
8 | use Weew\Container\Exceptions\ImplementationNotFoundException; |
||
9 | use Weew\Container\Exceptions\TypeMismatchException; |
||
10 | use Weew\Container\Exceptions\ValueNotFoundException; |
||
11 | |||
12 | class Resolver { |
||
13 | /** |
||
14 | * @var IContainer |
||
15 | */ |
||
16 | protected $container; |
||
17 | |||
18 | /** |
||
19 | * @var Reflector |
||
20 | */ |
||
21 | protected $reflector; |
||
22 | |||
23 | /** |
||
24 | * @var bool |
||
25 | */ |
||
26 | protected $strictMode = true; |
||
27 | |||
28 | /** |
||
29 | * @param IContainer $container |
||
30 | * @param Reflector $reflector |
||
31 | */ |
||
32 | public function __construct( |
||
33 | IContainer $container, |
||
34 | Reflector $reflector |
||
35 | ) { |
||
36 | $this->container = $container; |
||
37 | $this->reflector = $reflector; |
||
38 | } |
||
39 | |||
40 | /** |
||
41 | * @param string $id |
||
42 | * @param array $args |
||
43 | * |
||
44 | * @return mixed |
||
45 | * @throws ImplementationNotFoundException |
||
46 | * @throws TypeMismatchException |
||
47 | * @throws ValueNotFoundException |
||
48 | */ |
||
49 | public function resolveWithoutDefinition($id, array $args = []) { |
||
50 | if (class_exists($id)) { |
||
51 | return $this->getClass(new ClassDefinition($id, null), $args); |
||
52 | } |
||
53 | |||
54 | if (interface_exists($id)) { |
||
55 | throw new ImplementationNotFoundException( |
||
56 | s('No implementation found in container for interface %s.', $id) |
||
57 | ); |
||
58 | } |
||
59 | |||
60 | throw new ValueNotFoundException( |
||
61 | s('No value found in container for id %s.', $id) |
||
62 | ); |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * @param IDefinition $definition |
||
67 | * @param $id |
||
68 | * @param array $args |
||
69 | * |
||
70 | * @return mixed |
||
71 | * @throws TypeMismatchException |
||
72 | */ |
||
73 | public function resolveDefinition(IDefinition $definition, $id, array $args = []) { |
||
74 | if ($definition instanceof WildcardDefinition) { |
||
75 | $value = $this->getWildcard($definition, $id, $args); |
||
76 | } else if ($definition instanceof InterfaceDefinition) { |
||
77 | $value = $this->getInterface($definition, $args); |
||
78 | } else if ($definition instanceof ClassDefinition) { |
||
79 | $value = $this->getClass($definition, $args); |
||
80 | } else { |
||
81 | $value = $definition->getValue(); |
||
82 | } |
||
83 | |||
84 | return $value; |
||
85 | } |
||
86 | |||
87 | /** |
||
88 | * @param ClassDefinition $definition |
||
89 | * @param array $args |
||
90 | * |
||
91 | * @return mixed |
||
92 | * @throws TypeMismatchException |
||
93 | */ |
||
94 | public function getClass(ClassDefinition $definition, array $args = []) { |
||
95 | $abstract = $definition->getValue(); |
||
96 | $class = $definition->getId(); |
||
97 | |||
98 | if (is_callable($abstract)) { |
||
99 | $instance = $this->container->call($abstract, $args); |
||
100 | } else if (is_object($abstract)) { |
||
101 | $instance = $abstract; |
||
102 | } else { |
||
103 | if ($abstract === null) { |
||
104 | $abstract = $class; |
||
105 | } |
||
106 | |||
107 | $instance = $this->reflector |
||
108 | ->resolveClass($this->container, $abstract, $args); |
||
109 | } |
||
110 | |||
111 | $this->matchClassType($class, $instance); |
||
112 | |||
113 | return $instance; |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * @param InterfaceDefinition $definition |
||
118 | * @param array $args |
||
119 | * |
||
120 | * @return mixed |
||
121 | * @throws TypeMismatchException |
||
122 | */ |
||
123 | View Code Duplication | public function getInterface(InterfaceDefinition $definition, array $args = []) { |
|
0 ignored issues
–
show
|
|||
124 | $abstract = $definition->getValue(); |
||
125 | $interface = $definition->getId(); |
||
126 | $instance = $this->resolveAbstract($abstract, $args); |
||
127 | $this->matchClassType($interface, $instance); |
||
128 | |||
129 | return $instance; |
||
130 | } |
||
131 | |||
132 | /** |
||
133 | * @param IDefinition $definition |
||
134 | * @param $id |
||
135 | * @param $args |
||
136 | * |
||
137 | * @return mixed|null |
||
138 | * @throws TypeMismatchException |
||
139 | */ |
||
140 | View Code Duplication | public function getWildcard(IDefinition $definition, $id, $args) { |
|
0 ignored issues
–
show
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. ![]() |
|||
141 | $abstract = $definition->getValue(); |
||
142 | $args['abstract'] = $id; |
||
143 | $instance = $this->resolveAbstract($abstract, $args); |
||
144 | $this->matchClassType($id, $instance); |
||
145 | |||
146 | return $instance; |
||
147 | } |
||
148 | |||
149 | /** |
||
150 | * @param bool $strictMode |
||
151 | */ |
||
152 | public function setStrictMode($strictMode) { |
||
153 | $this->strictMode = $strictMode; |
||
154 | } |
||
155 | |||
156 | /** |
||
157 | * @return bool |
||
158 | */ |
||
159 | public function isInStrictMode() { |
||
160 | return $this->strictMode; |
||
161 | } |
||
162 | |||
163 | /** |
||
164 | * @param $abstract |
||
165 | * @param array $args |
||
166 | * |
||
167 | * @return mixed |
||
168 | */ |
||
169 | protected function resolveAbstract($abstract, array $args) { |
||
170 | if (is_callable($abstract)) { |
||
171 | return $this->container->call($abstract, $args); |
||
172 | } else if (is_object($abstract)) { |
||
173 | return $abstract; |
||
174 | } else if (class_exists($abstract)) { |
||
175 | return $this->getClass(new ClassDefinition($abstract, null), $args); |
||
176 | } |
||
177 | |||
178 | return null; |
||
179 | } |
||
180 | |||
181 | /** |
||
182 | * @param $class |
||
183 | * @param $instance |
||
184 | * |
||
185 | * @throws TypeMismatchException |
||
186 | */ |
||
187 | protected function matchClassType($class, $instance) { |
||
188 | if ( ! $this->isInStrictMode()) { |
||
189 | return; |
||
190 | } |
||
191 | |||
192 | if ( ! $instance instanceof $class) { |
||
193 | throw new TypeMismatchException( |
||
194 | s( |
||
195 | 'Container expects an implementation of type %s, %s given.', |
||
196 | $class, get_type($instance) |
||
197 | ) |
||
198 | ); |
||
199 | } |
||
200 | } |
||
201 | } |
||
202 |
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.