Completed
Push — 2.x ( 590aac...5c2340 )
by Akihito
07:34 queued 03:49
created

Argument   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 138
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 0
dl 0
loc 138
ccs 58
cts 58
cp 1
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 20 3
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 serialize() 0 18 1
A unserialize() 0 10 1
A setDefaultValue() 0 12 3
A getType() 0 9 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 implements \Serializable
12
{
13
    /**
14
     * @var string
15
     */
16
    private $index;
17
18
    /**
19
     * @var bool
20
     */
21
    private $isDefaultAvailable;
22
23
    /**
24
     * @var mixed
25
     */
26
    private $default;
27
28
    /**
29
     * @var string
30
     */
31
    private $meta;
32
33
    /**
34
     * @var \ReflectionParameter
35
     */
36
    private $reflection;
37
38 67
    public function __construct(\ReflectionParameter $parameter, string $name)
39
    {
40 67
        $type = $this->getType($parameter);
41 67
        $isOptional = $parameter->isOptional();
42 67
        $this->isDefaultAvailable = $parameter->isDefaultValueAvailable() || $isOptional;
43 67
        if ($isOptional) {
44 12
            $this->default = null;
45
        }
46 67
        $this->setDefaultValue($parameter);
47 67
        $this->index = $type . '-' . $name;
48 67
        $this->reflection = $parameter;
49 67
        $this->meta = sprintf(
50 67
            "dependency '%s' with name '%s' used in %s:%d ($%s)",
51 67
            $type,
52 67
            $name,
53 67
            $this->reflection->getDeclaringFunction()->getFileName(),
54 67
            $this->reflection->getDeclaringFunction()->getStartLine(),
55 67
            $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 67
    }
58
59
    /**
60
     * @return string
61
     */
62 41
    public function __toString()
63
    {
64 41
        return $this->index;
65
    }
66
67
    /**
68
     * Return reflection
69
     */
70 39
    public function get() : \ReflectionParameter
71
    {
72 39
        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 4
    public function serialize() : string
97
    {
98
        /** @var \ReflectionMethod $method */
99 4
        $method = $this->reflection->getDeclaringFunction();
100
        $ref = [
101 4
            $method->class,
102 4
            $method->name,
103 4
            $this->reflection->getName()
0 ignored issues
show
Bug introduced by
Consider using $this->reflection->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
104
        ];
105
106 4
        return \serialize([
107 4
            $this->index,
108 4
            $this->isDefaultAvailable,
109 4
            $this->default,
110 4
            $this->meta,
111 4
            $ref
112
        ]);
113
    }
114
115 4
    public function unserialize($serialized)
116
    {
117 4
        list($this->index,
118 4
            $this->isDefaultAvailable,
119 4
            $this->default,
120 4
            $this->meta,
121
            $ref
122 4
        ) = unserialize($serialized);
123 4
        $this->reflection = new \ReflectionParameter([$ref[0], $ref[1]], $ref[2]);
124 4
    }
125
126 67
    private function setDefaultValue(\ReflectionParameter $parameter)
127
    {
128 67
        if (! $this->isDefaultAvailable) {
129 65
            return;
130
        }
131
        try {
132 12
            $this->default = $parameter->getDefaultValue();
133 1
        } /* @noinspection PhpRedundantCatchClauseInspection */ catch (\ReflectionException $e) {
134
            // probably it is internal class like \PDO
135 1
            $this->default = null;
136
        }
137 12
    }
138
139 67
    private function getType(\ReflectionParameter $parameter) : string
140
    {
141 67
        $type = $parameter->getType();
142 67
        if ($type instanceof \ReflectionType && \in_array((string) $type, ['bool', 'int', 'string', 'array', 'resource', 'callable'], true)) {
0 ignored issues
show
Bug introduced by
The class ReflectionType does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
143 3
            return '';
144
        }
145
146 67
        return (string) $type;
147
    }
148
}
149