Completed
Push — 2.x ( b6ae1b...61ad4a )
by Akihito
9s
created

Argument   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 10
Bugs 1 Features 1
Metric Value
wmc 15
c 10
b 1
f 1
lcom 1
cbo 0
dl 0
loc 117
ccs 37
cts 37
cp 1
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 21 4
A get() 0 4 1
A getTypeHint() 0 10 3
A __toString() 0 4 1
A isDefaultAvailable() 0 4 1
A getDefaultValue() 0 4 1
A getMeta() 0 4 1
A setDefaultValue() 0 12 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 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
        $this->setDefaultValue($parameter);
46 61
        $this->index = $interface . '-' . $name;
47 61
        $this->reflection = $parameter;
48 61
        $this->meta = sprintf(
49 61
            "dependency '%s' with name '%s' used in %s:%d ($%s)",
50
            $interface,
51
            $name,
52 61
            $this->reflection->getDeclaringFunction()->getFileName(),
53 61
            $this->reflection->getDeclaringFunction()->getStartLine(),
54 61
            $parameter->getName()
55
        );
56 61
    }
57
58
    /**
59
     * Return reflection
60
     *
61
     * @return \ReflectionParameter
62
     */
63 34
    public function get()
64
    {
65 34
        return $this->reflection;
66
    }
67
68
    /**
69
     * @param \ReflectionParameter $parameter
70
     *
71
     * @return string
72
     */
73 61
    private function getTypeHint(\ReflectionParameter $parameter)
74
    {
75 61
        if (defined('HHVM_VERSION')) {
76
            /* @noinspection PhpUndefinedFieldInspection */
77
            return $parameter->info['type_hint']; // @codeCoverageIgnore
78
        }
79 61
        $typHint = $parameter->getClass();
80
81 61
        return $typHint instanceof \ReflectionClass ? $typHint->name : '';
82
    }
83
84
    /**
85
     * @return string
86
     */
87 36
    public function __toString()
88
    {
89 36
        return $this->index;
90
    }
91
92
    /**
93
     * @return bool
94
     */
95 13
    public function isDefaultAvailable()
96
    {
97 13
        return $this->isDefaultAvailable;
98
    }
99
100
    /**
101
     * @return mixed
102
     */
103 7
    public function getDefaultValue()
104
    {
105 7
        return $this->default;
106
    }
107
108 7
    public function getMeta()
109
    {
110 7
        return $this->meta;
111
    }
112
113 61
    private function setDefaultValue(\ReflectionParameter $parameter)
114
    {
115 61
        if (! $this->isDefaultAvailable) {
116 59
            return;
117
        }
118
        try {
119 12
            $this->default = $parameter->getDefaultValue();
120 1
        } catch (\ReflectionException $e) {
121
            // probably it is internal class like \PDO
122 1
            $this->default = null;
123
        }
124 12
    }
125
}
126