ReflectionResolver   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 86.11%

Importance

Changes 0
Metric Value
dl 0
loc 90
ccs 31
cts 36
cp 0.8611
rs 10
c 0
b 0
f 0
wmc 12
lcom 1
cbo 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getConstructorParams() 0 23 4
A injectProperties() 0 22 4
A getInjectableProperties() 0 21 4
1
<?php
2
3
/**
4
 * This file is part of the Container package.
5
 *
6
 * Copyright (c) Miloš Đurić <[email protected]>
7
 *
8
 * For full copyright and license information, please refer to the LICENSE file,
9
 * located at the package root folder.
10
 */
11
12
namespace Laganica\Di\Resolver;
13
14
use Laganica\Di\Exception\ClassNotFoundException;
15
use PhpDocReader\AnnotationException;
16
use PhpDocReader\PhpDocReader;
17
use phpDocumentor\Reflection\DocBlockFactory;
18
use ReflectionClass;
19
use ReflectionException;
20
use ReflectionProperty;
21
22
/**
23
 * Class ReflectionResolver
24
 *
25
 * @package Laganica\Di\Resolver
26
 */
27
abstract class ReflectionResolver extends Resolver
28
{
29
    /**
30
     * @param string $class
31
     *
32
     * @throws ClassNotFoundException
33
     *
34
     * @return array
35
     */
36 14
    protected function getConstructorParams(string $class): array
37
    {
38 14
        $params = [];
39
40
        try {
41 14
            $reflection = new ReflectionClass($class);
42 3
        } catch (ReflectionException $e) {
43 3
            throw ClassNotFoundException::create($class);
44
        }
45
46 11
        $constructor = $reflection->getConstructor();
47
48 11
        if ($constructor === null) {
49 10
            return $params;
50
        }
51
52 4
        foreach ($constructor->getParameters() AS $param) {
53 4
            $dependencyId = $param->getClass()->name;
54 4
            $params[] = $this->getContainer()->get($dependencyId);
55
        }
56
57 3
        return $params;
58
    }
59
60
    /**
61
     * @param object $entry
62
     *
63
     * @throws ClassNotFoundException
64
     *
65
     * @return void
66
     */
67 1
    protected function injectProperties(object $entry): void
68
    {
69 1
        $class = get_class($entry);
70
71
        try {
72 1
            $reflectionClass = new ReflectionClass($class);
73
        } catch (ReflectionException $e) {
74
            throw ClassNotFoundException::create($class);
75
        }
76
77 1
        $phpDocReader = new PhpDocReader();
78
79 1
        foreach ($this->getInjectableProperties($reflectionClass) as $property) {
80
            try {
81 1
                $identifier = $phpDocReader->getPropertyClass($property);
82 1
                $property->setAccessible(true);
83 1
                $property->setValue($entry, $this->getContainer()->get($identifier));
84
            } catch (AnnotationException $ex) {
85
                throw new ClassNotFoundException($ex->getMessage());
86
            }
87
        }
88 1
    }
89
90
    /**
91
     * @param ReflectionClass $class
92
     *
93
     * @return ReflectionProperty[]
94
     */
95 1
    private function getInjectableProperties(ReflectionClass $class): array
96
    {
97 1
        $properties = [];
98 1
        $docBlockFactory = DocBlockFactory::createInstance();
99
100 1
        foreach ($class->getProperties() as $property) {
101 1
            if ($property->isStatic()) {
102
                continue;
103
            }
104
105 1
            $docBlock = $docBlockFactory->create($property->getDocComment());
106
107 1
            if (!$docBlock->hasTag('Inject')) {
108 1
                continue;
109
            }
110
111 1
            $properties[] = $property;
112
        }
113
114 1
        return $properties;
115
    }
116
}
117