Dependency::resolve()   B
last analyzed

Complexity

Conditions 5
Paths 11

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 24
ccs 13
cts 13
cp 1
rs 8.5125
c 0
b 0
f 0
cc 5
eloc 14
nc 11
nop 2
crap 5
1
<?php
2
3
namespace Zewa;
4
5
use Zewa\Exception\Exception;
6
use Zewa\Exception\LookupException;
7
8
class Dependency
9
{
10
    /**
11
     * @var Container
12
     */
13
    private $dependencies;
14
15 38
    public function __construct(Config $config, Container $container)
16
    {
17
        $local = [
18 38
            '\Zewa\Config' => $config,
19 38
            '\Zewa\Container' => $container,
20 38
            '\Zewa\Dependency' => $this
21
        ];
22
23 38
        $this->dependencies = $container;
24 38
        $this->dependencies->set('__dependencies', $local);
25 38
    }
26
27 1 View Code Duplication
    public function flushDependency(string $class)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
28
    {
29 1
        $dependencies = $this->dependencies->get('__dependencies');
30
31 1
        if (isset($dependencies[$class]) === true) {
32 1
            unset($dependencies[$class]);
33 1
            $this->dependencies->set('__dependencies', $dependencies);
34
        }
35 1
    }
36
37 37
    public function isDependencyLoaded(string $class) : bool
38
    {
39 37
        $dependencies = $this->dependencies->get('__dependencies');
40
41 37
        if (isset($dependencies[$class]) === false) {
42 36
            return false;
43
        }
44
45 34
        return true;
46
    }
47
48 33
    public function getDependency(string $class)
49
    {
50 33
        $dependencies = $this->dependencies->get('__dependencies');
51 33
        return $dependencies[$class] ?? null;
52
    }
53
54 34 View Code Duplication
    private function load(string $class, $dependency, bool $persist)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
55
    {
56 34
        if ($persist === true) {
57 33
            $dependencies = $this->dependencies->get('__dependencies');
58 33
            $dependencies[$class] = $dependency;
59 33
            $this->dependencies->set('__dependencies', $dependencies);
60
        }
61
62
//        $this->injectAppInstance($dependency);
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
63 34
        return $dependency;
64
    }
65
66 35
    public function resolve($class, $persist = false)
67
    {
68 35
        if ($this->isDependencyLoaded($class)) {
69 32
            return $this->getDependency($class);
70
        }
71
72
        try {
73 35
            $reflectionClass = new \ReflectionClass($class);
74
75 34
            $constructor = $reflectionClass->getConstructor();
76
77 34
            if (!$constructor || $constructor->getParameters() === 0) {
78 33
                $dependency = new $class;
79 33
                return $this->load($class, $dependency, $persist);
80
            }
81
82 33
            $params = $this->constructConstructorParameters($reflectionClass);
83 33
            $dependency = $reflectionClass->newInstanceArgs($params);
84
85 33
            return $this->load($class, $dependency, $persist);
86 1
        } catch (\ReflectionException $e) {
87 1
            return false;
88
        }
89
    }
90
91
    /**
92
     * @param \ReflectionClass $reflection
93
     * @return array
94
     */
95 33
    private function constructConstructorParameters(\ReflectionClass $reflection) : array
96
    {
97 33
        $constructor = $reflection->getConstructor();
98 33
        $params = $constructor->getParameters();
99
100 33
        $injection = [];
101
102 33
        foreach ($params as $param) {
103
            // Here we should perform a bunch of checks, such as: isArray(), isCallable(), isDefaultValueAvailable()
104
            // isOptional() etc.
105 33
            if (is_null($param->getClass())) {
106 1
                $injection[] = null;
107 1
                continue;
108
            }
109 32
            $injection[] = $this->resolve("\\" . $param->getClass()->getName());
110
        }
111
112 33
        return $injection;
113
    }
114
}
115