• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

FluidTYPO3 / flux / 15918415903

20 May 2025 10:36AM UTC coverage: 91.109% (-2.1%) from 93.21%
15918415903

push

github

NamelessCoder
[TASK] Lock phpstan version

6927 of 7603 relevant lines covered (91.11%)

9.53 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.05
/Classes/Form/AbstractFormField.php
1
<?php
2
declare(strict_types=1);
3
namespace FluidTYPO3\Flux\Form;
4

5
/*
6
 * This file is part of the FluidTYPO3/Flux project under GPLv2 or later.
7
 *
8
 * For the full copyright and license information, please read the
9
 * LICENSE.md file that was distributed with this source code.
10
 */
11

12
use FluidTYPO3\Flux\Form\Container\Section;
13
use FluidTYPO3\Flux\UserFunction\ClearValueWizard;
14
use TYPO3\CMS\Core\Utility\GeneralUtility;
15
use TYPO3\CMS\Core\Utility\VersionNumberUtility;
16

17
abstract class AbstractFormField extends AbstractFormComponent implements FieldInterface
18
{
19
    /**
20
     * @var mixed
21
     */
22
    protected $default;
23

24
    /**
25
     * Display condition - see https://docs.typo3.org/typo3cms/TCAReference/Reference/Columns/Index.html#displaycond
26
     *
27
     * @var array|string|null
28
     */
29
    protected $displayCondition = null;
30

31
    protected bool $native = false;
32
    protected bool $required = false;
33
    protected bool $requestUpdate = false;
34
    protected bool $inherit = true;
35
    protected bool $inheritEmpty = false;
36
    protected bool $clearable = false;
37
    protected bool $protectable = false;
38
    protected bool $exclude = false;
39
    protected ?string $validate = null;
40
    protected ?string $position = null;
41
    protected array $config = [];
42

43
    public static function create(array $settings = []): FormInterface
44
    {
45
        if (!isset($settings['type'])) {
81✔
46
            $settings['type'] = static::class;
1✔
47
        }
48
        if ('Section' === $settings['type']) {
81✔
49
            return Section::create($settings);
19✔
50
        } else {
51
            $prefix = AbstractFormComponent::NAMESPACE_FIELD . '\\';
62✔
52
            $type = $settings['type'];
62✔
53
            $className = str_replace('/', '\\', $type);
62✔
54
            $className = class_exists($prefix . $className) ? $prefix . $className : $className;
62✔
55
        }
56
        if (!class_exists($className)) {
62✔
57
            $className = $settings['type'];
19✔
58
        }
59
        if (!class_exists($className)) {
62✔
60
            throw new \RuntimeException(
19✔
61
                sprintf(
19✔
62
                    'Invalid class- or type-name used in type of field "%s"; "%s" is invalid',
19✔
63
                    $settings['name'] ?? '(unknown)',
19✔
64
                    $className
19✔
65
                ),
19✔
66
                1375373527
19✔
67
            );
19✔
68
        }
69
        /** @var FieldInterface $object */
70
        $object = GeneralUtility::makeInstance($className);
43✔
71
        foreach ($settings as $settingName => $settingValue) {
43✔
72
            $setterMethodName = 'set' . ucfirst($settingName);
43✔
73
            if (true === method_exists($object, $setterMethodName)) {
43✔
74
                $object->{$setterMethodName}($settingValue);
40✔
75
            }
76
        }
77
        return $object;
43✔
78
    }
79

80
    /**
81
     * Creates a TCEforms configuration array based on the
82
     * configuration stored in this ViewHelper. Calls the
83
     * expected-to-be-overridden stub method getConfiguration()
84
     * to return the TCE field configuration - see that method
85
     * for information about how to implement that method.
86
     */
87
    public function build(): array
88
    {
89
        if (!$this->getEnabled()) {
66✔
90
            return [];
19✔
91
        }
92

93
        // The "config" section consists of whichever configuration arry the component built, but with
94
        // priority to any options set directly as raw TCA field config options in $this->config.
95
        $configuration = array_replace($this->buildConfiguration(), $this->getConfig());
47✔
96
        $filterClosure = function ($value) {
47✔
97
            return $value !== null && $value !== '' && $value !== [];
46✔
98
        };
47✔
99
        $configuration = array_filter($configuration, $filterClosure);
47✔
100
        $fieldStructureArray = [
47✔
101
            'label' => $this->getLabel(),
47✔
102
            'exclude' => intval($this->getExclude()),
47✔
103
            'config' => $configuration
47✔
104
        ];
47✔
105
        if (($displayCondition = $this->getDisplayCondition())) {
47✔
106
            $fieldStructureArray['displayCond'] = $displayCondition;
1✔
107
        }
108

109
        if ($this->getClearable()) {
47✔
110
            $fieldStructureArray['config']['fieldWizard']['fluxClearValue'] = [
19✔
111
                'renderType' => 'fluxClearValue',
19✔
112
            ];
19✔
113
        }
114

115
        if ($this->getProtectable() && $this->getInherit()) {
47✔
116
            $fieldStructureArray['config']['fieldWizard']['fluxProtectValue'] = [
×
117
                'renderType' => 'fluxProtectValue',
×
118
            ];
×
119
        }
120

121
        if ($this->getRequestUpdate()) {
47✔
122
            $fieldStructureArray['onChange'] = 'reload';
5✔
123
        }
124
        return $fieldStructureArray;
47✔
125
    }
126

127
    protected function prepareConfiguration(string $type): array
128
    {
129
        $config = [
48✔
130
            'type' => $type,
48✔
131
            'transform' => $this->getTransform(),
48✔
132
            'default' => $this->getDefault(),
48✔
133
        ];
48✔
134
        if (version_compare(VersionNumberUtility::getCurrentTypo3Version(), '12.0', '>=')
48✔
135
            && $this->getRequired()
48✔
136
        ) {
137
            $config['required'] = $this->getRequired();
×
138
        }
139
        return $config;
48✔
140
    }
141

142
    public function isNative(): bool
143
    {
144
        return $this->native;
7✔
145
    }
146

147
    public function setNative(bool $native): self
148
    {
149
        $this->native = $native;
43✔
150
        return $this;
43✔
151
    }
152

153
    public function setRequired(bool $required): self
154
    {
155
        $this->required = $required;
48✔
156
        return $this;
48✔
157
    }
158

159
    public function getRequired(): bool
160
    {
161
        return $this->required;
20✔
162
    }
163

164
    /**
165
     * @param mixed $default
166
     */
167
    public function setDefault($default): self
168
    {
169
        $this->default = $default;
89✔
170
        return $this;
89✔
171
    }
172

173
    /**
174
     * @return mixed
175
     */
176
    public function getDefault()
177
    {
178
        return $this->default;
55✔
179
    }
180

181
    /**
182
     * @param string|array|null $displayCondition
183
     */
184
    public function setDisplayCondition($displayCondition): self
185
    {
186
        $this->displayCondition = $displayCondition;
47✔
187
        return $this;
47✔
188
    }
189

190
    /**
191
     * @return string|array|null
192
     */
193
    public function getDisplayCondition()
194
    {
195
        return $this->displayCondition;
48✔
196
    }
197

198
    public function setRequestUpdate(bool $requestUpdate): self
199
    {
200
        $this->requestUpdate = $requestUpdate;
91✔
201
        return $this;
91✔
202
    }
203

204
    public function getRequestUpdate(): bool
205
    {
206
        return $this->requestUpdate;
52✔
207
    }
208

209
    public function setExclude(bool $exclude): self
210
    {
211
        $this->exclude = $exclude;
44✔
212
        return $this;
44✔
213
    }
214

215
    public function getExclude(): bool
216
    {
217
        return $this->exclude;
48✔
218
    }
219

220
    public function setValidate(?string $validate): self
221
    {
222
        $this->validate = $validate;
61✔
223
        return $this;
61✔
224
    }
225

226
    public function getConfig(): array
227
    {
228
        return $this->config;
48✔
229
    }
230

231
    public function setConfig(array $config): self
232
    {
233
        $this->config = $config;
46✔
234
        return $this;
46✔
235
    }
236

237
    public function getValidate(): ?string
238
    {
239
        if (version_compare(VersionNumberUtility::getCurrentTypo3Version(), '12.0', '>=')) {
20✔
240
            return $this->validate;
×
241
        }
242

243
        if (!$this->getRequired()) {
20✔
244
            $validate = $this->validate;
16✔
245
        } else {
246
            if (empty($this->validate)) {
4✔
247
                $validate = 'required';
2✔
248
            } else {
249
                $validators = GeneralUtility::trimExplode(',', $this->validate);
2✔
250
                array_push($validators, 'required');
2✔
251
                $validate = implode(',', $validators);
2✔
252
            }
253
        }
254
        return $validate;
20✔
255
    }
256

257
    public function getPosition(): ?string
258
    {
259
        return $this->position;
1✔
260
    }
261

262
    public function setPosition(?string $position): self
263
    {
264
        $this->position = $position;
43✔
265
        return $this;
43✔
266
    }
267

268
    public function setClearable(bool $clearable): self
269
    {
270
        $this->clearable = (boolean) $clearable;
83✔
271
        return $this;
83✔
272
    }
273

274
    public function getClearable(): bool
275
    {
276
        return $this->clearable;
69✔
277
    }
278

279
    public function getProtectable(): bool
280
    {
281
        return $this->protectable;
47✔
282
    }
283

284
    public function setProtectable(bool $protectable): self
285
    {
286
        $this->protectable = $protectable;
44✔
287
        return $this;
44✔
288
    }
289
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc