Completed
Push — 2.x ( 5f03c1...dc1b30 )
by Akihito
04:41 queued 02:42
created

AnnotatedClass   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 1 Features 1
Metric Value
wmc 7
c 4
b 1
f 1
lcom 2
cbo 5
dl 0
loc 63
ccs 25
cts 25
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getNewInstance() 0 15 3
A getPostConstruct() 0 13 3
1
<?php
2
/**
3
 * This file is part of the Ray.Di package.
4
 *
5
 * @license http://opensource.org/licenses/MIT MIT
6
 */
7
namespace Ray\Di;
8
9
use Doctrine\Common\Annotations\AnnotationReader;
10
use Doctrine\Common\Annotations\AnnotationRegistry;
11
12
final class AnnotatedClass
13
{
14
    /**
15
     * @var AnnotationReader
16
     */
17
    private $reader;
18
19
    /**
20
     * @var AnnotatedClassMethods
21
     */
22
    private $injectionMethod;
23
24 44
    public function __construct(AnnotationReader $reader)
25
    {
26 44
        AnnotationRegistry::registerFile(__DIR__ . '/DoctrineAnnotations.php');
27 44
        $this->reader = $reader;
28 44
        $this->injectionMethod = new AnnotatedClassMethods($reader);
29 44
    }
30
31
    /**
32
     * Return factory instance
33
     *
34
     * @param \ReflectionClass $class Target class reflection
35
     *
36
     * @return NewInstance
37
     */
38 44
    public function getNewInstance(\ReflectionClass $class)
39
    {
40 44
        $setterMethods = new SetterMethods([]);
41 44
        $methods = $class->getMethods();
42 44
        foreach ($methods as $method) {
43 30
            if ($method->name === '__construct') {
44 17
                continue;
45
            }
46 25
            $setterMethods->add($this->injectionMethod->getSetterMethod($method));
47 44
        }
48 44
        $name = $this->injectionMethod->getConstructorName($class);
49 44
        $newInstance = new NewInstance($class, $setterMethods, $name);
50
51 44
        return $newInstance;
52
    }
53
54
    /**
55
     * Return @-PostConstruct method reflection
56
     *
57
     * @param \ReflectionClass $class
58
     *
59
     * @return null|\ReflectionMethod
60
     */
61 44
    public function getPostConstruct(\ReflectionClass $class)
62 1
    {
63 44
        $methods = $class->getMethods();
64 44
        foreach ($methods as $method) {
65
            /* @var $annotation \Ray\Di\Di\PostConstruct|null */
66 28
            $annotation = $this->reader->getMethodAnnotation($method, 'Ray\Di\Di\PostConstruct');
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $annotation is correct as $this->reader->getMethod...Di\\Di\\PostConstruct') (which targets Doctrine\Common\Annotati...::getMethodAnnotation()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
67 28
            if ($annotation) {
68 6
                return $method;
69
            }
70 44
        }
71
72 44
        return null;
73
    }
74
}
75