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\Argument; |
13
|
|
|
|
14
|
|
|
use Behat\Testwork\Argument\ArgumentOrganiser as BehatArgumentOrganiser; |
15
|
|
|
use Doctrine\Common\Annotations\Reader; |
16
|
|
|
use Gorghoa\StepArgumentInjectorBehatExtension\Annotation\StepInjectorArgument; |
17
|
|
|
// use Gorghoa\StepArgumentInjectorBehatExtension\Context\Initializer\StepArgumentInjectorInitializer; |
18
|
|
|
use ReflectionFunctionAbstract; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @author Rodrigue Villetard <[email protected]> |
22
|
|
|
* @author Vincent Chalamon <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
final class ArgumentOrganiser implements BehatArgumentOrganiser |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var BehatArgumentOrganiser |
28
|
|
|
*/ |
29
|
|
|
private $baseOrganiser; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var StepArgumentHolder[] |
33
|
|
|
*/ |
34
|
|
|
private $stepArgumentHolders; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var Reader |
38
|
|
|
*/ |
39
|
|
|
private $reader; |
40
|
|
|
|
41
|
1 |
|
public function __construct(BehatArgumentOrganiser $organiser, array $stepArgumentHolders, Reader $reader) |
42
|
|
|
{ |
43
|
1 |
|
$this->baseOrganiser = $organiser; |
44
|
1 |
|
$this->stepArgumentHolders = $stepArgumentHolders; |
45
|
1 |
|
$this->reader = $reader; |
46
|
1 |
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* {@inheritdoc} |
50
|
|
|
*/ |
51
|
1 |
|
public function organiseArguments(ReflectionFunctionAbstract $function, array $match) |
52
|
|
|
{ |
53
|
1 |
|
$i = array_slice(array_keys($match), -1, 1)[0]; |
54
|
1 |
|
$paramsKeys = array_map(function ($element) { |
55
|
1 |
|
return $element->name; |
56
|
1 |
|
}, $function->getParameters()); |
57
|
|
|
|
58
|
1 |
|
if (!$function instanceof \ReflectionMethod) { |
59
|
|
|
return $this->baseOrganiser->organiseArguments($function, $match); |
60
|
|
|
} |
61
|
|
|
|
62
|
1 |
|
$annotations = $this->reader->getMethodAnnotations($function); |
63
|
|
|
|
64
|
1 |
|
foreach ($annotations as $annotation) { |
65
|
1 |
View Code Duplication |
if ($annotation instanceof StepInjectorArgument && |
|
|
|
|
66
|
1 |
|
in_array($argument = $annotation->getArgument(), $paramsKeys) |
67
|
|
|
) { |
68
|
|
|
/* @var StepInjectorArgument $annotation */ |
69
|
1 |
|
foreach ($this->stepArgumentHolders as $hooker) { |
70
|
1 |
|
if ($hooker->doesHandleStepArgument($annotation)) { |
71
|
|
|
|
72
|
1 |
|
$match[$argument] |
73
|
1 |
|
= $match[strval(++$i)] |
74
|
1 |
|
= $hooker->getStepArgumentValueFor($annotation) |
75
|
|
|
; |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
1 |
|
return $this->baseOrganiser->organiseArguments($function, $match); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
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.