Completed
Push — internal ( 56c183...25b94e )
by Akihito
05:49 queued 03:41
created

Argument::__construct()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 18
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 3

Importance

Changes 4
Bugs 1 Features 0
Metric Value
c 4
b 1
f 0
dl 0
loc 18
ccs 13
cts 13
cp 1
rs 9.4285
cc 3
eloc 14
nc 4
nop 2
crap 3
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 60
    public function __construct(\ReflectionParameter $parameter, $name)
37
    {
38 60
        $interface = $this->getTypeHint($parameter);
39 60
        $interface = ($interface === 'array') ? '' : $interface; // hhvm
40 60
        $this->isDefaultAvailable = $parameter->isDefaultValueAvailable();
41 60
        if ($this->isDefaultAvailable) {
42 11
            $this->default = $parameter->getDefaultValue();
43
        }
44 60
        $this->index = $interface . '-' . $name;
45 60
        $this->reflection = $parameter;
46 60
        $this->meta = sprintf(
47 60
            "dependency '%s' with name '%s' used in %s:%d",
48
            $interface,
49
            $name,
50 60
            $this->reflection->getDeclaringFunction()->getFileName(),
51 60
            $this->reflection->getDeclaringFunction()->getStartLine()
52
        );
53 60
    }
54
55
    /**
56
     * Return reflection
57
     *
58
     * @return \ReflectionParameter
59
     */
60 33
    public function get()
61
    {
62 33
        return $this->reflection;
63
    }
64
65
    /**
66
     * @param \ReflectionParameter $parameter
67
     *
68
     * @return string
69
     */
70 60
    private function getTypeHint(\ReflectionParameter $parameter)
71
    {
72 60
        if (defined('HHVM_VERSION')) {
73
            /* @noinspection PhpUndefinedFieldInspection */
74
            return $parameter->info['type_hint']; // @codeCoverageIgnore
75
        }
76 60
        $typHint = $parameter->getClass();
77
78 60
        return $typHint instanceof \ReflectionClass ? $typHint->name : '';
79
    }
80
81
    /**
82
     * @return string
83
     */
84 35
    public function __toString()
85
    {
86 35
        return $this->index;
87
    }
88
89
    /**
90
     * @return bool
91
     */
92 12
    public function isDefaultAvailable()
93
    {
94 12
        return $this->isDefaultAvailable;
95
    }
96
97
    /**
98
     * @return mixed
99
     */
100 6
    public function getDefaultValue()
101
    {
102 6
        return $this->default;
103
    }
104
105 7
    public function getMeta()
106
    {
107 7
        return $this->meta;
108
    }
109
}
110