Passed
Pull Request — master (#1045)
by Aleksei
09:49
created

Proxy::create()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 32
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 4.0312

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
c 1
b 0
f 0
dl 0
loc 32
ccs 14
cts 16
cp 0.875
rs 9.7
cc 4
nc 5
nop 2
crap 4.0312
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiral\Core\Internal;
6
7
use Spiral\Core\Internal\Proxy\ProxyClassRenderer;
8
9
/**
10
 * @internal
11
 */
12
final class Proxy
13
{
14
    /** @var array<class-string, object> */
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<class-string, object> at position 2 could not be parsed: Unknown type name 'class-string' at position 2 in array<class-string, object>.
Loading history...
15
    private static array $cache = [];
16
17
    /**
18
     * @template TClass of object
19
     * @param \ReflectionClass<TClass> $type
20
     * @return TClass
0 ignored issues
show
Bug introduced by
The type Spiral\Core\Internal\TClass was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
21
     */
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);
0 ignored issues
show
introduced by
The use of eval() is discouraged.
Loading history...
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
    }
55
}
56