| Total Complexity | 9 |
| Total Lines | 54 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | <?php |
||
| 17 | final class Instantiator extends ReflectionClass implements ProvidesInstances |
||
| 18 | { |
||
| 19 | /** @throws CannotInstantiateThis */ |
||
| 20 | public function __construct(string $class) |
||
| 21 | { |
||
| 22 | try { |
||
| 23 | parent::__construct($class); |
||
| 24 | } catch (ReflectionException $exception) { |
||
| 25 | throw CannotFindTheClass::encountered($exception); |
||
| 26 | } |
||
| 27 | if ($this->isAbstract()) { |
||
| 28 | throw ClassIsAbstract::cannotInstantiate($class); |
||
| 29 | } |
||
| 30 | if ($this->isInterface()) { |
||
| 31 | throw ThatIsAnInterface::cannotInstantiate($class); |
||
| 32 | } |
||
| 33 | } |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Produces a new instantiator. |
||
| 37 | * |
||
| 38 | * @param string $class The class to create the instantiator for. |
||
| 39 | * @return ProvidesInstances The instantiator for the class. |
||
| 40 | * @throws CannotInstantiateThis |
||
| 41 | */ |
||
| 42 | public static function forThe(string $class): ProvidesInstances |
||
| 45 | } |
||
| 46 | |||
| 47 | /** @inheritdoc */ |
||
| 48 | public function instance(): object |
||
| 49 | { |
||
| 50 | try { |
||
| 51 | return $this->newInstanceWithoutConstructor(); |
||
| 52 | } catch (Throwable $exception) { |
||
| 53 | return $this->newInstanceFromDeserialization(); |
||
| 54 | } |
||
| 55 | } |
||
| 56 | |||
| 57 | /** @inheritdoc */ |
||
| 58 | public function class(): string |
||
| 61 | } |
||
| 62 | |||
| 63 | private function newInstanceFromDeserialization(): object |
||
| 71 | } |
||
| 72 | } |
||
| 73 |