Passed
Pull Request — master (#78)
by Dmitriy
02:32
created

ParameterDefinition::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Factory\Definition;
6
7
use Psr\Container\ContainerInterface;
8
use ReflectionParameter;
9
use Yiisoft\Factory\FactoryInterface;
10
use function is_object;
11
12
class ParameterDefinition implements DefinitionInterface
13
{
14
    private ReflectionParameter $parameter;
15
    private bool $hasValue = false;
16
17
    /**
18
     * @var mixed
19
     */
20
    private $value = null;
21
    private ?string $type;
22
23 6
    public function __construct(ReflectionParameter $parameter, string $type = null)
24
    {
25 6
        $this->parameter = $parameter;
26 6
        $this->type = $type;
27 6
    }
28
29
    /**
30
     * @param mixed $value
31
     */
32 6
    public function setValue($value): void
33
    {
34 6
        $this->hasValue = true;
35 6
        $this->value = $value;
36 6
    }
37
38 32
    public function getParameter(): ReflectionParameter
39
    {
40 32
        return $this->parameter;
41
    }
42
43 28
    public function hasValue(): bool
44
    {
45 28
        return $this->hasValue;
46
    }
47
48
    public function getType(): ?string
49
    {
50
        return $this->type;
51
    }
52
53 28
    public function resolve(ContainerInterface $container)
54
    {
55 28
        if ($container instanceof FactoryInterface && is_object($this->value)) {
56
            return clone $this->value;
57
        }
58
59 28
        return $this->value;
60
    }
61
}
62