1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Ciandt\Behat\PlaceholdersExtension\Transformation; |
5
|
|
|
|
6
|
|
|
use Behat\Behat\Definition\Call\DefinitionCall; |
7
|
|
|
use Behat\Gherkin\Node\TableNode; |
8
|
|
|
use Behat\Testwork\Call\RuntimeCallee; |
9
|
|
|
use Ciandt\Behat\PlaceholdersExtension\Config\PlaceholdersRepository; |
10
|
|
|
use Ciandt\Behat\PlaceholdersExtension\PlaceholderContainer\PlaceholderContainer; |
11
|
|
|
use Ciandt\Behat\PlaceholdersExtension\PlaceholderContainer\PlaceholderContainerStepNode; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* |
15
|
|
|
*/ |
16
|
|
|
final class RuntimePlaceholdersTransformation extends RuntimeCallee implements PlaceholdersTransformation { |
17
|
|
|
|
18
|
|
|
const USER_DEFINED_PLACEHOLDER_REGEX = '/\${(?P<placeholder>[a-zA-Z0-9_-]+)}/'; |
19
|
|
|
|
20
|
|
|
public function __toString() { |
21
|
|
|
return 'UserDefinedPlaceholderTransform'; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public function getPattern() { |
25
|
|
|
return self::USER_DEFINED_PLACEHOLDER_REGEX; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
private static function replaceStringPlaceholders($string, PlaceholdersRepository $repository, $tags) { |
29
|
|
|
preg_match_all(self::USER_DEFINED_PLACEHOLDER_REGEX, $string, $matches, PREG_SET_ORDER); |
30
|
|
|
foreach ($matches as $match) { |
|
|
|
|
31
|
|
|
$placeholder = $match['placeholder']; |
32
|
|
|
$replacement = $repository->getReplacement($placeholder, $tags); |
33
|
|
|
$string = str_replace('${' . $placeholder . '}', $replacement, $string); |
34
|
|
|
} |
35
|
|
|
return $string; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
private static function replaceTablePlaceholders(TableNode $table, PlaceholdersRepository $repository, PlaceholderContainerStepNode $container) { |
|
|
|
|
39
|
|
|
//@todo |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public static function supportsDefinitionAndArgument($tags, $argumentValue) { |
43
|
|
|
if (is_string($argumentValue) && preg_match(self::USER_DEFINED_PLACEHOLDER_REGEX, $argumentValue) === 1) { |
44
|
|
|
return true; |
45
|
|
|
} |
46
|
|
|
if ($argumentValue instanceof TableNode) { |
47
|
|
|
return false; |
48
|
|
|
} |
49
|
|
|
return false; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
private static function tableHasPlaceholders(TableNode $table) { |
|
|
|
|
53
|
|
|
return (preg_match(self::USER_DEFINED_PLACEHOLDER_REGEX, $table->getTableAsString()) === 1); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public static function transformArgument($argumentValue, PlaceholdersRepository $repository, $tags) { |
57
|
|
|
if (is_string($argumentValue)) { |
58
|
|
|
return self::replaceStringPlaceholders($argumentValue, $repository, $tags); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
if ($argumentValue instanceof TableNode) { |
62
|
|
|
return self::replaceTablePlaceholders($table, $repository, $tags); |
|
|
|
|
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
} |
67
|
|
|
|
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.