|
22 | 22 | * The instantiator service handles lazy instantiation of services and/or ad hoc instantiation of objects. |
23 | 23 | * |
24 | 24 | * @author Niels Keurentjes <[email protected]> |
| 25 | + * |
| 26 | + * @phpstan-type SupportedTypes AdapterInterface|AbstractColumn|DataTableTypeInterface |
25 | 27 | */ |
26 | 28 | class Instantiator |
27 | 29 | { |
28 | | - /** @var ServiceLocator[] */ |
| 30 | + /** @var ServiceLocator<SupportedTypes>[] */ |
29 | 31 | private array $locators; |
30 | 32 |
|
31 | 33 | /** |
32 | 34 | * Instantiator constructor. |
33 | 35 | * |
34 | | - * @param ServiceLocator[] $locators |
| 36 | + * @param ServiceLocator<SupportedTypes>[] $locators |
35 | 37 | */ |
36 | 38 | public function __construct(array $locators = []) |
37 | 39 | { |
@@ -61,15 +63,16 @@ public function getType(string $type): DataTableTypeInterface |
61 | 63 | private function getInstance(string $type, string $baseType) |
62 | 64 | { |
63 | 65 | if (isset($this->locators[$baseType]) && $this->locators[$baseType]->has($type)) { |
64 | | - return $this->locators[$baseType]->get($type); |
65 | | - } elseif (class_exists($type) && is_subclass_of($type, $baseType)) { |
| 66 | + $instance = $this->locators[$baseType]->get($type); |
| 67 | + } elseif (class_exists($type)) { |
66 | 68 | $instance = new $type(); |
67 | | - if (!$instance instanceof $baseType) { |
68 | | - throw new InvalidArgumentException(sprintf('Class "%s" must implement/extend %s', $type, $baseType)); |
69 | | - } |
70 | | - |
71 | | - return $instance; |
| 69 | + } else { |
| 70 | + throw new InvalidArgumentException(sprintf('Could not resolve type "%s" to a service or class, are you missing a use statement? Or is it implemented but does it not correctly derive from "%s"?', $type, $baseType)); |
72 | 71 | } |
73 | | - throw new InvalidArgumentException(sprintf('Could not resolve type "%s" to a service or class, are you missing a use statement? Or is it implemented but does it not correctly derive from "%s"?', $type, $baseType)); |
| 72 | + if (!$instance instanceof $baseType) { |
| 73 | + throw new InvalidArgumentException(sprintf('Class "%s" must implement/extend %s', $type, $baseType)); |
| 74 | + } |
| 75 | + |
| 76 | + return $instance; |
74 | 77 | } |
75 | 78 | } |
0 commit comments