Completed
Push — master ( 7203d2...e7a5da )
by Rudie
02:02
created

BehatVariablesContext::saveItInto()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 24
rs 8.5125
cc 5
eloc 15
nc 5
nop 1
1
<?php
2
3
namespace rdx\behatvars;
4
5
use Behat\Behat\Context\Context;
6
use Behat\Behat\Context\SnippetAcceptingContext;
7
use Behat\Behat\Hook\Scope\AfterFeatureScope;
8
use Behat\Behat\Hook\Scope\AfterStepScope;
9
use rdx\behatvars\BehatVariablesArgumentTransformer;
10
11
class BehatVariablesContext implements Context, SnippetAcceptingContext {
12
13
	protected $lastResult = [];
14
15
	static protected $storage = array();
16
17
	/**
18
	 * Initializes context.
19
	 */
20
	public function __construct() {
21
22
	}
23
24
25
26
	/**
27
	 * @AfterStep
28
	 */
29
	public function afterStep(AfterStepScope $scope) {
30
		$this->lastResult = [];
31
32
		$result = $scope->getTestResult();
33
		if (is_callable(array($result, 'getCallResult'))) {
34
			$result = $result->getCallResult();
35
			if (is_callable(array($result, 'getReturn'))) {
36
				$result = $result->getReturn();
37
				if ($result !== null) {
38
					$this->lastResult = is_array($result) && isset($result[0]) ? array_values($result) : [$result];
39
				}
40
			}
41
		}
42
	}
43
44
	/**
45
	 * @AfterFeature
46
	 */
47
	static public function afterFeature(AfterFeatureScope $scope) {
0 ignored issues
show
Unused Code introduced by
The parameter $scope is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
48
		self::storageClear();
49
	}
50
51
52
53
	/**
54
	 * @When /^(?:I|we) save (?:it|that|those|them) into "([\w,]+)"$/
55
	 */
56
	public function saveItInto($slot) {
57
		if (!$this->lastResult) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->lastResult of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
58
			throw new \Exception("Can't store empty return value. Have a step method return a value.");
59
		}
60
61
		$slots = explode(',', $slot);
62
		if (count($slots) != count($this->lastResult)) {
63
			$slots = count($slots);
64
			$results = count($this->lastResult);
65
			throw new \Exception("Number of slots ($slots) does not match number of last results ($results).");
66
		}
67
68
		$valids = array_filter($slots, [BehatVariablesArgumentTransformer::class, 'validSlotName']);
69
		if ($valids !== $slots) {
70
			throw new \Exception("Invalid slot name(s) in '$slot'");
71
		}
72
73
		foreach ($slots as $index => $slot) {
74
			$value = $this->lastResult[$index];
75
			$this->storageSet($slot, $value);
76
		}
77
78
		$this->lastResult = [];
79
	}
80
81
82
83
	/**
84
	 *
85
	 */
86
	protected function storageSet($name, $value) {
87
		if (!is_scalar($value)) {
88
			$type = gettype($value);
89
			throw new \Exception("Storing value must be scalar, but it's a '$type'.");
90
		}
91
92
		self::$storage[$name] = $value;
93
	}
94
95
	/**
96
	 *
97
	 */
98
	public function storageGet($name) {
99
		if (!isset(self::$storage[$name])) {
100
			throw new \Exception("Value for '$name' does not exist.");
101
		}
102
103
		return self::$storage[$name];
104
	}
105
106
	/**
107
	 *
108
	 */
109
	static protected function storageClear() {
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
110
		self::$storage = array();
111
	}
112
113
}
114