1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the StepArgumentInjectorBehatExtension project. |
5
|
|
|
* |
6
|
|
|
* (c) Rodrigue Villetard <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Gorghoa\StepArgumentInjectorBehatExtension\Resolver; |
13
|
|
|
|
14
|
|
|
use Doctrine\Common\Annotations\Reader; |
15
|
|
|
use Gorghoa\StepArgumentInjectorBehatExtension\Annotation\StepInjectorArgument; |
16
|
|
|
use Gorghoa\StepArgumentInjectorBehatExtension\Argument\StepArgumentHolder; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @author Vincent Chalamon <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
class ArgumentsResolver |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var Reader |
25
|
|
|
*/ |
26
|
|
|
private $reader; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
private $hookers; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param Reader $reader |
35
|
|
|
*/ |
36
|
|
|
public function __construct(array $hookers, Reader $reader) |
37
|
|
|
{ |
38
|
|
|
$this->hookers = $hookers; |
39
|
|
|
$this->reader = $reader; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param \ReflectionMethod $function |
44
|
|
|
* @param array $arguments |
45
|
|
|
* |
46
|
|
|
* @return array |
47
|
|
|
*/ |
48
|
|
|
public function resolve(\ReflectionMethod $function, array $arguments) |
49
|
|
|
{ |
50
|
|
|
// No `@StepArgumentInjectorArgument` annotation found |
51
|
|
|
if (null === $this->reader->getMethodAnnotation($function, StepInjectorArgument::class)) { |
52
|
|
|
return $arguments; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$paramsKeys = array_map(function (\ReflectionParameter $element) { |
56
|
|
|
return $element->getName(); |
57
|
|
|
}, $function->getParameters()); |
58
|
|
|
|
59
|
|
|
|
60
|
|
|
// Prepare arguments from annotations |
61
|
|
|
/** @var StepArgumentInjectorArgument[] $annotations */ |
62
|
|
|
$annotations = $this->reader->getMethodAnnotations($function); |
63
|
|
View Code Duplication |
foreach ($annotations as $annotation) { |
|
|
|
|
64
|
|
|
if ($annotation instanceof StepInjectorArgument && |
65
|
|
|
in_array($annotation->getArgument(), $paramsKeys) |
66
|
|
|
) { |
67
|
|
|
foreach ($this->hookers as $hooker) { |
68
|
|
|
if ($hooker->hasStepArgumentFor($annotation->getName())) { |
69
|
|
|
$arguments[$annotation->getArgument()] = $hooker->getStepArgumentFor($annotation->getName()); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
// Reorder arguments |
76
|
|
|
$params = []; |
77
|
|
|
foreach ($function->getParameters() as $parameter) { |
78
|
|
|
$name = $parameter->getName(); |
|
|
|
|
79
|
|
|
$params[$name] = isset($arguments[$name]) ? $arguments[$name] : $arguments[$parameter->getPosition()]; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return $params; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
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.