Passed
Push — master ( 37e009...cfbad5 )
by Aleksei
21:07 queued 10:08
created

Resolver::getScope()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
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\ContainerScope;
9
use Spiral\Core\Exception\Container\ContainerException;
10
use Spiral\Core\Exception\Container\RecursiveProxyException;
11
use Spiral\Core\Internal\Introspector;
12
use Spiral\Core\Internal\Proxy;
13
14
/**
15
 * @internal
16
 */
17
final class Resolver
18
{
19 460
    public static function resolve(
20
        string $alias,
21
        \Stringable|string|null $context = null,
22
        ?ContainerInterface $c = null,
23
    ): object {
24 460
        $c ??= ContainerScope::getContainer() ?? throw new ContainerException('Proxy is out of scope.');
25
26
        try {
27
            /** @psalm-suppress TooManyArguments */
28 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

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