Passed
Push — master ( bbff43...c1ff08 )
by Aleksei
11:50
created

Resolver::resolve()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 32
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 6.6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 18
dl 0
loc 32
ccs 12
cts 20
cp 0.6
rs 9.3554
c 1
b 0
f 0
cc 5
nc 3
nop 3
crap 6.6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiral\Core\Internal\Proxy;
6
7
use Psr\Container\ContainerInterface;
8
use Spiral\Core\Container;
9
use Spiral\Core\ContainerScope;
10
use Spiral\Core\Exception\Container\ContainerException;
11
use Spiral\Core\Exception\Container\RecursiveProxyException;
12
use Spiral\Core\Internal\Introspector;
13
use Spiral\Core\Internal\Proxy;
14
15
/**
16
 * @internal
17
 */
18
final class Resolver
19
{
20 460
    public static function resolve(
21
        string $alias,
22
        \Stringable|string|null $context = null,
23
        ?ContainerInterface $c = null,
24
    ): object {
25 460
        $c ??= ContainerScope::getContainer() ?? throw new ContainerException('Proxy is out of scope.');
26
27
        try {
28
            /** @psalm-suppress TooManyArguments */
29 457
            $result = $c->get($alias, $context) ?? throw new ContainerException(
0 ignored issues
show
Unused Code introduced by
The call to Psr\Container\ContainerInterface::get() has too many arguments starting with $context. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

29
            $result = $c->/** @scrutinizer ignore-call */ get($alias, $context) ?? throw new ContainerException(

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
30 457
                'Resolved `null` from the container.',
31 457
            );
32
        } catch (\Throwable $e) {
33
            $scope = self::getScope($c);
34
            throw new ContainerException(
35
                $scope === null
36
                    ? "Unable to resolve `{$alias}` in a Proxy."
37
                    : "Unable to resolve `{$alias}` in a Proxy in `{$scope}` scope.",
38
                previous: $e,
39
            );
40
        }
41
42 457
        if (Proxy::isProxy($result)) {
43 1
            $scope = self::getScope($c);
44 1
            throw new RecursiveProxyException(
45 1
                $scope === null
46
                    ? "Recursive proxy detected for `{$alias}`."
47 1
                    : "Recursive proxy detected for `{$alias}` in `{$scope}` scope.",
48 1
            );
49
        }
50
51 456
        return $result;
52
    }
53
54
    /**
55
     * @return non-empty-string|null
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-string|null at position 0 could not be parsed: Unknown type name 'non-empty-string' at position 0 in non-empty-string|null.
Loading history...
56
     */
57 1
    private static function getScope(ContainerInterface $c): ?string
58
    {
59 1
        if (!$c instanceof Container) {
60
            if (!Proxy::isProxy($c)) {
61
                return null;
62
            }
63
64
            $c = null;
65
        }
66
67 1
        return \implode('.', \array_reverse(\array_map(
68 1
            static fn (?string $name): string => $name ?? 'null',
69 1
            Introspector::scopeNames($c),
70 1
        )));
71
    }
72
}
73