Passed
Pull Request — master (#6)
by Korotkov
01:52
created

ContainerReflectionTrait::getParamsIoC()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 11
nc 5
nop 2
dl 0
loc 19
rs 8.8571
c 0
b 0
f 0
1
<?php
2
/**
3
 * Date: 03.05.18
4
 * Time: 18:23
5
 *
6
 * @author    : Korotkov Danila <[email protected]>
7
 * @copyright Copyright (c) 2018, Korotkov Danila
8
 * @license   http://www.gnu.org/licenses/gpl.html GNU GPLv3.0
9
 */
10
11
namespace Rudra;
12
13
14
use \ReflectionClass;
15
use \ReflectionMethod;
16
17
18
/**
19
 * Trait ContainerReflectionTrait
20
 * @package Rudra
21
 */
22
trait ContainerReflectionTrait
23
{
24
25
    /**
26
     * @param string $key
27
     * @param        $object
28
     */
29
    protected function rawSet(string $key, $object)
30
    {
31
        $this->objects[$key] = $object;
0 ignored issues
show
Bug Best Practice introduced by
The property objects does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
32
    }
33
34
    /**
35
     * @param string $key
36
     * @param        $object
37
     * @param null   $params
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $params is correct as it would always require null to be passed?
Loading history...
38
     */
39
    protected function iOc(string $key, $object, $params = null): void
40
    {
41
        $reflection  = new ReflectionClass($object);
42
        $constructor = $reflection->getConstructor();
43
44
        if ($constructor && $constructor->getNumberOfParameters()) {
45
            $paramsIoC           = $this->getParamsIoC($constructor, $params);
46
            $this->objects[$key] = $reflection->newInstanceArgs($paramsIoC);
0 ignored issues
show
Bug Best Practice introduced by
The property objects does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
47
            return;
48
        }
49
50
        $this->objects[$key] = new $object;
51
    }
52
53
54
    /**
55
     * @param ReflectionMethod $constructor
56
     * @param                  $params
57
     * @return array
58
     */
59
    protected function getParamsIoC(ReflectionMethod $constructor, $params): array
60
    {
61
        $paramsIoC = [];
62
        foreach ($constructor->getParameters() as $key => $value) {
63
            if (isset($value->getClass()->name)) {
64
                $className       = $this->getBinding($value->getClass()->name);
0 ignored issues
show
Bug introduced by
It seems like getBinding() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

64
                /** @scrutinizer ignore-call */ 
65
                $className       = $this->getBinding($value->getClass()->name);
Loading history...
65
                $paramsIoC[$key] = (is_object($className)) ? $className : new $className;
66
                continue;
67
            }
68
69
            if ($value->isDefaultValueAvailable()) {
70
                $paramsIoC[$key] = $value->getDefaultValue();
71
                continue;
72
            }
73
74
            $paramsIoC[$key] = $params[$value->getName()];
75
        }
76
77
        return $paramsIoC;
78
    }
79
}