AnnotatedClass   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 7
eloc 17
dl 0
loc 48
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getPostConstruct() 0 11 3
A getNewInstance() 0 15 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Ray\Di;
6
7
use Ray\Aop\ReflectionClass;
8
use Ray\Di\Di\PostConstruct;
9
use ReflectionMethod;
10
11
final class AnnotatedClass
12
{
13
    /** @var AnnotatedClassMethods */
14
    private $injectionMethod;
15
16
    public function __construct()
17
    {
18
        $this->injectionMethod = new AnnotatedClassMethods();
19
    }
20
21
    /**
22
     * Return factory instance
23
     *
24
     * @phpstan-param ReflectionClass<object> $class Target class reflection
25
     */
26
    public function getNewInstance(ReflectionClass $class): NewInstance
27
    {
28
        $setterMethods = new SetterMethods([]);
29
        $methods = $class->getMethods();
30
        foreach ($methods as $method) {
31
            if ($method->name === '__construct') {
32
                continue;
33
            }
34
35
            $setterMethods->add($this->injectionMethod->getSetterMethod($method));
36
        }
37
38
        $name = $this->injectionMethod->getConstructorName($class);
39
40
        return new NewInstance($class, $setterMethods, $name);
41
    }
42
43
    /**
44
     * Return @-PostConstruct method reflection
45
     *
46
     * @phpstan-param ReflectionClass<object> $class
47
     */
48
    public function getPostConstruct(ReflectionClass $class): ?ReflectionMethod
49
    {
50
        $methods = $class->getMethods();
51
        foreach ($methods as $method) {
52
            $annotation = $method->getAnnotation(PostConstruct::class);
53
            if ($annotation instanceof PostConstruct) {
54
                return $method;
55
            }
56
        }
57
58
        return null;
59
    }
60
}
61