1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the ScenarioStateBehatExtension 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\ScenarioStateBehatExtension\Argument; |
13
|
|
|
|
14
|
|
|
use Behat\Testwork\Argument\ArgumentOrganiser; |
15
|
|
|
use Doctrine\Common\Annotations\Reader; |
16
|
|
|
use Gorghoa\ScenarioStateBehatExtension\Annotation\ScenarioStateArgument; |
17
|
|
|
use Gorghoa\ScenarioStateBehatExtension\Context\Initializer\ScenarioStateInitializer; |
18
|
|
|
use ReflectionFunctionAbstract; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @author Rodrigue Villetard <[email protected]> |
22
|
|
|
* @author Vincent Chalamon <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
final class ScenarioStateArgumentOrganiser implements ArgumentOrganiser |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var ArgumentOrganiser |
28
|
|
|
*/ |
29
|
|
|
private $baseOrganiser; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var ScenarioStateInitializer |
33
|
|
|
*/ |
34
|
|
|
private $store; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var Reader |
38
|
|
|
*/ |
39
|
|
|
private $reader; |
40
|
|
|
|
41
|
1 |
|
public function __construct(ArgumentOrganiser $organiser, ScenarioStateInitializer $store, Reader $reader) |
42
|
|
|
{ |
43
|
1 |
|
$this->baseOrganiser = $organiser; |
44
|
1 |
|
$this->store = $store; |
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 |
|
$store = $this->store->getStore(); |
59
|
|
|
|
60
|
1 |
|
if (!($function instanceof \ReflectionMethod)) { |
61
|
|
|
return $this->baseOrganiser->organiseArguments($function, $match); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** @var ScenarioStateArgument[] $annotations */ |
65
|
1 |
|
$annotations = $this->reader->getMethodAnnotations($function); |
66
|
1 |
View Code Duplication |
foreach ($annotations as $annotation) { |
|
|
|
|
67
|
1 |
|
if ($annotation instanceof ScenarioStateArgument && |
68
|
1 |
|
in_array($annotation->getArgument(), $paramsKeys) && |
69
|
1 |
|
$store->hasStateFragment($annotation->getName()) |
70
|
|
|
) { |
71
|
1 |
|
$match[$annotation->getArgument()] = $store->getStateFragment($annotation->getName()); |
72
|
1 |
|
$match[strval(++$i)] = $store->getStateFragment($annotation->getName()); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
1 |
|
return $this->baseOrganiser->organiseArguments($function, $match); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
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.