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

Field   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 20
dl 0
loc 45
rs 10
c 1
b 0
f 0
wmc 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 18 6
A __call() 0 7 2
A attr() 0 3 1
A __construct() 0 5 1
1
<?php
2
3
namespace Zenstruck\Browser\Dom\Form;
4
5
use Symfony\Component\DomCrawler\Field\ChoiceFormField;
6
use Symfony\Component\DomCrawler\Field\FileFormField;
7
use Symfony\Component\DomCrawler\Field\FormField;
8
use Symfony\Component\DomCrawler\Form;
9
use Zenstruck\Browser\Dom;
10
use Zenstruck\Browser\Dom\Form\Field\ChoiceField;
11
use Zenstruck\Browser\Dom\Form\Field\FileField;
12
use Zenstruck\Browser\Dom\Form\Field\InputField;
13
14
/**
15
 * @author Kevin Bond <[email protected]>
16
 */
17
abstract class Field
18
{
19
    protected FormField $inner;
20
    protected Dom $dom;
21
    protected Form $form;
22
23
    public function __construct(FormField $inner, Dom $dom, Form $form)
24
    {
25
        $this->inner = $inner;
26
        $this->dom = $dom;
27
        $this->form = $form;
28
    }
29
30
    public function __call(string $name, array $arguments)
31
    {
32
        if (!\method_exists($this->inner, $name)) {
33
            throw new \BadMethodCallException(\sprintf('Method "%s" does not exist on "%s".', $name, \get_class($this->inner)));
34
        }
35
36
        return $this->inner->{$name}(...$arguments);
37
    }
38
39
    public static function create(Dom $dom): self
40
    {
41
        $form = $dom->form();
42
        $field = $form->get(\str_replace('[]', '', $dom->attr('name')));
0 ignored issues
show
Bug introduced by
The method attr() 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

42
        $field = $form->get(\str_replace('[]', '', $dom->/** @scrutinizer ignore-call */ attr('name')));
Loading history...
43
44
        if (\is_array($field) && isset($field[0]) && $field[0] instanceof FileFormField) {
45
            $field = $field[0];
46
        }
47
48
        if ($field instanceof FileFormField) {
49
            return new FileField($field, $dom, $form);
50
        }
51
52
        if ($field instanceof ChoiceFormField) {
53
            return new ChoiceField($field, $dom, $form);
54
        }
55
56
        return new InputField($field, $dom, $form);
57
    }
58
59
    public function attr(string $name): ?string
60
    {
61
        return $this->dom->attr($name);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->dom->attr($name) could return the type Zenstruck\Browser\Dom which is incompatible with the type-hinted return null|string. Consider adding an additional type-check to rule them out.
Loading history...
62
    }
63
}
64