| Total Complexity | 8 |
| Total Lines | 37 |
| Duplicated Lines | 0 % |
| Coverage | 92.31% |
| Changes | 0 | ||
| 1 | <?php |
||
| 17 | class ObjectFactory |
||
| 18 | { |
||
| 19 | /** @var object[] */ |
||
| 20 | private $prototypes = []; |
||
| 21 | |||
| 22 | /** @var ReflectionClass[] */ |
||
| 23 | private $reflectionClasses = []; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @return object |
||
| 27 | */ |
||
| 28 | 19 | public function create(string $className) |
|
| 29 | { |
||
| 30 | 19 | if (! isset($this->prototypes[$className])) { |
|
| 31 | 19 | if ($this->isReflectionMethodAvailable()) { |
|
| 32 | 19 | $this->prototypes[$className] = $this->getReflectionClass($className) |
|
| 33 | 19 | ->newInstanceWithoutConstructor(); |
|
| 34 | } else { |
||
| 35 | $this->prototypes[$className] = unserialize(sprintf('O:%d:"%s":0:{}', strlen($className), $className)); |
||
| 36 | } |
||
| 37 | } |
||
| 38 | |||
| 39 | 19 | return clone $this->prototypes[$className]; |
|
| 40 | } |
||
| 41 | |||
| 42 | 18 | protected function isReflectionMethodAvailable() : bool |
|
| 43 | { |
||
| 44 | 18 | return PHP_VERSION_ID === 50429 || PHP_VERSION_ID === 50513 || PHP_VERSION_ID >= 50600; |
|
| 45 | } |
||
| 46 | |||
| 47 | 19 | private function getReflectionClass(string $className) : ReflectionClass |
|
| 54 | } |
||
| 55 | } |
||
| 56 |