Conditions | 4 |
Paths | 5 |
Total Lines | 32 |
Code Lines | 17 |
Lines | 0 |
Ratio | 0 % |
Tests | 14 |
CRAP Score | 4.0312 |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
22 | 15 | public static function create(\ReflectionClass $type, \Stringable|string|null $context): object |
|
23 | { |
||
24 | 15 | $interface = $type->getName(); |
|
25 | |||
26 | 15 | if (!\array_key_exists($interface, self::$cache)) { |
|
27 | /** @var class-string<TClass> $className */ |
||
28 | 4 | $className = "{$type->getNamespaceName()}\\{$type->getShortName()} SCOPED PROXY"; |
|
29 | |||
30 | try { |
||
31 | 4 | $classString = ProxyClassRenderer::renderClass($type, $className); |
|
32 | |||
33 | 4 | eval($classString); |
|
34 | } catch (\Throwable $e) { |
||
35 | throw new \Error("Unable to create proxy for `{$interface}`: {$e->getMessage()}", 0, $e); |
||
36 | } |
||
37 | |||
38 | 4 | $instance = new $className(); |
|
39 | 4 | (static fn() => $instance::$__container_proxy_alias = $interface)->bindTo(null, $instance::class)(); |
|
40 | |||
41 | // Store in cache without context |
||
42 | 4 | self::$cache[$interface] = $instance; |
|
43 | } else { |
||
44 | /** @var TClass $instance */ |
||
45 | 11 | $instance = self::$cache[$interface]; |
|
46 | } |
||
47 | |||
48 | 15 | if ($context !== null) { |
|
49 | 15 | $instance = clone $instance; |
|
50 | 15 | (static fn() => $instance->__container_proxy_context = $context)->bindTo(null, $instance::class)(); |
|
51 | } |
||
52 | |||
53 | 15 | return $instance; |
|
54 | } |
||
56 |