RenderViewHelper   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 9
dl 0
loc 63
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A initializeArguments() 0 5 1
A render() 0 6 1
A renderStatic() 0 31 4
1
<?php
2
/*
3
 * 2017 Romain CANON <[email protected]>
4
 *
5
 * This file is part of the TYPO3 FormZ project.
6
 * It is free software; you can redistribute it and/or modify it
7
 * under the terms of the GNU General Public License, either
8
 * version 3 of the License, or any later version.
9
 *
10
 * For the full copyright and license information, see:
11
 * http://www.gnu.org/licenses/gpl-3.0.html
12
 */
13
14
namespace Romm\Formz\ViewHelpers\Slot;
15
16
use Closure;
17
use Romm\Formz\Core\Core;
18
use Romm\Formz\Exceptions\ContextNotFoundException;
19
use Romm\Formz\Service\ViewHelper\Field\FieldViewHelperService;
20
use Romm\Formz\Service\ViewHelper\Slot\SlotViewHelperService;
21
use Romm\Formz\ViewHelpers\AbstractViewHelper;
22
use TYPO3\CMS\Core\Utility\ArrayUtility;
23
use TYPO3\CMS\Core\Utility\VersionNumberUtility;
24
use TYPO3\CMS\Fluid\Core\Rendering\RenderingContextInterface;
25
use TYPO3\CMS\Fluid\Core\ViewHelper\Facets\CompilableInterface;
26
27
/**
28
 * This is the rendering function for the `slot` view helper.
29
 *
30
 * @see \Romm\Formz\ViewHelpers\SlotViewHelper
31
 */
32
class RenderViewHelper extends AbstractViewHelper implements CompilableInterface
33
{
34
    /**
35
     * @var bool
36
     */
37
    protected $escapeOutput = false;
38
39
    /**
40
     * @inheritdoc
41
     */
42
    public function initializeArguments()
43
    {
44
        $this->registerArgument('slot', 'string', 'Instance of the slot which will be rendered.', true);
45
        $this->registerArgument('arguments', 'array', 'Arguments sent to the slot.', false, []);
46
    }
47
48
    /**
49
     * @inheritdoc
50
     */
51
    public function render()
52
    {
53
        parent::initializeArguments();
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (initializeArguments() instead of render()). Are you sure this is correct? If so, you might want to change this to $this->initializeArguments().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
54
55
        return self::renderStatic($this->arguments, $this->buildRenderChildrenClosure(), $this->renderingContext);
0 ignored issues
show
Compatibility introduced by
$this->renderingContext of type object<TYPO3Fluid\Fluid\...deringContextInterface> is not a sub-type of object<TYPO3\CMS\Fluid\C...deringContextInterface>. It seems like you assume a child interface of the interface TYPO3Fluid\Fluid\Core\Re...nderingContextInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
56
    }
57
58
    /**
59
     * Will render the slot with the given name, only if the slot is found.
60
     *
61
     * @inheritdoc
62
     */
63
    public static function renderStatic(array $arguments, Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
64
    {
65
        /** @var FieldViewHelperService $fieldService */
66
        $fieldService = Core::instantiate(FieldViewHelperService::class);
67
68
        if (false === $fieldService->fieldContextExists()) {
69
            throw ContextNotFoundException::slotRenderViewHelperFieldContextNotFound();
70
        }
71
72
        /** @var SlotViewHelperService $slotService */
73
        $slotService = Core::instantiate(SlotViewHelperService::class);
74
        $slotName = $arguments['slot'];
75
        $result = '';
76
77
        if ($slotService->hasSlot($slotName)) {
78
            $currentVariables = version_compare(VersionNumberUtility::getCurrentTypo3Version(), '8.0.0', '<')
79
                ? $renderingContext->getTemplateVariableContainer()->getAll()
0 ignored issues
show
Bug introduced by
The method getTemplateVariableContainer() does not seem to exist on object<TYPO3\CMS\Fluid\C...deringContextInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
80
                : $renderingContext->getVariableProvider()->getAll();
81
82
            ArrayUtility::mergeRecursiveWithOverrule($currentVariables, $arguments['arguments']);
83
84
            $slotService->addTemplateVariables($slotName, $currentVariables);
85
86
            $slotClosure = $slotService->getSlotClosure($slotName);
87
            $result = $slotClosure();
88
89
            $slotService->restoreTemplateVariables($slotName);
90
        }
91
92
        return $result;
93
    }
94
}
95