Passed
Push — main ( 25a875...ad40d6 )
by Chema
02:14
created

InstanceCreator::getDependencyResolver()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 9
ccs 6
cts 6
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gacela\DependencyResolver;
6
7
final class InstanceCreator
8
{
9
    private ?DependencyResolver $dependencyResolver = null;
10
11
    /** @var array<class-string,list<mixed>> */
12
    private array $cachedDependencies = [];
13
14
    /**
15
     * @param array<class-string,class-string|callable|object> $mappingInterfaces
16
     */
17 4
    public function __construct(
18
        private array $mappingInterfaces = [],
19
    ) {
20 4
    }
21
22
    /**
23
     * @param class-string $className
24
     */
25 4
    public function createByClassName(string $className): ?object
26
    {
27 4
        if (class_exists($className)) {
28 4
            if (!isset($this->cachedDependencies[$className])) {
29 4
                $this->cachedDependencies[$className] = $this
30 4
                    ->getDependencyResolver()
31 4
                    ->resolveDependencies($className);
32
            }
33
34
            /** @psalm-suppress MixedMethodCall */
35 4
            return new $className(...$this->cachedDependencies[$className]);
36
        }
37
38
        return null;
39
    }
40
41 4
    private function getDependencyResolver(): DependencyResolver
42
    {
43 4
        if ($this->dependencyResolver === null) {
44 4
            $this->dependencyResolver = new DependencyResolver(
45 4
                $this->mappingInterfaces,
46 4
            );
47
        }
48
49 4
        return $this->dependencyResolver;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->dependencyResolver could return the type null which is incompatible with the type-hinted return Gacela\DependencyResolver\DependencyResolver. Consider adding an additional type-check to rule them out.
Loading history...
50
    }
51
}
52