Completed
Push — 2.x ( bcb56c...6c548f )
by Akihito
04:48 queued 03:07
created

src/NewInstance.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 Ray\Aop\Bind as AopBind;
10
11
final class NewInstance
12
{
13
    /**
14
     * @var string
15
     */
16
    private $class;
17
18
    /**
19
     * @var SetterMethods
20
     */
21
    private $setterMethods;
22
23
    /**
24
     * @var Arguments
25
     */
26
    private $arguments;
27
28
    /**
29
     * @var AopBind
30
     */
31
    private $bind;
32
33 49
    public function __construct(
34
        \ReflectionClass $class,
35
        SetterMethods $setterMethods,
36
        Name $constructorName = null
37
    ) {
38 49
        $constructorName = $constructorName ?: new Name(Name::ANY);
39 49
        $this->class = $class->name;
40 49
        $constructor = $class->getConstructor();
41 49
        if ($constructor) {
42 26
            $this->arguments = new Arguments($constructor, $constructorName);
43 26
        }
44 49
        $this->setterMethods = $setterMethods;
45 49
    }
46
47
    /**
48
     * @param string  $class
49
     * @param AopBind $bind
50
     */
51 7
    public function weaveAspects($class, AopBind $bind)
52
    {
53 7
        $this->class = $class;
54 7
        $this->bind = new AspectBind($bind);
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Ray\Di\AspectBind($bind) of type object<Ray\Di\AspectBind> is incompatible with the declared type object<Ray\Aop\Bind> of property $bind.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
55 7
    }
56
57
    /**
58
     * @param Container $container
59
     *
60
     * @return object
61
     */
62 48
    public function __invoke(Container $container)
63
    {
64
        // constructor injection
65 48
        $instance = $this->arguments ? (new \ReflectionClass($this->class))->newInstanceArgs($this->arguments->inject($container)) : new $this->class;
66
67
        // setter injection
68 47
        $this->setterMethods->__invoke($instance, $container);
69
70
        // bind dependency injected interceptors
71 47
        if ($this->bind instanceof AspectBind) {
72 8
            $instance->bindings = $this->bind->inject($container);
73 8
        }
74
75 47
        return $instance;
76
    }
77
78
    /**
79
     * @return string
80
     */
81 20
    public function __toString()
82
    {
83 20
        return $this->class;
84
    }
85
}
86