Passed
Push — master ( 37709b...f45e89 )
by Aleksei
06:22 queued 17s
created

Resolver   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Test Coverage

Coverage 62.07%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
eloc 28
c 1
b 0
f 0
dl 0
loc 62
ccs 18
cts 29
cp 0.6207
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B resolve() 0 42 6
A getScope() 0 13 3
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 484
    public static function resolve(
21
        string $alias,
22
        \Stringable|string|null $context = null,
23
        ?ContainerInterface $c = null,
24
    ): object {
25 484
        $c ??= ContainerScope::getContainer() ?? throw new ContainerException('Proxy is out of scope.');
26
27
        try {
28
            /** @psalm-suppress TooManyArguments */
29 481
            $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 481
                'Resolved `null` from the container.',
31 481
            );
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
                    : \sprintf('Unable to resolve `%s` in a Proxy in `%s` scope.', $alias, \implode('.', $scope)),
38
                previous: $e,
39
            );
40
        }
41
42 481
        if (!Proxy::isProxy($result)) {
43 478
            return $result;
44
        }
45
46
        /**
47
         * If we got a Proxy again, that we should retry with the new context
48
         * to try to get the instance from the Proxy Fallback Factory.
49
         * If there is no the Proxy Fallback Factory, {@see RecursiveProxyException} will be thrown.
50
         */
51
        try {
52
            /** @psalm-suppress TooManyArguments */
53 25
            $result = $c->get($alias, new RetryContext($context));
54 5
        } catch (RecursiveProxyException $e) {
55 1
            throw new RecursiveProxyException($e->alias, $e->bindingScope, self::getScope($c));
56
        }
57
58
        // If Container returned a Proxy after the retry, then we have a recursion.
59 21
        return Proxy::isProxy($result)
60 1
            ? throw new RecursiveProxyException($alias, null, self::getScope($c))
61 20
            : $result;
62
    }
63
64
    /**
65
     * @return list<non-empty-string|null>|null
0 ignored issues
show
Bug introduced by
The type Spiral\Core\Internal\Proxy\list 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...
66
     */
67 2
    private static function getScope(ContainerInterface $c): ?array
68
    {
69 2
        if (!$c instanceof Container) {
70
            if (!Proxy::isProxy($c)) {
71
                return null;
72
            }
73
74
            $c = null;
75
        }
76
77 2
        return \array_reverse(\array_map(
78 2
            static fn (?string $name): string => $name ?? 'null',
79 2
            Introspector::scopeNames($c),
80 2
        ));
81
    }
82
}
83