Passed
Push — master ( d343d0...ce5123 )
by Mihail
05:32
created

DIException   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Test Coverage

Coverage 96.15%

Importance

Changes 5
Bugs 0 Features 1
Metric Value
wmc 7
eloc 38
c 5
b 0
f 1
dl 0
loc 74
ccs 25
cts 26
cp 0.9615
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A forInvalidParameterName() 0 3 1
A cannotInstantiate() 0 9 1
A forCircularDependency() 0 3 1
A forMissingArgument() 0 13 1
A forNonPublicMethod() 0 3 1
A forReflectionError() 0 3 1
A __construct() 0 6 1
1
<?php
2
3
/*
4
 * This file is part of the Koded package.
5
 *
6
 * (c) Mihail Binev <[email protected]>
7
 *
8
 * Please view the LICENSE distributed with this source code
9
 * for the full copyright and license information.
10
 *
11
 */
12
13
namespace Koded;
14
15
use Psr\Container\{ContainerExceptionInterface, NotFoundExceptionInterface};
16
17
class DIException extends \LogicException implements ContainerExceptionInterface
18
{
19
    public const
20
        E_CIRCULAR_DEPENDENCY = 7001,
21
        E_NON_PUBLIC_METHOD = 7002,
22
        E_CANNOT_INSTANTIATE = 7003,
23
        E_INVALID_PARAMETER_NAME = 7004,
24
        E_INSTANCE_NOT_FOUND = 7005,
25
        E_MISSING_ARGUMENT = 7006,
26
        E_REFLECTION_ERROR = 7007;
27
28
    protected array $messages = [
29
        DIException::E_CIRCULAR_DEPENDENCY => 'Circular dependency detected while creating an instance for :class',
30
        DIException::E_NON_PUBLIC_METHOD => 'Failed to create an instance, because the method ":class:::method" is not public',
31
        DIException::E_CANNOT_INSTANTIATE => 'Cannot instantiate :type :name',
32
        DIException::E_INVALID_PARAMETER_NAME => 'Provide a valid name for the global parameter: ":name"',
33
        DIException::E_INSTANCE_NOT_FOUND => 'The requested instance :id is not found in the container',
34
        DIException::E_MISSING_ARGUMENT => 'Required parameter ":name" is missing at position :position in :function()',
35
        DIException::E_REFLECTION_ERROR => ':message',
36
    ];
37
38 17
    public function __construct(int $code, array $arguments = [], \Throwable $previous = null)
39
    {
40 17
        parent::__construct(
41 17
            \strtr($this->messages[$code] ?? ':message', $arguments + [':message' => $this->message]),
42
            $code,
43
            $previous
44
        );
45 17
    }
46
47 1
    public static function forCircularDependency(string $class): static
48
    {
49 1
        return new static(static::E_CIRCULAR_DEPENDENCY, [':class' => $class]);
50
    }
51
52 1
    public static function forNonPublicMethod(string $class, string $method): static
53
    {
54 1
        return new static(static::E_NON_PUBLIC_METHOD, [':class' => $class, ':method' => $method]);
55
    }
56
57 5
    public static function cannotInstantiate(\ReflectionClass $dependency): static
58
    {
59 5
        $type = match (true) {
60 5
            $dependency->isInterface() => 'interface',
61 3
            $dependency->isAbstract() => 'abstract class',
62 1
            $dependency->isTrait() => 'trait',
63
            default => 'class',
64
        };
65 5
        return new static(static::E_CANNOT_INSTANTIATE, [':name' => $dependency->name, ':type' => $type]);
66
    }
67
68 7
    public static function forInvalidParameterName(string $name): static
69
    {
70 7
        return new static(static::E_INVALID_PARAMETER_NAME, [':name' => $name]);
71
    }
72
73 1
    public static function forMissingArgument(
74
        string $name,
75
        \ReflectionParameter $parameter,
76
        \Throwable $e = null): static
77
    {
78 1
        return new static(static::E_MISSING_ARGUMENT, [
79 1
            ':name' => $name,
80 1
            ':position' => $parameter->getPosition(),
81 1
            ':function' => \join('::', \array_filter([
82 1
                $parameter->getDeclaringClass()?->name,
83 1
                $parameter->getDeclaringFunction()?->name
84
            ]))
85
        ], $e);
86
    }
87
88 1
    public static function forReflectionError(\ReflectionException $e): static
89
    {
90 1
        return new static(static::E_REFLECTION_ERROR, [':message' => $e->getMessage()], $e);
91
    }
92
}
93
94
95
class DIInstanceNotFound extends DIException implements NotFoundExceptionInterface
96
{
97 1
    public static function for(string $id): NotFoundExceptionInterface
98
    {
99 1
        return new static(static::E_INSTANCE_NOT_FOUND, [':id' => $id]);
100
    }
101
}
102