Passed
Pull Request — 1.x (#62)
by Kevin
02:14
created

ChoiceField::setValue()   B

Complexity

Conditions 10
Paths 16

Size

Total Lines 28
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
eloc 14
nc 16
nop 1
dl 0
loc 28
rs 7.6666
c 1
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Zenstruck\Browser\Dom\Form\Field;
4
5
use Facebook\WebDriver\Exception\NoSuchElementException;
6
use Facebook\WebDriver\WebDriverSelect;
7
use Symfony\Component\DomCrawler\Field\ChoiceFormField;
8
use Symfony\Component\Panther\DomCrawler\Crawler as PantherCrawler;
9
use Zenstruck\Browser\Dom\Form\Field;
10
11
/**
12
 * @mixin ChoiceFormField
13
 *
14
 * @author Kevin Bond <[email protected]>
15
 */
16
final class ChoiceField extends Field
17
{
18
    public function tick(): void
19
    {
20
        if ($this->is('radio')) {
21
            $this->setValue(null);
22
23
            return;
24
        }
25
26
        $this->inner->tick();
0 ignored issues
show
Bug introduced by
The method tick() does not exist on Symfony\Component\DomCrawler\Field\FormField. It seems like you code against a sub-type of Symfony\Component\DomCrawler\Field\FormField such as Symfony\Component\DomCrawler\Field\ChoiceFormField. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

26
        $this->inner->/** @scrutinizer ignore-call */ 
27
                      tick();
Loading history...
27
    }
28
29
    public function untick(): void
30
    {
31
        if ($this->is('radio')) {
32
            throw new \InvalidArgumentException('Radio fields cannot be unchecked.');
33
        }
34
35
        $this->inner->untick();
0 ignored issues
show
Bug introduced by
The method untick() does not exist on Symfony\Component\DomCrawler\Field\FormField. It seems like you code against a sub-type of Symfony\Component\DomCrawler\Field\FormField such as Symfony\Component\DomCrawler\Field\ChoiceFormField. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

35
        $this->inner->/** @scrutinizer ignore-call */ 
36
                      untick();
Loading history...
36
    }
37
38
    public function setValue($value): void
39
    {
40
        if ($this->is('radio')) {
41
            $value = $value ?? $this->attr('value');
42
        }
43
44
        if (null === $value) {
45
            throw new \InvalidArgumentException('Value required for select form fields.');
46
        }
47
48
        if (\is_array($value) && !$this->isMultiple()) {
0 ignored issues
show
Bug introduced by
The method isMultiple() does not exist on Zenstruck\Browser\Dom\Form\Field\ChoiceField. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

48
        if (\is_array($value) && !$this->/** @scrutinizer ignore-call */ isMultiple()) {
Loading history...
49
            throw new \InvalidArgumentException('Value for a single select form field cannot be an array.');
50
        }
51
52
        if (\is_array($value) && $this->isMultiple() && $this->dom->inner() instanceof PantherCrawler) {
0 ignored issues
show
Bug introduced by
The method inner() does not exist on Zenstruck\Browser\Dom. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

52
        if (\is_array($value) && $this->isMultiple() && $this->dom->/** @scrutinizer ignore-call */ inner() instanceof PantherCrawler) {
Loading history...
53
            // todo have a separate PantherCrawler to avoid the "inner" check above
54
            // with panther, unselect all before setting (otherwise existing selections will remain)
55
            (new WebDriverSelect($this->dom->getElement(0)))->deselectAll();
0 ignored issues
show
Bug introduced by
It seems like $this->dom->getElement(0) can also be of type Zenstruck\Browser\Dom; however, parameter $element of Facebook\WebDriver\WebDriverSelect::__construct() does only seem to accept Facebook\WebDriver\WebDriverElement, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

55
            (new WebDriverSelect(/** @scrutinizer ignore-type */ $this->dom->getElement(0)))->deselectAll();
Loading history...
Bug introduced by
The method getElement() does not exist on Zenstruck\Browser\Dom. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

55
            (new WebDriverSelect($this->dom->/** @scrutinizer ignore-call */ getElement(0)))->deselectAll();
Loading history...
56
        }
57
58
        try {
59
            $this->inner->setValue($value);
60
        } catch (NoSuchElementException $e) {
61
            // try selecting by visible text
62
            $select = new WebDriverSelect($this->dom->getElement(0));
63
64
            foreach ((array) $value as $item) {
65
                $select->selectByVisibleText($item);
66
            }
67
        }
68
    }
69
70
    /**
71
     * @param array|string $type
72
     */
73
    public function is($type): bool
74
    {
75
        return \in_array($this->getType(), (array) $type, true);
0 ignored issues
show
Bug introduced by
The method getType() does not exist on Zenstruck\Browser\Dom\Form\Field\ChoiceField. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

75
        return \in_array($this->/** @scrutinizer ignore-call */ getType(), (array) $type, true);
Loading history...
76
    }
77
}
78