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
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @author Vincent Chalamon <[email protected]> |
19
|
|
|
*/ |
20
|
|
|
class ArgumentsResolver |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var Reader |
24
|
|
|
*/ |
25
|
|
|
private $reader; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var StepArgumentHolder[] |
29
|
|
|
*/ |
30
|
|
|
private $stepArgumentHolders; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* ArgumentsResolver constructor. |
34
|
|
|
* |
35
|
|
|
* @param StepArgumentHolder[] $stepArgumentHolders |
36
|
|
|
* @param Reader $reader |
37
|
|
|
*/ |
38
|
1 |
|
public function __construct($stepArgumentHolders, Reader $reader) |
39
|
|
|
{ |
40
|
1 |
|
$this->stepArgumentHolders = $stepArgumentHolders; |
41
|
1 |
|
$this->reader = $reader; |
42
|
1 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param \ReflectionMethod $function |
46
|
|
|
* @param array $arguments |
47
|
|
|
* |
48
|
|
|
* @return array |
49
|
|
|
*/ |
50
|
1 |
|
public function resolve(\ReflectionMethod $function, array $arguments) |
51
|
|
|
{ |
52
|
|
|
// No `@StepInjectorArgument` annotation found |
53
|
1 |
|
if (null === $this->reader->getMethodAnnotation($function, StepInjectorArgument::class)) { |
54
|
|
|
return $arguments; |
55
|
|
|
} |
56
|
|
|
|
57
|
1 |
|
$paramsKeys = array_map(function (\ReflectionParameter $element) { |
58
|
1 |
|
return $element->getName(); |
59
|
1 |
|
}, $function->getParameters()); |
60
|
|
|
|
61
|
|
|
// Prepare arguments from annotations |
62
|
1 |
|
$annotations = $this->reader->getMethodAnnotations($function); |
63
|
1 |
|
foreach ($annotations as $annotation) { |
64
|
1 |
View Code Duplication |
if ($annotation instanceof StepInjectorArgument && |
|
|
|
|
65
|
1 |
|
in_array($argument = $annotation->getArgument(), $paramsKeys) |
66
|
|
|
) { |
67
|
|
|
/* @var StepArgumentInjectorArgument $annotation */ |
68
|
1 |
|
foreach ($this->stepArgumentHolders as $hooker) { |
69
|
1 |
|
if ($hooker->doesHandleStepArgument($annotation)) { |
70
|
1 |
|
$arguments[$argument] = $hooker->getStepArgumentValueFor($annotation); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
// Reorder arguments |
77
|
1 |
|
$params = []; |
78
|
1 |
|
foreach ($function->getParameters() as $parameter) { |
79
|
1 |
|
$name = $parameter->getName(); |
|
|
|
|
80
|
1 |
|
$params[$name] = isset($arguments[$name]) ? $arguments[$name] : $arguments[$parameter->getPosition()]; |
81
|
|
|
} |
82
|
|
|
|
83
|
1 |
|
return $params; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
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.