Completed
Push — develop ( e1ba4a...6139e1 )
by Tom
04:14
created

InjectionHelper::constructorInjection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 10
rs 9.4285
cc 1
eloc 6
nc 1
nop 2
1
<?php
2
3
namespace N98\Util\Console\Helper;
4
5
use Magento\Framework\ObjectManager\ObjectManager;
6
use Symfony\Component\Console\Helper\Helper as AbstractHelper;
7
8
class InjectionHelper extends AbstractHelper
9
{
10
    /**
11
     * Returns the canonical name of this helper.
12
     *
13
     * @return string The canonical name
14
     *
15
     * @api
16
     */
17
    public function getName()
18
    {
19
        return 'injection';
20
    }
21
22
    /**
23
     * @param Object $object
24
     * @param string $methodName
25
     */
26
    public function methodInjection($object, $methodName, ObjectManager $objectManager)
27
    {
28
        $parameters = $this->getMethod($object, $methodName);
0 ignored issues
show
Documentation introduced by
$object is of type object, but the function expects a string.

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...
29
        $argumentsToInject = array_map([$objectManager, 'get'], $parameters);
30
31
        call_user_func_array([$object, $methodName], $argumentsToInject);
32
    }
33
34
    public function constructorInjection($class, ObjectManager $objectManager)
35
    {
36
        $parameters = $this->getMethod($class, '__construct');
37
        $argumentsToInject = array_map([$objectManager, 'get'], $parameters);
38
39
        $refl = new \ReflectionClass($class);
40
        $object = $refl->newInstanceArgs($argumentsToInject);
41
42
        return $object;
43
    }
44
45
    /**
46
     * Read class method signature
47
     *
48
     * @param string $class
49
     * @param string $methodName
50
     * @return array
51
     * @throws \ReflectionException
52
     */
53
    protected function getMethod($class, $methodName)
54
    {
55
        $refl = new \ReflectionClass($class);
56
        if (!$refl->hasMethod($methodName)) {
57
            return [];
58
        }
59
60
        $method = $refl->getMethod($methodName);
61
        if (!$method) {
62
            throw new \InvalidArgumentException(
63
                sprintf("Unable to obtain method \"%s\" for class \"%s\"", $class, $methodName)
64
            );
65
        }
66
67
        $result = array_map([$this, 'getParameterClass'], $method->getParameters());
68
69
        return $result;
70
    }
71
72
    private function getParameterClass(\ReflectionParameter $parameter)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
73
    {
74
        return $parameter->getClass() !== null ? $parameter->getClass()->getName() : null;
75
    }
76
}
77