Completed
Push — 2.x ( 26569f...004d76 )
by Akihito
02:57
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 3
Bugs 0 Features 1
Metric Value
wmc 15
lcom 1
cbo 0
dl 0
loc 117
ccs 37
cts 37
cp 1
rs 10
c 3
b 0
f 1

8 Methods

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