DependencyInvalidArgumentException   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
dl 0
loc 47
ccs 4
cts 4
cp 1
rs 10
c 1
b 0
f 0
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A formatResolutionChain() 0 8 2
A noParameterTypeFor() 0 12 1
A unableToResolve() 0 12 1
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 2
    /**
13
     * @param list<string> $resolutionChain
14 2
     */
15
    public static function noParameterTypeFor(string $parameter, array $resolutionChain = []): self
16
    {
17 2
        $chainInfo = self::formatResolutionChain($resolutionChain);
18
19 2
        $message = <<<TXT
20
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
  public function __construct(YourClass \${$parameter}) { ... }
25
TXT;
26
        return new self($message);
27
    }
28
29
    /**
30
     * @param list<string> $resolutionChain
31
     */
32
    public static function unableToResolve(string $parameter, string $className, array $resolutionChain = []): self
33
    {
34
        $chainInfo = self::formatResolutionChain($resolutionChain);
35
36
        $message = <<<TXT
37
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
  public function __construct({$parameter} \$param = 'default') { ... }
42
TXT;
43
        return new self($message);
44
    }
45
46
    /**
47
     * @param list<string> $chain
48
     */
49
    private static function formatResolutionChain(array $chain): string
50
    {
51
        if (empty($chain)) {
52
            return '';
53
        }
54
55
        $formatted = implode(' -> ', $chain);
56
        return "\nResolution chain: {$formatted}";
57
    }
58
}
59