|
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( |
|
|
|
|
|
|
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 |
|
|
|
|
|
|
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
|
|
|
|
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.