• 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

89.86
/Classes/ViewHelpers/AbstractFormViewHelper.php
1
<?php
2
declare(strict_types=1);
3
namespace FluidTYPO3\Flux\ViewHelpers;
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;
13
use FluidTYPO3\Flux\Form\Container\Grid;
14
use FluidTYPO3\Flux\Form\FormInterface;
15
use TYPO3\CMS\Core\Utility\VersionNumberUtility;
16
use TYPO3Fluid\Fluid\Component\Argument\ArgumentCollection;
17
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
18
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
19

20
/**
21
 * Base class for all FlexForm related ViewHelpers
22
 */
23
abstract class AbstractFormViewHelper extends AbstractViewHelper
24
{
25
    const SCOPE = FormViewHelper::class;
26
    const SCOPE_VARIABLE_EXTENSIONNAME = 'extensionName';
27
    const SCOPE_VARIABLE_FORM = 'form';
28
    const SCOPE_VARIABLE_CONTAINER = 'container';
29
    const SCOPE_VARIABLE_GRIDS = 'grids';
30

31
    protected function overrideArgument(
32
        $name,
33
        $type,
34
        $description,
35
        $required = false,
36
        $defaultValue = null,
37
        $escape = null
38
    ) {
39
        if (version_compare(VersionNumberUtility::getCurrentTypo3Version(), '13.4', '>=')) {
6✔
40
            return parent::registerArgument($name, $type, $description, $required, $defaultValue, $escape);
×
41
        }
42
        return parent::overrideArgument($name, $type, $description, $required, $defaultValue, $escape);
6✔
43
    }
44

45
    /**
46
     * @return string
47
     */
48
    public function render()
49
    {
50
        return static::renderStatic($this->arguments, $this->buildRenderChildrenClosure(), $this->renderingContext);
×
51
    }
52

53
    protected function callRenderMethod(): string
54
    {
55
        return static::renderStatic(
46✔
56
            $this->arguments instanceof ArgumentCollection ? $this->arguments->getArrayCopy() : $this->arguments,
46✔
57
            $this->buildRenderChildrenClosure(),
46✔
58
            $this->renderingContext
46✔
59
        );
46✔
60
    }
61

62
    public static function renderStatic(
63
        array $arguments,
64
        \Closure $renderChildrenClosure,
65
        RenderingContextInterface $renderingContext
66
    ): string {
67
        $container = static::getContainerFromRenderingContext($renderingContext);
38✔
68
        if (method_exists(static::class, 'getComponent')) {
38✔
69
            $component = static::getComponent($renderingContext, $arguments, $renderChildrenClosure);
38✔
70
            // rendering child nodes with Form's last sheet as active container
71
            static::setContainerInRenderingContext($renderingContext, $component);
37✔
72
        }
73
        $renderChildrenClosure();
37✔
74
        static::setContainerInRenderingContext($renderingContext, $container);
37✔
75

76
        return '';
37✔
77
    }
78

79
    public static function getComponent(
80
        RenderingContextInterface $renderingContext,
81
        iterable $arguments
82
    ): FormInterface {
83
        return Form::create();
1✔
84
    }
85

86
    /**
87
     * @return mixed
88
     */
89
    public function renderChildren()
90
    {
91
        // Make sure the current extension name always propagates to child nodes
92
        static::setExtensionNameInRenderingContext(
45✔
93
            $this->renderingContext,
45✔
94
            static::getExtensionNameFromRenderingContextOrArguments($this->renderingContext, $this->arguments)
45✔
95
        );
45✔
96

97
        return parent::renderChildren();
45✔
98
    }
99

100
    protected static function setExtensionNameInRenderingContext(
101
        RenderingContextInterface $renderingContext,
102
        string $name
103
    ): void {
104
        $renderingContext->getViewHelperVariableContainer()
45✔
105
            ->addOrUpdate(static::SCOPE, static::SCOPE_VARIABLE_EXTENSIONNAME, $name);
45✔
106
    }
107

108
    protected static function getExtensionNameFromRenderingContextOrArguments(
109
        RenderingContextInterface $renderingContext,
110
        array $arguments
111
    ): string {
112
        if ($extensionName = $arguments[static::SCOPE_VARIABLE_EXTENSIONNAME] ?? false) {
69✔
113
            return (string) $extensionName;
1✔
114
        }
115
        $viewHelperVariableContainer = $renderingContext->getViewHelperVariableContainer();
68✔
116
        if ($extensionName = $viewHelperVariableContainer->get(static::SCOPE, static::SCOPE_VARIABLE_EXTENSIONNAME)) {
68✔
117
            return is_scalar($extensionName) ? (string) $extensionName : 'FluidTYPO3.Flux';
14✔
118
        }
119
        $request = null;
68✔
120
        $controllerContext = null;
68✔
121
        if (method_exists($renderingContext, 'getControllerContext')) {
68✔
122
            $controllerContext = $renderingContext->getControllerContext();
68✔
123
            if ($controllerContext && $controllerContext->getRequest()) {
68✔
124
                $request = $controllerContext->getRequest();
68✔
125
            }
126
        } elseif (method_exists($renderingContext, 'getRequest')) {
×
127
            $request = $renderingContext->getRequest();
×
128
        }
129
        if (!$request && $controllerContext) {
68✔
130
            $request = $controllerContext->getRequest();
×
131
            /** @var string|null $controllerExtensionName */
132
            $controllerExtensionName = $request->getControllerExtensionName();
×
133
            return $controllerExtensionName ?? 'FluidTYPO3.Flux';
×
134
        }
135
        return 'FluidTYPO3.Flux';
68✔
136
    }
137

138
    public static function getFormFromRenderingContext(RenderingContextInterface $renderingContext): Form
139
    {
140
        /** @var Form|null $form */
141
        $form = $renderingContext->getViewHelperVariableContainer()->get(static::SCOPE, static::SCOPE_VARIABLE_FORM);
66✔
142
        if (!$form) {
66✔
143
            $form = Form::create([
57✔
144
                'extensionName' => $renderingContext->getViewHelperVariableContainer()->get(
57✔
145
                    FormViewHelper::SCOPE,
57✔
146
                    FormViewHelper::SCOPE_VARIABLE_EXTENSIONNAME
57✔
147
                )
57✔
148
            ]);
57✔
149
            $renderingContext->getViewHelperVariableContainer()->add(static::SCOPE, static::SCOPE_VARIABLE_FORM, $form);
57✔
150
        }
151
        return $form;
66✔
152
    }
153

154
    protected static function getGridFromRenderingContext(
155
        RenderingContextInterface $renderingContext,
156
        string $gridName = 'grid'
157
    ): Grid {
158
        $viewHelperVariableContainer = $renderingContext->getViewHelperVariableContainer();
13✔
159
        /** @var Grid[] $grids */
160
        $grids = (array) $viewHelperVariableContainer->get(static::SCOPE, static::SCOPE_VARIABLE_GRIDS);
13✔
161

162
        if (!isset($grids[$gridName])) {
13✔
163
            $grids[$gridName] = Grid::create(['name' => $gridName]);
12✔
164
            $viewHelperVariableContainer->addOrUpdate(static::SCOPE, static::SCOPE_VARIABLE_GRIDS, $grids);
12✔
165
        }
166
        /** @var Grid $grid */
167
        $grid = $grids[$gridName];
13✔
168
        return $grid;
13✔
169
    }
170

171
    protected static function getContainerFromRenderingContext(
172
        RenderingContextInterface $renderingContext
173
    ): FormInterface {
174
        /** @var Form\FormInterface|null $container */
175
        $container = $renderingContext->getViewHelperVariableContainer()->get(
72✔
176
            static::SCOPE,
72✔
177
            static::SCOPE_VARIABLE_CONTAINER
72✔
178
        );
72✔
179
        return $container ?? static::getFormFromRenderingContext($renderingContext);
72✔
180
    }
181

182
    protected static function setContainerInRenderingContext(
183
        RenderingContextInterface $renderingContext,
184
        FormInterface $container
185
    ): void {
186
        $renderingContext->getViewHelperVariableContainer()->addOrUpdate(
40✔
187
            static::SCOPE,
40✔
188
            static::SCOPE_VARIABLE_CONTAINER,
40✔
189
            $container
40✔
190
        );
40✔
191
    }
192
}
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