Completed
Push — spike ( 2414ba )
by Akihito
06:46
created

Injector::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2.0625

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 6
cts 8
cp 0.75
rs 9.9
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 2.0625
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * This file is part of the Ray.Di package.
7
 *
8
 * @license http://opensource.org/licenses/MIT MIT
9
 */
10
namespace Ray\Di;
11
12
use Ray\Aop\Compiler;
13
use Ray\Di\Exception\Untargeted;
14
15
class Injector implements InjectorInterface
16
{
17
    /**
18
     * @var string
19
     */
20
    private $classDir;
21
22
    /**
23
     * @var Container
24
     */
25
    private $container;
26
27
    /**
28
     * @param AbstractModule $module Binding module
29
     * @param string         $tmpDir Temp directory for generated class
30
     */
31 2
    public function __construct(AbstractModule $module = null, string $tmpDir = '')
32
    {
33 2
        $module = $module ?? new NullModule;
34 2
        $module->install(new AssistedModule);
0 ignored issues
show
Documentation introduced by
new \Ray\Di\AssistedModule() is of type object<Ray\Di\AssistedModule>, but the function expects a object<self>.

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...
35 2
        $this->container = $module->getContainer();
36 2
        $this->classDir = is_dir($tmpDir) ? $tmpDir : sys_get_temp_dir();
37 2
        $this->container->weaveAspects(new Compiler($this->classDir));
38
39
        // builtin injection
40
        (new Bind($this->container, InjectorInterface::class))->toInstance($this);
41
    }
42
43
    /**
44
     * Wakeup
45
     */
46
    public function __wakeup()
47
    {
48
        spl_autoload_register(
49
            function (string $class) {
50
                $file = $this->classDir . DIRECTORY_SEPARATOR . $class . '.php';
51
                if (file_exists($file)) {
52
                    // @codeCoverageIgnoreStart
53
                    include $file;
54
                    // @codeCoverageIgnoreEnd
55
                }
56
            }
57
        );
58
    }
59
60
    /**
61
     * @param string $interface
62
     * @param string $name
63
     *
64
     * @return mixed
65
     */
66
    public function getInstance($interface, $name = Name::ANY)
67
    {
68
        try {
69
            $instance = $this->container->getInstance($interface, $name);
70
        } catch (Untargeted $e) {
71
            $this->bind($interface);
72
            $instance = $this->getInstance($interface, $name);
73
        }
74
75
        return $instance;
76
    }
77
78
    /**
79
     * @param string $class
80
     */
81
    private function bind(string $class)
82
    {
83
        new Bind($this->container, $class);
84
        /* @var $bound Dependency */
85
        $bound = $this->container->getContainer()[$class . '-' . Name::ANY];
86
        if ($bound instanceof Dependency) {
87
            $this->container->weaveAspect(new Compiler($this->classDir), $bound)->getInstance($class, Name::ANY);
88
        }
89
    }
90
}
91