InjectionHelper   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 10
c 0
b 0
f 0
lcom 0
cbo 1
dl 0
loc 66
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getName() 0 4 1
A methodInjection() 0 11 2
C getMethod() 0 27 7
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);
29
30
        $argumentsToInject = [];
31
        foreach ($parameters as $parameter) {
0 ignored issues
show
Bug introduced by
The expression $parameters of type array|null is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
32
            $argumentsToInject[] = $objectManager->get($parameter[1]);
33
        }
34
35
        call_user_func_array([$object, $methodName], $argumentsToInject);
36
    }
37
38
    /**
39
     * Read class constructor signature
40
     *
41
     * @param Object $object
42
     * @param string $methodName
43
     * @return array|null
44
     * @throws \ReflectionException
45
     */
46
    protected function getMethod($object, $methodName)
47
    {
48
        $object = new \ReflectionObject($object);
49
        $result = null;
50
        $method = $object->getMethod($methodName);
51
        if ($method) {
52
            $result = [];
53
            /** @var $parameter \ReflectionParameter */
54
            foreach ($method->getParameters() as $parameter) {
55
                try {
56
                    $result[] = [
57
                        $parameter->getName(),
58
                        $parameter->getClass() !== null ? $parameter->getClass()->getName() : null,
59
                        !$parameter->isOptional(),
60
                        $parameter->isOptional()
61
                            ? ($parameter->isDefaultValueAvailable() ? $parameter->getDefaultValue() : null)
62
                            : null,
63
                    ];
64
                } catch (\ReflectionException $e) {
65
                    $message = $e->getMessage();
66
                    throw new \ReflectionException($message, 0, $e);
67
                }
68
            }
69
        }
70
71
        return $result;
72
    }
73
}
74