SetFormValue::execute()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
3
4
namespace Magium\Actions;
5
6
use Facebook\WebDriver\WebDriverElement;
7
use Facebook\WebDriver\WebDriverSelect;
8
use Magium\NotFoundException;
9
use Magium\WebDriver\WebDriver;
10
11
class SetFormValue implements ActionInterface
12
{
13
    const ACTION = "SetFormValue";
14
15
    protected $webDriver;
16
17
    public function __construct(
18
        WebDriver $webDriver
19
    )
20
    {
21
        $this->webDriver = $webDriver;
22
    }
23
24
    public function setByLabel($label, $value)
25
    {
26
        $this->set($label, $value);
27
    }
28
29
    public function setById($id, $value)
30
    {
31
        $this->set('id:' . $id, $value);
32
    }
33
34
    public function set($name, $value)
35
    {
36
        $formElement = null;
0 ignored issues
show
Unused Code introduced by
$formElement is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
37
        $pos = strpos($name, 'id:');
38
        if ($pos === 0) {
39
            $formElement = $this->webDriver->byId(substr($name, 3));
40
        } else {
41
            $element = $this->webDriver->byXpath(sprintf('//label[.="%s"]', $name));
42
            if (!$element->getAttribute('for')) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $element->getAttribute('for') of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
43
                throw new NotFoundException('Unable to find the "for" attribute for a label with the text: ' . $name);
44
            }
45
            $formElement = $this->webDriver->byId($element->getAttribute('for'));
46
        }
47
48
        $type = strtolower($formElement->getTagName());
49
        switch ($type) {
50
            case 'select':
51
                $this->setSelect($formElement, $value);
52
                break;
53
            default:
54
                $type = strtolower($formElement->getAttribute('type'));
55
                switch ($type) {
56
                    case 'radio':
57
                        $this->setClicked($formElement, $value);
58
                        break;
59
                    case 'checkbox':
60
                        $this->setClicked($formElement, $value);
61
                        break;
62
                    default:
63
                        $this->setText($formElement, $value);
64
                }
65
66
        }
67
    }
68
69
    protected function setText(WebDriverElement $element, $value)
70
    {
71
        $element->clear();
72
        $element->sendKeys($value);
73
74
    }
75
76
    protected function setClicked(WebDriverElement $element, $value)
77
    {
78
        $checked = (bool)$element->getAttribute('checked');
79
        if ((bool)$checked != (bool)$value) {
80
            $element->click();
81
        }
82
    }
83
84
    protected function setSelect(WebDriverElement $element, $value)
85
    {
86
        $select = new WebDriverSelect($element);
87
        if ($select->isMultiple()) {
88
            $select->deselectAll();
89
        }
90
        if (!is_array($value)) {
91
            $value = [$value];
92
        }
93
        foreach ($value as $v) {
94
            if (strpos($v, 'value:') === 0) {
95
                $select->selectByValue(substr($value, 6));
96
            } else {
97
                $select->selectByVisibleText($v);
98
            }
99
        }
100
    }
101
102
    public function execute($name, $value)
103
    {
104
        $this->set($name, $value);
105
    }
106
107
}
108