formatResolutionChain()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 4
c 0
b 0
f 0
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 10
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gacela\Container\Exception;
6
7
use InvalidArgumentException;
8
use Psr\Container\ContainerExceptionInterface;
9
10
final class DependencyInvalidArgumentException extends InvalidArgumentException implements ContainerExceptionInterface
11
{
12
    /**
13
     * @param list<string> $resolutionChain
14
     */
15 2
    public static function noParameterTypeFor(string $parameter, array $resolutionChain = []): self
16
    {
17 2
        $chainInfo = self::formatResolutionChain($resolutionChain);
18
19 2
        $message = <<<TXT
20 2
No type hint found for parameter '\${$parameter}'.{$chainInfo}
21
Type hints are required for dependency injection to work properly.
22
23
Add a type hint to the parameter, for example:
24 2
  public function __construct(YourClass \${$parameter}) { ... }
25 2
TXT;
26 2
        return new self($message);
27
    }
28
29
    /**
30
     * @param list<string> $resolutionChain
31
     */
32 3
    public static function unableToResolve(string $parameter, string $className, array $resolutionChain = []): self
33
    {
34 3
        $chainInfo = self::formatResolutionChain($resolutionChain);
35
36 3
        $message = <<<TXT
37 3
Unable to resolve parameter of type '{$parameter}' in '{$className}'.{$chainInfo}
38
Scalar types (string, int, float, bool, array) cannot be auto-resolved.
39
40
Provide a default value for the parameter:
41 3
  public function __construct({$parameter} \$param = 'default') { ... }
42 3
TXT;
43 3
        return new self($message);
44
    }
45
46
    /**
47
     * @param list<string> $chain
48
     */
49 5
    private static function formatResolutionChain(array $chain): string
50
    {
51 5
        if (empty($chain)) {
52 4
            return '';
53
        }
54
55 1
        $formatted = implode(' -> ', $chain);
56 1
        return "\nResolution chain: {$formatted}";
57
    }
58
}
59