Test Failed
Push — master ( 7e410e...bf8e81 )
by Alexey
04:20
created

UnresolvableDependencyException::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 3
crap 2
1
<?php declare(strict_types = 1);
2
3
namespace Venta\Container\Exception;
4
5
use Exception;
6
use ReflectionFunctionAbstract;
7
use ReflectionMethod;
8
use ReflectionParameter;
9
10
/**
11
 * Class ResolveException
12
 *
13
 * @package Venta\Container\Exception
14
 */
15
class UnresolvableDependencyException extends ContainerException
16
{
17
18
    /**
19
     * @inheritDoc
20
     * @param ArgumentResolverException $previous
21
     */
22
    public function __construct($entryId, array $referenceChain, ArgumentResolverException $previous)
23
    {
24
        parent::__construct($entryId, $referenceChain, $previous);
25
    }
26
27
    /**
28
     * @inheritDoc
29
     * @param ArgumentResolverException $argumentResolveException
30
     */
31
    protected function createMessage(Exception $argumentResolveException = null): string
32
    {
33
        return sprintf(
34
            'Unable to resolve parameter "%s" value for "%s" %s, path: "%s".',
35
            $this->formatParameter($argumentResolveException->getParameter()),
36
            $this->formatFunction($argumentResolveException->getFunction()),
37
            $argumentResolveException->getFunction() instanceof ReflectionMethod ? 'method' : 'function',
38
            implode(' -> ', $this->referenceChain)
39
        );
40
    }
41
42
    /**
43
     * Formats function declaration depending on method/function type.
44
     *
45
     * @param ReflectionFunctionAbstract $function
46
     * @return string
47
     */
48
    private function formatFunction(ReflectionFunctionAbstract $function)
49
    {
50
        return $function instanceof ReflectionMethod ?
51
            $function->getDeclaringClass()->getName() . '::' . $function->getName() :
52
            $function->getName();
53
    }
54
55
    /**
56
     * Formats parameter depending on type
57
     *
58
     * @param ReflectionParameter $parameter
59
     * @return string
60
     */
61
    private function formatParameter(ReflectionParameter $parameter): string
62
    {
63
        return $parameter->hasType() ?
64
            sprintf('%s $%s', $parameter->getType(), $parameter->getName()) :
65
            sprintf('$%s', $parameter->getName());
66
    }
67
}