Passed
Push — php82 ( f26871 )
by Akihito
02:19
created

AnnotatedClassMethods::__construct()   A

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\Aop\ReflectionMethod;
9
use Ray\Di\Di\InjectInterface;
10
use ReflectionAttribute;
11
12
final class AnnotatedClassMethods
13
{
14
    /**
15
     * @phpstan-param ReflectionClass<object> $class
16
     */
17
    public function getConstructorName(ReflectionClass $class): Name
18
    {
19
        $constructor = $class->getConstructor();
20
        if (! $constructor) {
0 ignored issues
show
introduced by
$constructor is of type Ray\Aop\ReflectionMethod, thus it always evaluated to true.
Loading history...
21
            return new Name(Name::ANY);
22
        }
23
24
        $reflMethod = new \ReflectionMethod($class->getName(), '__construct');
25
        $name = Name::withAttributes($reflMethod);
26
        if ($name) {
27
            return $name;
28
        }
29
30
        return new Name(Name::ANY);
31
    }
32
33
    public function getSetterMethod(ReflectionMethod $method): ?SetterMethod
34
    {
35
        $inject = $method->getAnnotation(InjectInterface::class, ReflectionAttribute::IS_INSTANCEOF);
36
        if (! $inject instanceof InjectInterface) {
37
            return null;
38
        }
39
40
        $name = $this->getName($method);
41
        $setterMethod = new SetterMethod($method, $name);
42
        if ($inject->isOptional()) {
43
            $setterMethod->setOptional();
44
        }
45
46
        return $setterMethod;
47
    }
48
49
    private function getName(ReflectionMethod $method): Name
50
    {
51
        $name = Name::withAttributes($method);
52
        if ($name) {
53
            return $name;
54
        }
55
56
        return new Name(Name::ANY);
57
    }
58
}
59