|
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, PlaceholderContainerStepNode $container) { |
|
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, $container); |
|
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(DefinitionCall $definitionCall, $argumentValue) { |
|
43
|
|
|
if ($definitionCall->getStep() instanceof PlaceholderContainerStepNode) { |
|
44
|
|
|
if (is_string($argumentValue) && preg_match(self::USER_DEFINED_PLACEHOLDER_REGEX, $argumentValue) === 1) { |
|
45
|
|
|
return true; |
|
46
|
|
|
} |
|
47
|
|
|
if ($argumentValue instanceof TableNode && $this->tableHasPlaceholders($table)) { |
|
|
|
|
|
|
48
|
|
|
return false; |
|
49
|
|
|
} |
|
50
|
|
|
} |
|
51
|
|
|
return false; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
private static function tableHasPlaceholders(TableNode $table) { |
|
55
|
|
|
return (preg_match(self::USER_DEFINED_PLACEHOLDER_REGEX, $table->getTableAsString()) === 1); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public static function transformArgument($argumentValue, PlaceholdersRepository $repository, PlaceholderContainer $container) { |
|
59
|
|
|
if (is_string($argumentValue)) { |
|
60
|
|
|
return self::replaceStringPlaceholders($argumentValue, $repository, $container); |
|
|
|
|
|
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
if ($argumentValue instanceof TableNode) { |
|
64
|
|
|
return self::replaceTablePlaceholders($table, $repository, $container); |
|
|
|
|
|
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
} |
|
69
|
|
|
|
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.