Issues (35)

src/Traits/Container.php (3 issues)

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, null|string $label = null)
15
 * @method Elements\Hidden hidden(string $name, null|string $value = null)
16
 * @method Elements\Password password(string $name, null|string $label = null)
17
 * @method Elements\Submit submit(null|string $name = null, null|string $title = null)
18
 * @method Elements\Header header(null|string $title = null)
19
 * @method Elements\Color color(string $name, null|string $label = null)
20
 * @method Elements\Date date(string $name, null|string $label = null)
21
 * @method Elements\Datetime datetime(string $name, null|string $label = null)
22
 * @method Elements\Datetimelocal datetimelocal(string $name, null|string $label = null)
23
 * @method Elements\Email email(string $name, null|string $label = null)
24
 * @method Elements\Number number(string $name, null|string $label = null)
25
 * @method Elements\Range range(string $name, null|string $label = null)
26
 * @method Elements\Search search(string $name, null|string $label = null)
27
 * @method Elements\Tel tel(string $name, null|string $label = null)
28
 * @method Elements\Time time(string $name, null|string $label = null)
29
 * @method Elements\Url url(string $name, null|string $label = null)
30
 * @method Elements\Month month(string $name, null|string $label = null)
31
 * @method Elements\Week week(string $name, null|string $label = null)
32
 * @method Elements\Textarea textarea(string $name, null|string $label = null)
33
 * @method Elements\Select select(string $name, null|string $label = null)
34
 * @method Elements\Button button(string $name, null|string $title = null)
35
 * @method Elements\Datalist datalist(string $name, null|string $label = null)
36
 * @method Elements\Checkbox checkbox(string $name, null|string $label = null)
37
 * @method Elements\Image image(string $name, null|string $src = null)
38
 * @method Elements\Radio radio(string $name, null|string $title = null)
39
 * @method Elements\Reset reset(string $name, null|string $title = null)
40
 * @method Elements\Captcha captcha(CaptchaInterface $captcha)
41
 * @method Elements\Group group(null|string $title = null)
42
 * @method Elements\File file(string $name, null|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 62
    public function __call(string $name, array $arguments): Element
57
    {
58 62
        $className = '\Enjoys\\Forms\\Elements\\' . ucfirst($name);
59 62
        Assert::classExists($className);
60
        /** @var class-string<ElementInterface> $className */
61 60
        $element = new $className(...$arguments);
62
63
        /** @var Element $element */
64 60
        $this->addElement($element);
65 60
        return $element;
66
    }
67
68
    /**
69
     * @psalm-suppress MixedPropertyTypeCoercion
70
     */
71 96
    public function addElement(Element $element, ?string $before = null, ?string $after = null): static
72
    {
73 96
        $element->setRequest($this->getRequest());
0 ignored issues
show
$this->getRequest() of type Enjoys\Forms\Element is incompatible with the type Psr\Http\Message\ServerRequestInterface|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

73
        $element->setRequest(/** @scrutinizer ignore-type */ $this->getRequest());
Loading history...
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

73
        $element->setRequest($this->/** @scrutinizer ignore-call */ getRequest());
Loading history...
74 96
        if ($element->prepare() === true) {
0 ignored issues
show
The condition $element->prepare() === true is always false.
Loading history...
75 18
            return $this;
76
        }
77
78
79
        if (
80 96
            $element->isAllowSameNames() === false
81 96
            && false !== $key = $this->getElementKey($element)
82
        ) {
83 93
            $this->elements[$key] = $element;
84 93
            return $this;
85
        }
86
87 96
        if ($after !== null) {
88 2
            $this->elements = array_insert_after($this->elements, $this->getElementKey($after), $element);
89 2
            return $this;
90
        }
91
92 96
        if ($before !== null) {
93 1
            $this->elements = array_insert_before($this->elements, $this->getElementKey($before), $element);
94 1
            return $this;
95
        }
96
97 96
        $this->elements[] = $element;
98 96
        return $this;
99
    }
100
101
102
    /**
103
     * @return Element[]
104
     */
105 18
    public function getElements(): array
106
    {
107 18
        return $this->elements;
108
    }
109
110 20
    public function getElement(string $name): ?Element
111
    {
112 20
        if (false !== $key = $this->getElementKey($name)) {
113 20
            return $this->elements[$key];
114
        }
115
116 3
        return null;
117
    }
118
119 25
    public function removeElement(?Element $element = null): static
120
    {
121 25
        if (null === $element) {
122 1
            return $this;
123
        }
124
125 25
        if (false !== $key = $this->getElementKey($element)) {
126 12
            unset($this->elements[$key]);
127
        }
128 25
        return $this;
129
    }
130
131 96
    private function getElementKey(string|Element $element): int|false
132
    {
133 96
        $name = ($element instanceof Element) ? $element->getName() : $element;
134
135 96
        foreach ($this->elements as $key => $el) {
136 96
            if ($el->getName() === $name) {
137
                /** @var int $key */
138 93
                return $key;
139
            }
140
        }
141 96
        return false;
142
    }
143
}
144