Completed
Push — master ( 6f1a19...e34473 )
by Vitaly
02:18
created

resolveMethodAnnotations()   B

Complexity

Conditions 5
Paths 10

Size

Total Lines 28
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 28
ccs 0
cts 9
cp 0
rs 8.439
cc 5
eloc 16
nc 10
nop 2
crap 30
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
    public function resolve(\ReflectionClass $classData, ClassMetadata $classMetadata)
23
    {
24
        /** @var \ReflectionMethod $method */
25
        foreach ($classData->getMethods() as $method) {
26
            $this->resolveMethodAnnotations($method, $classMetadata);
27
        }
28
29
        return $this->classMetadata;
30
    }
31
32
    /**
33
     * Resolve class method annotations.
34
     *
35
     * @param \ReflectionMethod $method
36
     * @param ClassMetadata     $classMetadata
37
     */
38
    protected function resolveMethodAnnotations(\ReflectionMethod $method, ClassMetadata $classMetadata)
39
    {
40
        // Create method metadata instance
41
        $methodMetadata = new MethodMetadata($classMetadata);
42
        $methodMetadata->name = $method->getName();
0 ignored issues
show
Bug introduced by
Consider using $method->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
43
        $methodMetadata->modifiers = $method->getModifiers();
44
45
        /** @var \ReflectionParameter $parameter */
46
        $parameterMetadata = new ParameterMetadata($classMetadata, $methodMetadata);
47
        foreach ($method->getParameters() as $parameter) {
48
            $parameterMetadata = clone $parameterMetadata;
49
            $parameterMetadata->name = $parameter->getName();
0 ignored issues
show
Bug introduced by
Consider using $parameter->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
50
            $parameterMetadata->typeHint = $parameter->getType()->__toString();
51
            $methodMetadata->parametersMetadata[$parameterMetadata->name] = $parameterMetadata;
52
        }
53
54
        /** @var MethodInterface|ParameterInterface $annotation */
55
        foreach ($this->reader->getMethodAnnotations($method) as $annotation) {
56
            if ($annotation instanceof MethodInterface) {
57
                $annotation->toMethodMetadata($methodMetadata);
58
            }
59
            if ($annotation instanceof ParameterInterface) {
60
                $annotation->toParameterMetadata(new ParameterMetadata($methodMetadata));
0 ignored issues
show
Bug introduced by
The call to ParameterMetadata::__construct() misses a required argument $methodMetadata.

This check looks for function calls that miss required arguments.

Loading history...
Documentation introduced by
$methodMetadata is of type object<samsonframework\c...etadata\MethodMetadata>, but the function expects a object<samsonframework\c...metadata\ClassMetadata>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
61
            }
62
        }
63
64
        $classMetadata->methodsMetadata[$method->getName()] = $methodMetadata;
0 ignored issues
show
Bug introduced by
Consider using $method->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
65
    }
66
}
67