Completed
Push — master ( 10cb72...3c6184 )
by Nikola
01:39
created

ParameterAnalysis   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 97.22%

Importance

Changes 0
Metric Value
wmc 18
lcom 1
cbo 1
dl 0
loc 99
ccs 35
cts 36
cp 0.9722
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getName() 0 4 1
A getType() 0 4 1
A hasArgument() 0 4 1
A getArgument() 0 4 1
A isOptional() 0 4 1
A getDefaultValue() 0 4 1
A isNestedObject() 0 4 3
A isArray() 0 4 2
A getDeclaringClass() 0 4 1
A setParameterType() 0 9 3
A setArgument() 0 9 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cascader;
6
7
use Cascader\Exception\OptionNotSetException;
8
use ReflectionParameter;
9
use ReflectionType;
10
11
final class ParameterAnalysis
12
{
13
    /**
14
     * @var ReflectionParameter
15
     */
16
    private $parameter;
17
18
    /**
19
     * @var ReflectionType|null
20
     */
21
    private $parameterType;
22
23
    /**
24
     * @var string
25
     */
26
    private $parameterTypeName;
27
28
    /**
29
     * @var mixed
30
     */
31
    private $argument;
32
33
    /**
34
     * @var bool
35
     */
36
    private $hasArgument;
37
38 8
    public function __construct(ReflectionParameter $parameter, Options $options)
39
    {
40 8
        $this->parameter = $parameter;
41 8
        $this->setParameterType();
42 8
        $this->setArgument($options);
43 8
    }
44
45 1
    public function getName() : string
46
    {
47 1
        return $this->parameter->getName();
0 ignored issues
show
Bug introduced by
Consider using $this->parameter->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
48
    }
49
50 2
    public function getType() : string
51
    {
52 2
        return $this->parameterTypeName;
53
    }
54
55 8
    public function hasArgument() : bool
56
    {
57 8
        return $this->hasArgument;
58
    }
59
60 7
    public function getArgument()
61
    {
62 7
        return $this->argument;
63
    }
64
65 6
    public function isOptional() : bool
66
    {
67 6
        return $this->parameter->isOptional();
68
    }
69
70 5
    public function getDefaultValue()
71
    {
72 5
        return $this->parameter->getDefaultValue();
73
    }
74
75 7
    public function isNestedObject() : bool
76
    {
77 7
        return null !== $this->parameterType && !$this->parameterType->isBuiltin() && \is_array($this->argument);
78
    }
79
80 7
    public function isArray() : bool
81
    {
82 7
        return 'array' === $this->parameterTypeName && \is_array($this->argument);
83
    }
84
85 1
    public function getDeclaringClass() : string
86
    {
87 1
        return $this->parameter->getDeclaringClass()->name;
88
    }
89
90 8
    private function setParameterType()
91
    {
92 8
        if (null !== ($parameterType = $this->parameter->getType())) {
93 8
            $this->parameterType = $parameterType;
94 8
            $this->parameterTypeName = method_exists($parameterType, 'getName')
95
                ? $parameterType->getName() //PHP 7.1
96 8
                : (string) $parameterType;
97
        }
98 8
    }
99
100 8
    private function setArgument(Options $options)
101
    {
102
        try {
103 8
            $this->argument = $options->get($this->parameter->name);
104 7
            $this->hasArgument = true;
105 6
        } catch (OptionNotSetException $ex) {
106 6
            $this->hasArgument = false;
107
        }
108 8
    }
109
}
110