Passed
Push — master ( 621a9a...2a422d )
by Gerrit
05:17
created

ObjectInstanciationArgument   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 41
ccs 0
cts 13
cp 0
rs 10
c 0
b 0
f 0
wmc 2
lcom 1
cbo 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A resolve() 0 19 1
1
<?php
2
/**
3
 * Copyright (C) 2019 Gerrit Addiks.
4
 * This package (including this file) was released under the terms of the GPL-3.0.
5
 * You should have received a copy of the GNU General Public License along with this program.
6
 * If not, see <http://www.gnu.org/licenses/> or send me a mail so i can send you a copy.
7
 *
8
 * @license GPL-3.0
9
 *
10
 * @author Gerrit Addiks <[email protected]>
11
 */
12
13
namespace Addiks\SymfonyGenerics\Arguments;
14
15
use Addiks\SymfonyGenerics\Arguments\Argument;
16
use Webmozart\Assert\Assert;
17
use ReflectionClass;
18
19
final class ObjectInstanciationArgument implements Argument
20
{
21
22
    /** @var Argument */
23
    private $objectClass;
24
25
    /** @var array<Argument> */
26
    private $arguments;
27
28
    /** @param array<Argument> $arguments */
29
    public function __construct(
30
        Argument $objectClass,
31
        array $arguments = array()
32
    ) {
33
        $this->objectClass = $objectClass;
34
        $this->arguments = array_map(function (Argument $argument): Argument {
35
            return $argument;
36
        }, $arguments);
37
    }
38
39
    public function resolve()
40
    {
41
        /** @var string $objectClass */
42
        $objectClass = (string)$this->objectClass->resolve();
43
44
        Assert::classExists($objectClass);
45
46
        $reflection = new ReflectionClass($objectClass);
47
48
        /** @var array<mixed> $arguments */
49
        $arguments = array_map(function (Argument $argument) {
50
            return $argument->resolve();
51
        }, $this->arguments);
52
53
        /** @var object $object */
54
        $object = $reflection->newInstanceArgs($arguments);
55
56
        return $object;
57
    }
58
59
}
60