AnnotatedClass   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Importance

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

3 Methods

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