Completed
Push — internal ( 56c183 )
by Akihito
04:55
created

Argument::__construct()   B

Complexity

Conditions 6
Paths 24

Size

Total Lines 27
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 6

Importance

Changes 5
Bugs 1 Features 0
Metric Value
c 5
b 1
f 0
dl 0
loc 27
ccs 18
cts 18
cp 1
rs 8.439
cc 6
eloc 20
nc 24
nop 2
crap 6
1
<?php
2
/**
3
 * This file is part of the Ray.Di package.
4
 *
5
 * @license http://opensource.org/licenses/MIT MIT
6
 */
7
namespace Ray\Di;
8
9
final class Argument
10
{
11
    /**
12
     * @var string
13
     */
14
    private $index;
15
16
    /**
17
     * @var bool
18
     */
19
    private $isDefaultAvailable;
20
21
    /**
22
     * @var
23
     */
24
    private $default;
25
26
    /**
27
     * @var string
28
     */
29
    private $meta;
30
31
    /**
32
     * @var \ReflectionParameter
33
     */
34
    private $reflection;
35
36 61
    public function __construct(\ReflectionParameter $parameter, $name)
37
    {
38 61
        $interface = $this->getTypeHint($parameter);
39 61
        $interface = ($interface === 'array') ? '' : $interface; // hhvm
40 61
        $isOptional = $parameter->isOptional();
41 61
        $this->isDefaultAvailable = $parameter->isDefaultValueAvailable() || $isOptional;
42 61
        if ($isOptional) {
43 12
            $this->default = null;
44
        }
45 61
        if ($this->isDefaultAvailable) {
46
            try {
47 12
                $this->default = $parameter->getDefaultValue();
48 1
            } catch (\ReflectionException $e) {
49
                // probably it is internal class like \PDO
50 1
                $this->default = null;
51
            }
52
        }
53 61
        $this->index = $interface . '-' . $name;
54 61
        $this->reflection = $parameter;
55 61
        $this->meta = sprintf(
56 61
            "dependency '%s' with name '%s' used in %s:%d",
57
            $interface,
58
            $name,
59 61
            $this->reflection->getDeclaringFunction()->getFileName(),
60 61
            $this->reflection->getDeclaringFunction()->getStartLine()
61
        );
62 61
    }
63
64
    /**
65
     * Return reflection
66
     *
67
     * @return \ReflectionParameter
68
     */
69 34
    public function get()
70
    {
71 34
        return $this->reflection;
72
    }
73
74
    /**
75
     * @param \ReflectionParameter $parameter
76
     *
77
     * @return string
78
     */
79 61
    private function getTypeHint(\ReflectionParameter $parameter)
80
    {
81 61
        if (defined('HHVM_VERSION')) {
82
            /* @noinspection PhpUndefinedFieldInspection */
83
            return $parameter->info['type_hint']; // @codeCoverageIgnore
84
        }
85 61
        $typHint = $parameter->getClass();
86
87 61
        return $typHint instanceof \ReflectionClass ? $typHint->name : '';
88
    }
89
90
    /**
91
     * @return string
92
     */
93 36
    public function __toString()
94
    {
95 36
        return $this->index;
96
    }
97
98
    /**
99
     * @return bool
100
     */
101 13
    public function isDefaultAvailable()
102
    {
103 13
        return $this->isDefaultAvailable;
104
    }
105
106
    /**
107
     * @return mixed
108
     */
109 7
    public function getDefaultValue()
110
    {
111 7
        return $this->default;
112
    }
113
114 7
    public function getMeta()
115
    {
116 7
        return $this->meta;
117
    }
118
}
119