Conditions | 5 |
Paths | 4 |
Total Lines | 19 |
Code Lines | 9 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
37 | public function getNewObject(string $classname): object |
||
38 | { |
||
39 | if (!class_exists($classname)) { |
||
40 | throw new InstantiatorException("Class '$classname' does not exist"); |
||
41 | } |
||
42 | |||
43 | $reflector = new \ReflectionClass($classname); |
||
44 | |||
45 | if (!$reflector->isInstantiable()) { |
||
46 | throw new InstantiatorException("Unable to instantiate non-instantiable class '$classname'"); |
||
47 | } |
||
48 | |||
49 | $constructor = $reflector->getConstructor(); |
||
50 | |||
51 | if ($constructor && $constructor->getNumberOfRequiredParameters()) { |
||
52 | throw new InstantiatorException("Unable to instantiate '$classname' with no parameters"); |
||
53 | } |
||
54 | |||
55 | return new $classname(); |
||
56 | } |
||
67 |