1 | <?php |
||
43 | class SimpleContainer extends Container implements IContainer { |
||
44 | |||
45 | |||
46 | /** |
||
47 | * @param ReflectionClass $class the class to instantiate |
||
48 | * @return \stdClass the created class |
||
49 | * @suppress PhanUndeclaredClassInstanceof |
||
50 | */ |
||
51 | private function buildClass(ReflectionClass $class) { |
||
52 | $constructor = $class->getConstructor(); |
||
53 | if ($constructor === null) { |
||
54 | return $class->newInstance(); |
||
55 | } else { |
||
56 | $parameters = []; |
||
57 | foreach ($constructor->getParameters() as $parameter) { |
||
58 | $parameterClass = $parameter->getClass(); |
||
59 | |||
60 | // try to find out if it is a class or a simple parameter |
||
61 | if ($parameterClass === null) { |
||
62 | $resolveName = $parameter->getName(); |
||
63 | } else { |
||
64 | $resolveName = $parameterClass->name; |
||
65 | } |
||
66 | |||
67 | try { |
||
68 | $parameters[] = $this->query($resolveName); |
||
69 | } catch (\Exception $e) { |
||
70 | if (class_exists('PHPUnit_Framework_AssertionFailedError', false) && |
||
71 | $e instanceof \PHPUnit_Framework_AssertionFailedError) { |
||
|
|||
72 | // Easier debugging of "Your test case is not allowed to access the database." |
||
73 | throw $e; |
||
74 | } |
||
75 | |||
76 | // Service not found, use the default value when available |
||
77 | if ($parameter->isDefaultValueAvailable()) { |
||
78 | $parameters[] = $parameter->getDefaultValue(); |
||
79 | } else if ($parameterClass !== null) { |
||
80 | $resolveName = $parameter->getName(); |
||
81 | $parameters[] = $this->query($resolveName); |
||
82 | } else { |
||
83 | throw $e; |
||
84 | } |
||
85 | } |
||
86 | } |
||
87 | return $class->newInstanceArgs($parameters); |
||
88 | } |
||
89 | } |
||
90 | |||
91 | |||
92 | /** |
||
93 | * If a parameter is not registered in the container try to instantiate it |
||
94 | * by using reflection to find out how to build the class |
||
95 | * @param string $name the class name to resolve |
||
96 | * @return \stdClass |
||
97 | * @throws QueryException if the class could not be found or instantiated |
||
98 | */ |
||
99 | public function resolve($name) { |
||
113 | |||
114 | |||
115 | /** |
||
116 | * @param string $name name of the service to query for |
||
117 | * @return mixed registered service for the given $name |
||
118 | * @throws QueryException if the query could not be resolved |
||
119 | */ |
||
120 | public function query($name) { |
||
132 | |||
133 | /** |
||
134 | * @param string $name |
||
135 | * @param mixed $value |
||
136 | */ |
||
137 | public function registerParameter($name, $value) { |
||
140 | |||
141 | /** |
||
142 | * The given closure is call the first time the given service is queried. |
||
143 | * The closure has to return the instance for the given service. |
||
144 | * Created instance will be cached in case $shared is true. |
||
145 | * |
||
146 | * @param string $name name of the service to register another backend for |
||
147 | * @param Closure $closure the closure to be called on service creation |
||
148 | * @param bool $shared |
||
149 | */ |
||
150 | public function registerService($name, Closure $closure, $shared = true) { |
||
161 | |||
162 | /** |
||
163 | * Shortcut for returning a service from a service under a different key, |
||
164 | * e.g. to tell the container to return a class when queried for an |
||
165 | * interface |
||
166 | * @param string $alias the alias that should be registered |
||
167 | * @param string $target the target that should be resolved instead |
||
168 | */ |
||
169 | public function registerAlias($alias, $target) { |
||
174 | |||
175 | /* |
||
176 | * @param string $name |
||
177 | * @return string |
||
178 | */ |
||
179 | protected function sanitizeName($name) { |
||
185 | |||
186 | } |
||
187 |
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.json
file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.json
to be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
require
orrequire-dev
section?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceof
checks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.