Completed
Pull Request — master (#127)
by Kevin
07:07
created

SetFormValue   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 17
c 1
b 0
f 0
lcom 1
cbo 5
dl 0
loc 95
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A setByLabel() 0 4 1
A setById() 0 4 1
B set() 0 34 6
A setText() 0 6 1
A setClicked() 0 7 2
A setSelect() 0 15 4
A execute() 0 4 1
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\FastSelectElement;
10
use Magium\WebDriver\WebDriver;
11
12
class SetFormValue
13
{
14
    const ACTION = "SetFormValue";
15
16
    protected $webDriver;
17
18
    public function __construct(
19
        WebDriver $webDriver
20
    )
21
    {
22
        $this->webDriver = $webDriver;
23
    }
24
25
    public function setByLabel($label, $value)
26
    {
27
        $this->set($label, $value);
28
    }
29
30
    public function setById($id, $value)
31
    {
32
        $this->set('id:' . $id, $value);
33
    }
34
35
    public function set($name, $value)
36
    {
37
        $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...
38
        $pos = strpos($name, 'id:');
39
        if ($pos === 0) {
40
            $formElement = $this->webDriver->byId(substr($name, 3));
41
        } else {
42
            $element = $this->webDriver->byXpath(sprintf('//label[.="%s"]', $name));
43
            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...
44
                throw new NotFoundException('Unable to find the "for" attribute for a label with the text: ' . $name);
45
            }
46
            $formElement = $this->webDriver->byId($element->getAttribute('for'));
47
        }
48
49
        $type = strtolower($formElement->getTagName());
50
        switch ($type) {
51
            case 'select':
52
                $this->setSelect($formElement, $value);
53
                break;
54
            default:
55
                $type = strtolower($formElement->getAttribute('type'));
56
                switch ($type) {
57
                    case 'radio':
58
                        $this->setClicked($formElement, $value);
59
                        break;
60
                    case 'checkbox':
61
                        $this->setClicked($formElement, $value);
62
                        break;
63
                    default:
64
                        $this->setText($formElement, $value);
65
                }
66
67
        }
68
    }
69
70
    protected function setText(WebDriverElement $element, $value)
71
    {
72
        $element->clear();
73
        $element->sendKeys($value);
74
75
    }
76
77
    protected function setClicked(WebDriverElement $element, $value)
78
    {
79
        $checked = (bool)$element->getAttribute('checked');
80
        if ((bool)$checked != (bool)$value) {
81
            $element->click();
82
        }
83
    }
84
85
    protected function setSelect(WebDriverElement $element, $value)
86
    {
87
        $select = new WebDriverSelect($element);
88
        $select->deselectAll();
89
        if (!is_array($value)) {
90
            $value = [$value];
91
        }
92
        foreach ($value as $v) {
93
            if (strpos($v, 'value:') === 0) {
94
                $select->selectByValue(substr($value, 6));
95
            } else {
96
                $select->selectByVisibleText($v);
97
            }
98
        }
99
    }
100
101
    public function execute($name, $value)
102
    {
103
        $this->set($name, $value);
104
    }
105
106
}