Completed
Push — master ( 692093...daa27a )
by Vitaly
02:57
created

AnnotationMethodResolver::resolve()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 9
ccs 4
cts 4
cp 1
rs 9.6666
cc 2
eloc 4
nc 2
nop 2
crap 2
1
<?php
2
/**
3
 * Created by Vitaly Iegorov <[email protected]>.
4
 * on 07.08.16 at 13:04
5
 */
6
namespace samsonframework\container\resolver;
7
8
use samsonframework\container\annotation\MethodInterface;
9
use samsonframework\container\annotation\ParameterInterface;
10
use samsonframework\container\metadata\ClassMetadata;
11
use samsonframework\container\metadata\MethodMetadata;
12
use samsonframework\container\metadata\ParameterMetadata;
13
14
/**
15
 * Class method annotation resolver.
16
 */
17
class AnnotationMethodResolver extends AbstractAnnotationResolver implements AnnotationResolverInterface
18
{
19
    /**
20
     * {@inheritDoc}
21
     */
22 9
    public function resolve(\ReflectionClass $classReflection, ClassMetadata $classMetadata)
23
    {
24
        /** @var \ReflectionMethod $method */
25 9
        foreach ($classReflection->getMethods() as $method) {
26 8
            $this->resolveMethodAnnotations($method, $classMetadata);
27
        }
28
29 1
        return $classMetadata;
30
    }
31
32
    /**
33
     * Resolve class method annotations.
34
     *
35
     * @param \ReflectionMethod $method
36
     * @param ClassMetadata     $classMetadata
37
     */
38 8
    protected function resolveMethodAnnotations(\ReflectionMethod $method, ClassMetadata $classMetadata)
39
    {
40
        // Create method metadata instance
41 8
        $methodMetadata = new MethodMetadata($classMetadata);
42 8
        $methodMetadata->name = $method->name;
43 8
        $methodMetadata->modifiers = $method->getModifiers();
44 8
        $methodMetadata->isPublic = $method->isPublic();
45
46
        /** @var \ReflectionParameter $parameter */
47 8
        $parameterMetadata = new ParameterMetadata($classMetadata, $methodMetadata);
48 8
        foreach ($method->getParameters() as $parameter) {
49 8
            $parameterMetadata = clone $parameterMetadata;
50 8
            $parameterMetadata->name = $parameter->name;
51 8
            $parameterMetadata->typeHint = (string)$parameter->getType();
52 8
            $methodMetadata->parametersMetadata[$parameterMetadata->name] = $parameterMetadata;
53
        }
54
55
        /** @var MethodInterface $annotation */
56 8
        foreach ($this->reader->getMethodAnnotations($method) as $annotation) {
57
            if ($annotation instanceof MethodInterface) {
58
                $annotation->toMethodMetadata($methodMetadata);
59
            }
60
        }
61
62
        $classMetadata->methodsMetadata[$methodMetadata->name] = $methodMetadata;
63
    }
64
}
65