BehatVariablesArgumentTransformer::validSlotName()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 9
rs 9.6666
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
3
namespace rdx\behatvars;
4
5
use Behat\Behat\Definition\Call\DefinitionCall;
6
use Behat\Behat\Transformation\Transformer\ArgumentTransformer;
7
use rdx\behatvars\BehatVariablesContext;
8
use rdx\behatvars\BehatVariablesDatabase;
9
10
class BehatVariablesArgumentTransformer implements ArgumentTransformer {
11
12
	const SLOT_NAME_OPEN = '<<';
13
	const SLOT_NAME_CLOSE = '>>';
14
	const SLOT_NAME_REGEX = '#<<([a-z]\w*)>>#i';
15
16
	protected $context;
17
	protected $matches;
18
19
	/**
20
	 *
21
	 */
22
	static public function validSlotName($slot) {
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
23
		// Empty is allowed, and must be handled by the caller.
24
		if ($slot === '') {
25
			return true;
26
		}
27
28
		$slot = self::SLOT_NAME_OPEN . $slot . self::SLOT_NAME_CLOSE;
29
		return preg_match(self::SLOT_NAME_REGEX, $slot);
30
	}
31
32
	/**
33
	 *
34
	 */
35
	public function supportsDefinitionAndArgument(DefinitionCall $definitionCall, $argumentIndex, $argumentValue) {
36
		return
37
			is_scalar($argumentValue) &&
38
			preg_match_all(self::SLOT_NAME_REGEX, $argumentValue, $this->matches, PREG_SET_ORDER);
39
	}
40
41
	/**
42
	 *
43
	 */
44
	public function transformArgument(DefinitionCall $definitionCall, $argumentIndex, $argumentValue) {
45
		$replacements = array();
46
		foreach ($this->matches as $match) {
47
			$replacements[ $match[0] ] = BehatVariablesDatabase::get($match[1]);
48
		}
49
50
		return strtr($argumentValue, $replacements);
51
	}
52
53
}
54