|
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) { |
|
|
|
|
|
|
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
|
|
|
|
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
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:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.