Completed
Pull Request — master (#446)
by Claus
02:04
created

SectionViewHelper   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 71
Duplicated Lines 9.86 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 7
loc 71
rs 10
c 0
b 0
f 0
wmc 8
lcom 1
cbo 2

4 Methods

Rating   Name   Duplication   Size   Complexity  
A initializeArguments() 0 4 1
A postParseEvent() 0 9 2
A allowsChildNodeType() 7 7 3
A render() 0 9 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
namespace TYPO3Fluid\Fluid\ViewHelpers;
3
4
/*
5
 * This file belongs to the package "TYPO3 Fluid".
6
 * See LICENSE.txt that was shipped with this package.
7
 */
8
9
use TYPO3Fluid\Fluid\Core\Parser\NodeFilterInterface;
10
use TYPO3Fluid\Fluid\Core\Parser\SyntaxTree\NodeInterface;
11
use TYPO3Fluid\Fluid\Core\Parser\SyntaxTree\TextNode;
12
use TYPO3Fluid\Fluid\Core\Parser\SyntaxTree\ViewHelperNode;
13
use TYPO3Fluid\Fluid\Core\Variables\VariableProviderInterface;
14
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
15
use TYPO3Fluid\Fluid\Core\ViewHelper\TemplateVariableContainer;
16
use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\ParserRuntimeOnly;
17
18
/**
19
 * A ViewHelper to declare sections in templates for later use with e.g. the RenderViewHelper.
20
 *
21
 * = Examples =
22
 *
23
 * <code title="Rendering sections">
24
 * <f:section name="someSection">This is a section. {foo}</f:section>
25
 * <f:render section="someSection" arguments="{foo: someVariable}" />
26
 * </code>
27
 * <output>
28
 * the content of the section "someSection". The content of the variable {someVariable} will be available in the partial as {foo}
29
 * </output>
30
 *
31
 * <code title="Rendering recursive sections">
32
 * <f:section name="mySection">
33
 *  <ul>
34
 *    <f:for each="{myMenu}" as="menuItem">
35
 *      <li>
36
 *        {menuItem.text}
37
 *        <f:if condition="{menuItem.subItems}">
38
 *          <f:render section="mySection" arguments="{myMenu: menuItem.subItems}" />
39
 *        </f:if>
40
 *      </li>
41
 *    </f:for>
42
 *  </ul>
43
 * </f:section>
44
 * <f:render section="mySection" arguments="{myMenu: menu}" />
45
 * </code>
46
 * <output>
47
 * <ul>
48
 *   <li>menu1
49
 *     <ul>
50
 *       <li>menu1a</li>
51
 *       <li>menu1b</li>
52
 *     </ul>
53
 *   </li>
54
 * [...]
55
 * (depending on the value of {menu})
56
 * </output>
57
 *
58
 * @api
59
 */
60
class SectionViewHelper extends AbstractViewHelper implements NodeFilterInterface
61
{
62
    use ParserRuntimeOnly;
63
64
    /**
65
     * @var boolean
66
     */
67
    protected $escapeOutput = false;
68
69
    /**
70
     * Initialize the arguments.
71
     *
72
     * @return void
73
     * @api
74
     */
75
    public function initializeArguments()
76
    {
77
        $this->registerArgument('name', 'string', 'Name of the section', true);
78
    }
79
80
    /**
81
     * Save the associated ViewHelper node in a static public class variable.
82
     * called directly after the ViewHelper was built.
83
     *
84
     * @param ViewHelperNode $node
85
     * @param TextNode[] $arguments
86
     * @param VariableProviderInterface $variableContainer
87
     * @return void
88
     */
89
    public static function postParseEvent(ViewHelperNode $node, array $arguments, VariableProviderInterface $variableContainer)
90
    {
91
        /** @var $nameArgument TextNode */
92
        $nameArgument = $arguments['name'];
93
        $sectionName = $nameArgument->getText();
94
        $sections = $variableContainer['1457379500_sections'] ? $variableContainer['1457379500_sections'] : [];
95
        $sections[$sectionName] = $node;
96
        $variableContainer['1457379500_sections'] = $sections;
97
    }
98
99
    /**
100
     * Section ViewHelper disallows any whitespace-only text nodes as children. The result is that
101
     * the section wrapping itself becomes "invisible" and the output of the section when rendered
102
     * does not include the whitespace leading up to or trailing any "real" child content.
103
     *
104
     * @param NodeInterface $node
105
     * @return bool
106
     */
107 View Code Duplication
    public function allowsChildNodeType(NodeInterface $node): bool
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
108
    {
109
        if ($node instanceof TextNode && trim($node->getText()) === '') {
110
            return false;
111
        }
112
        return true;
113
    }
114
115
    /**
116
     * Rendering directly returns all child nodes.
117
     *
118
     * @return string HTML String of all child nodes.
119
     * @api
120
     */
121
    public function render()
122
    {
123
        $content = '';
124
        if ($this->viewHelperVariableContainer->exists(SectionViewHelper::class, 'isCurrentlyRenderingSection')) {
125
            $this->viewHelperVariableContainer->remove(SectionViewHelper::class, 'isCurrentlyRenderingSection');
126
            $content = $this->renderChildren();
127
        }
128
        return $content;
129
    }
130
}
131