Passed
Pull Request — master (#129)
by Sergei
02:39
created

ParameterDefinition::isOptional()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
c 0
b 0
f 0
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Factory\Definition;
6
7
use ReflectionParameter;
8
use Yiisoft\Factory\DependencyResolverInterface;
9
use Yiisoft\Factory\Exception\NotInstantiableException;
10
use Yiisoft\Factory\Exception\NotDetermineDefaultValueOfPhpInternalException;
11
12
final class ParameterDefinition implements DefinitionInterface
13
{
14
    private ReflectionParameter $parameter;
15
16 17
    public function __construct(ReflectionParameter $parameter)
17
    {
18 17
        $this->parameter = $parameter;
19 17
    }
20
21 63
    public function isVariadic(): bool
22
    {
23 63
        return $this->parameter->isVariadic();
24
    }
25
26 45
    public function isOptional(): bool
27
    {
28 45
        return $this->parameter->isOptional();
29
    }
30
31 41
    public function hasValue(): bool
32
    {
33 41
        return $this->parameter->isDefaultValueAvailable() || $this->parameter->allowsNull();
34
    }
35
36 46
    public function resolve(DependencyResolverInterface $container)
37
    {
38 46
        if ($this->parameter->isDefaultValueAvailable()) {
39 42
            return $this->parameter->getDefaultValue();
40
        }
41
42 4
        if ($this->parameter->allowsNull()) {
43 2
            return null;
44
        }
45
46 2
        if ($this->isOptional()) {
47 1
            throw new NotDetermineDefaultValueOfPhpInternalException($this->parameter);
48
        }
49
50 1
        throw new NotInstantiableException('Parameter definition does not contain a value.');
51
    }
52
}
53