1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Ciandt\Behat\PlaceholdersExtension\Transformation; |
5
|
|
|
|
6
|
|
|
use Behat\Behat\Definition\Call\DefinitionCall; |
7
|
|
|
use Behat\Gherkin\Node\PyStringNode; |
8
|
|
|
use Behat\Gherkin\Node\TableNode; |
9
|
|
|
use Behat\Testwork\Call\RuntimeCallee; |
10
|
|
|
use Ciandt\Behat\PlaceholdersExtension\Config\PlaceholdersRepository; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* |
14
|
|
|
*/ |
15
|
|
|
class RuntimePlaceholdersTransformation extends RuntimeCallee implements PlaceholdersTransformation { |
16
|
|
|
|
17
|
|
|
public function __toString() { |
18
|
|
|
return 'UserDefinedPlaceholderTransform'; |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public function getPattern() { |
22
|
|
|
return PlaceholdersRepository::PLACEHOLDER_REGEX; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
protected static function replaceStringPlaceholders(&$string, PlaceholdersRepository $repository) { |
26
|
|
|
preg_match_all(PlaceholdersRepository::PLACEHOLDER_REGEX, $string, $matches, PREG_SET_ORDER); |
27
|
|
|
foreach ($matches as $match) { |
|
|
|
|
28
|
|
|
$placeholder = $match['placeholder']; |
29
|
|
|
$replacement = $repository->getReplacement($placeholder); |
30
|
|
|
$string = str_replace('${' . $placeholder . '}', $replacement, $string); |
31
|
|
|
} |
32
|
|
|
return $string; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public static function transformArgument($argument, $repository) { |
36
|
|
|
if (is_string($argument)) { |
37
|
|
|
return self::replaceStringPlaceholders($argument, $repository); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
if ($argument instanceof PyStringNode) { |
41
|
|
|
$strings = $argument->getStrings(); |
42
|
|
|
foreach ($strings as &$string){ |
43
|
|
|
self::replaceStringPlaceholders($string, $repository); |
44
|
|
|
} |
45
|
|
|
return new PyStringNode($strings, $argument->getLine()); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
if ($argument instanceof TableNode) { |
49
|
|
|
$table = $argument->getTable(); |
50
|
|
|
array_walk_recursive($table, 'self::replaceTablePlaceholders', $repository); |
51
|
|
|
return new TableNode($table); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
private static function replaceTablePlaceholders(&$item, $key, $repository){ |
|
|
|
|
56
|
|
|
$item = self::replaceStringPlaceholders($item, $repository); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
|
60
|
|
|
public static function supportsArgument($argument) |
61
|
|
|
{ |
62
|
|
|
if (is_string($argument) && preg_match(PlaceholdersRepository::PLACEHOLDER_REGEX, $argument) === 1) { |
63
|
|
|
return true; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
if ($argument instanceof PyStringNode && |
67
|
|
|
preg_match(PlaceholdersRepository::PLACEHOLDER_REGEX, $argument->getRaw()) === 1) { |
68
|
|
|
return true; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
if ($argument instanceof TableNode && |
72
|
|
|
preg_match(PlaceholdersRepository::PLACEHOLDER_REGEX, $argument->getTableAsString()) === 1) { |
73
|
|
|
return true; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return false; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
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.