BehatVariablesArgumentTransformer   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 6
c 3
b 0
f 0
lcom 1
cbo 0
dl 0
loc 44
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A validSlotName() 0 9 2
A supportsDefinitionAndArgument() 0 5 2
A transformArgument() 0 8 2
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