Completed
Push — 2.x ( 34f5e4...8c01f4 )
by Akihito
11s
created

src/AnnotatedClass.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
use Ray\Di\Di\PostConstruct;
12
13
final class AnnotatedClass
14
{
15
    /**
16
     * @var AnnotationReader
17
     */
18
    private $reader;
19
20
    /**
21
     * @var AnnotatedClassMethods
22
     */
23
    private $injectionMethod;
24
25 64
    public function __construct(AnnotationReader $reader)
26
    {
27 64
        AnnotationRegistry::registerFile(__DIR__ . '/DoctrineAnnotations.php');
0 ignored issues
show
Deprecated Code introduced by
The method Doctrine\Common\Annotati...egistry::registerFile() has been deprecated with message: this method is deprecated and will be removed in doctrine/annotations 2.0 autoloading should be deferred to the globally registered autoloader by then. For now, use @example AnnotationRegistry::registerLoader('class_exists')

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
28 64
        $this->reader = $reader;
29 64
        $this->injectionMethod = new AnnotatedClassMethods($reader);
30 64
    }
31
32
    /**
33
     * Return factory instance
34
     *
35
     * @param \ReflectionClass $class Target class reflection
36
     *
37
     * @return NewInstance
38
     */
39 64
    public function getNewInstance(\ReflectionClass $class)
40
    {
41 64
        $setterMethods = new SetterMethods([]);
42 64
        $methods = $class->getMethods();
43 64
        foreach ($methods as $method) {
44 54
            if ($method->name === '__construct') {
45 49
                continue;
46
            }
47 53
            $setterMethods->add($this->injectionMethod->getSetterMethod($method));
48
        }
49 64
        $name = $this->injectionMethod->getConstructorName($class);
50 64
        $newInstance = new NewInstance($class, $setterMethods, $name);
51
52 64
        return $newInstance;
53
    }
54
55
    /**
56
     * Return @-PostConstruct method reflection
57
     *
58
     * @param \ReflectionClass $class
59
     *
60
     * @return null|\ReflectionMethod
61
     */
62 64
    public function getPostConstruct(\ReflectionClass $class)
63
    {
64 64
        $methods = $class->getMethods();
65 64
        foreach ($methods as $method) {
66
            /* @var $annotation PostConstruct|null */
67 52
            $annotation = $this->reader->getMethodAnnotation($method, 'Ray\Di\Di\PostConstruct');
68 52
            if ($annotation instanceof PostConstruct) {
69 52
                return $method;
70
            }
71
        }
72
73 64
        return null;
74
    }
75
}
76