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