Completed
Push — master ( 96adce...435a1a )
by Rudie
02:06
created

BehatVariablesContext::storageSet()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 2
eloc 5
nc 2
nop 2
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
use rdx\behatvars\BehatVariablesDatabase;
11
12
class BehatVariablesContext implements Context, SnippetAcceptingContext {
13
14
	protected $lastResult = [];
15
16
17
18
	/**
19
	 * @AfterStep
20
	 */
21
	public function afterStep(AfterStepScope $scope) {
22
		$this->lastResult = [];
23
24
		$result = $scope->getTestResult();
25
		if (is_callable(array($result, 'getCallResult'))) {
26
			$result = $result->getCallResult();
27
			if (is_callable(array($result, 'getReturn'))) {
28
				$result = $result->getReturn();
29
				if ($result !== null) {
30
					$this->lastResult = is_array($result) && isset($result[0]) ? array_values($result) : [$result];
31
				}
32
			}
33
		}
34
	}
35
36
	/**
37
	 * @AfterFeature
38
	 */
39
	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...
40
		BehatVariablesDatabase::clear();
0 ignored issues
show
Bug introduced by
The method clear() does not exist on rdx\behatvars\BehatVariablesDatabase. Did you maybe mean _clear()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
41
	}
42
43
44
45
	/**
46
	 * @When /^(?:I|we) save (?:it|that|those|them) into "([\w,]+)"$/
47
	 */
48
	public function saveItInto($slot) {
49
		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...
50
			throw new \Exception("Can't store empty return value. Have a step method return a value.");
51
		}
52
53
		$slots = explode(',', $slot);
54
		if (count($slots) != count($this->lastResult)) {
55
			$slots = count($slots);
56
			$results = count($this->lastResult);
57
			throw new \Exception("Number of slots ($slots) does not match number of last results ($results).");
58
		}
59
60
		$valids = array_filter($slots, [BehatVariablesArgumentTransformer::class, 'validSlotName']);
61
		if ($valids !== $slots) {
62
			throw new \Exception("Invalid slot name(s) in '$slot'");
63
		}
64
65
		foreach ($slots as $index => $slot) {
66
			$value = $this->lastResult[$index];
67
			BehatVariablesDatabase::set($slot, $value);
68
		}
69
70
		$this->lastResult = [];
71
	}
72
73
}
74