AnnotatedClass::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
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