Passed
Push — master ( 34f20f...a12a6d )
by Enjoys
57s queued 12s
created

Container::elementExists()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 3
c 3
b 0
f 0
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 10
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Enjoys\Forms\Traits;
6
7
use Enjoys\Forms\Element;
8
use Enjoys\Forms\Elements;
9
use Enjoys\Forms\Interfaces\CaptchaInterface;
10
use Enjoys\Forms\Interfaces\ElementInterface;
11
use Webmozart\Assert\Assert;
12
13
/**
14
 * @method Elements\Text text(string $name, string $label = null)
15
 * @method Elements\Hidden hidden(string $name, string $value = null)
16
 * @method Elements\Password password(string $name, string $label = null)
17
 * @method Elements\Submit submit(string $name = null, string $title = null)
18
 * @method Elements\Header header(string $title = null)
19
 * @method Elements\Color color(string $name, string $label = null)
20
 * @method Elements\Date date(string $name, string $label = null)
21
 * @method Elements\Datetime datetime(string $name, string $label = null)
22
 * @method Elements\Datetimelocal datetimelocal(string $name, string $label = null)
23
 * @method Elements\Email email(string $name, string $label = null)
24
 * @method Elements\Number number(string $name, string $label = null)
25
 * @method Elements\Range range(string $name, string $label = null)
26
 * @method Elements\Search search(string $name, string $label = null)
27
 * @method Elements\Tel tel(string $name, string $label = null)
28
 * @method Elements\Time time(string $name, string $label = null)
29
 * @method Elements\Url url(string $name, string $label = null)
30
 * @method Elements\Month month(string $name, string $label = null)
31
 * @method Elements\Week week(string $name, string $label = null)
32
 * @method Elements\Textarea textarea(string $name, string $label = null)
33
 * @method Elements\Select select(string $name, string $label = null)
34
 * @method Elements\Button button(string $name, string $title = null)
35
 * @method Elements\Datalist datalist(string $name, string $label = null)
36
 * @method Elements\Checkbox checkbox(string $name, string $label = null)
37
 * @method Elements\Image image(string $name, string $src = null)
38
 * @method Elements\Radio radio(string $name, string $title = null)
39
 * @method Elements\Reset reset(string $name, string $title = null)
40
 * @method Elements\Captcha captcha(CaptchaInterface $captcha)
41
 * @method Elements\Group group(string $title = null)
42
 * @method Elements\File file(string $name, string $label = null)
43
 * @method Elements\Csrf csrf()
44
 * @method Elements\Html html(string $html)
45
 *
46
 * @author Enjoys
47
 */
48
trait Container
49
{
50
    /**
51
     * @var Element[]
52
     */
53
    private array $elements = [];
54
55
56 58
    public function __call(string $name, array $arguments): Element
57
    {
58 58
        $className = '\Enjoys\\Forms\\Elements\\' . ucfirst($name);
59 58
        Assert::classExists($className);
60
        /** @var class-string<ElementInterface> $className */
61 56
        $element = new $className(...$arguments);
62
63
        /** @var Element $element */
64 56
        $this->addElement($element);
65 56
        return $element;
66
    }
67
68
    /**
69
     * @param Element $element
70
     * @return $this
71
     * @noinspection PhpMissingReturnTypeInspection
72
     */
73 90
    public function addElement(Element $element)
74
    {
75 90
        $element->setRequest($this->getRequest());
0 ignored issues
show
Bug introduced by
$this->getRequest() of type Enjoys\Forms\Element is incompatible with the type Enjoys\ServerRequestWrapperInterface|null expected by parameter $request of Enjoys\Forms\Element::setRequest(). ( Ignorable by Annotation )

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

75
        $element->setRequest(/** @scrutinizer ignore-type */ $this->getRequest());
Loading history...
Bug introduced by
The method getRequest() does not exist on Enjoys\Forms\Traits\Container. 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
        $element->setRequest($this->/** @scrutinizer ignore-call */ getRequest());
Loading history...
76 90
        if ($element->prepare() === true) {
0 ignored issues
show
introduced by
The condition $element->prepare() === true is always true.
Loading history...
77 14
            return $this;
78
        }
79
80
81 90
        if ($element->isAllowSameNames() === false
82 90
            && false !== $key = $this->getElementKey($element)
83
        ) {
84 87
            $this->elements[$key] = $element;
85 87
            return $this;
86
        }
87
88
89 90
        $this->elements[] = $element;
90 90
        return $this;
91
    }
92
93
94
    /**
95
     * @return Element[]
96
     */
97 15
    public function getElements(): array
98
    {
99 15
        return $this->elements;
100
    }
101
102 19
    public function getElement(string $name): ?Element
103
    {
104 19
        if (false !== $key = $this->getElementKey($name)) {
105 19
            return $this->elements[$key];
106
        }
107
108 3
        return null;
109
    }
110
111
    /**
112
     * @param Element|null $element
113
     * @return $this
114
     * @noinspection PhpMissingReturnTypeInspection
115
     */
116 21
    public function removeElement(?Element $element = null)
117
    {
118 21
        if (null === $element) {
119 1
            return $this;
120
        }
121
122 21
        if (false !== $key = $this->getElementKey($element)) {
123 12
            unset($this->elements[$key]);
124
        }
125 21
        return $this;
126
    }
127
128 90
    private function getElementKey(string|Element $element): int|false
129
    {
130 90
        $name = ($element instanceof Element) ? $element->getName() : $element;
131
132 90
        foreach ($this->elements as $key => $el) {
133 90
            if ($el->getName() === $name) {
134
                /** @var int $key */
135 87
                return $key;
136
            }
137
        }
138 90
        return false;
139
    }
140
141
142
}
143