Completed
Push — master ( 644e0d...d1a3ac )
by Pierre
05:15
created

RawMinkContext::elementAction()   B

Complexity

Conditions 5
Paths 16

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 15
rs 8.8571
cc 5
eloc 8
nc 16
nop 5
1
<?php
2
3
namespace Knp\FriendlyContexts\Context;
4
5
use Behat\Mink\Mink;
6
use Behat\MinkExtension\Context\MinkAwareContext;
7
8
abstract class RawMinkContext extends Context implements MinkAwareContext
9
{
10
    private $mink;
11
12
    private $minkParameters;
13
14
    public function setMink(Mink $mink)
15
    {
16
        $this->mink = $mink;
17
    }
18
19
    public function getMink()
20
    {
21
        return $this->mink;
22
    }
23
24
    public function setMinkParameters(array $parameters)
25
    {
26
        $this->minkParameters = $parameters;
27
    }
28
29
    public function getMinkParameter($offset)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
30
    {
31
       if (!isset($this->minkParameters[$offset])) {
32
            throw new \Exception(sprintf(
33
                'Invalid mink parameter "%s".',
34
                $offset
35
            ));
36
        }
37
38
        return $this->minkParameters[$offset];
39
    }
40
41
    public function getSession($name = null)
42
    {
43
        return $this->getMink()->getSession($name);
44
    }
45
46
    public function assertSession($name = null)
47
    {
48
        return $this->getMink()->assertSession($name);
49
    }
50
51
    public function locatePath($path)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
52
    {
53
        $startUrl = rtrim($this->getMinkParameter('base_url'), '/') . '/';
54
55
        return 0 !== strpos($path, 'http') ? $startUrl . ltrim($path, '/') : $path;
56
    }
57
58
    protected function searchElement($locator, $element, $filterCallback = null, TraversableElement $parent = null)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
59
    {
60
        $parent  = $parent ?: $this->getSession()->getPage();
61
        $locator = $this->fixStepArgument($locator);
0 ignored issues
show
Bug introduced by
The method fixStepArgument() does not seem to exist on object<Knp\FriendlyConte...Context\RawMinkContext>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
62
63
        $elements = $parent->findAll('named', array(
64
            $element, $locator
65
        ));
66
67
        if (null !== $filterCallback && is_callable($filterCallback)) {
68
            $elements = array_values(array_filter($elements, $filterCallback));
69
        }
70
71
        return $elements;
72
    }
73
74
    protected function elementAction($locator, $element, $nbr = 1, $actionCallback, $filterCallback = null)
0 ignored issues
show
Coding Style introduced by
Parameters which have default values should be placed at the end.

If you place a parameter with a default value before a parameter with a default value, the default value of the first parameter will never be used as it will always need to be passed anyway:

// $a must always be passed; it's default value is never used.
function someFunction($a = 5, $b) { }
Loading history...
75
    {
76
        $elements = $this->searchElement($locator, $element, $filterCallback);
77
78
        $nbr = is_numeric($nbr) ? intval($nbr) : $nbr;
79
        $nbr = is_string($nbr) ? 1 : (-1 === $nbr ? count($elements) : $nbr);
80
81
        if ($nbr > count($elements)) {
82
            throw new ElementNotFoundException($this->getSession(), $element, null, $locator);
83
        }
84
85
        $e = $elements[$nbr - 1];
86
87
        $actionCallback($e);
88
    }
89
90
    protected function getAsserter()
91
    {
92
        return new Asserter(new TextFormater);
93
    }
94
}
95