Completed
Pull Request — 2.x (#162)
by Akihito
01:31
created

Argument::getTypeHint()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 5
nc 3
nop 1
crap 3
1
<?php
2
3
declare(strict_types=1);
4
/**
5
 * This file is part of the Ray.Di package.
6
 *
7
 * @license http://opensource.org/licenses/MIT MIT
8
 */
9
namespace Ray\Di;
10
11
final class Argument
12
{
13
    /**
14
     * @var string
15
     */
16
    private $index;
17
18
    /**
19
     * @var bool
20
     */
21
    private $isDefaultAvailable;
22
23
    /**
24
     * @var
25
     */
26
    private $default;
27
28
    /**
29
     * @var string
30
     */
31
    private $meta;
32
33
    /**
34
     * @var \ReflectionParameter
35
     */
36
    private $reflection;
37
38 65
    public function __construct(\ReflectionParameter $parameter, string $name)
39
    {
40 65
        $type = $parameter->getType();
41 65
        $isOptional = $parameter->isOptional();
42 65
        $this->isDefaultAvailable = $parameter->isDefaultValueAvailable() || $isOptional;
43 65
        if ($isOptional) {
44 12
            $this->default = null;
45
        }
46 65
        $this->setDefaultValue($parameter);
47 65
        $this->index = $type . '-' . $name;
48 65
        $this->reflection = $parameter;
49 65
        $this->meta = sprintf(
50 65
            "dependency '%s' with name '%s' used in %s:%d ($%s)",
51 65
            $type,
52 65
            $name,
53 65
            $this->reflection->getDeclaringFunction()->getFileName(),
54 65
            $this->reflection->getDeclaringFunction()->getStartLine(),
55 65
            $parameter->getName()
0 ignored issues
show
Bug introduced by
Consider using $parameter->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
56
        );
57 65
    }
58
59
    /**
60
     * @return string
61
     */
62 39
    public function __toString()
63
    {
64 39
        return $this->index;
65
    }
66
67
    /**
68
     * Return reflection
69
     */
70 37
    public function get() : \ReflectionParameter
71
    {
72 37
        return $this->reflection;
73
    }
74
75
    /**
76
     * @return bool
77
     */
78 13
    public function isDefaultAvailable() : bool
79
    {
80 13
        return $this->isDefaultAvailable;
81
    }
82
83
    /**
84
     * @return mixed
85
     */
86 7
    public function getDefaultValue()
87
    {
88 7
        return $this->default;
89
    }
90
91 7
    public function getMeta() : string
92
    {
93 7
        return $this->meta;
94
    }
95
96 65
    private function setDefaultValue(\ReflectionParameter $parameter)
97
    {
98 65
        if (! $this->isDefaultAvailable) {
99 63
            return;
100
        }
101
        try {
102 12
            $this->default = $parameter->getDefaultValue();
103 1
        } catch (\ReflectionException $e) {
104
            // probably it is internal class like \PDO
105 1
            $this->default = null;
106
        }
107 12
    }
108
}
109