Completed
Pull Request — master (#470)
by Claus
01:32
created

AbstractConditionViewHelper::renderElseChild()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
nc 5
nop 0
dl 0
loc 19
rs 9.3222
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
namespace TYPO3Fluid\Fluid\Core\ViewHelper;
4
5
/*
6
 * This file belongs to the package "TYPO3 Fluid".
7
 * See LICENSE.txt that was shipped with this package.
8
 */
9
10
use TYPO3Fluid\Fluid\Component\ComponentInterface;
11
use TYPO3Fluid\Fluid\ViewHelpers\ElseViewHelper;
12
use TYPO3Fluid\Fluid\ViewHelpers\ThenViewHelper;
13
14
/**
15
 * This view helper is an abstract ViewHelper which implements an if/else condition.
16
 *
17
 * = Usage =
18
 *
19
 * To create a custom Condition ViewHelper, you need to subclass this class, and
20
 * implement your own render() method. Inside there, you should call $this->renderThenChild()
21
 * if the condition evaluated to TRUE, and $this->renderElseChild() if the condition evaluated
22
 * to FALSE.
23
 *
24
 * Every Condition ViewHelper has a "then" and "else" argument, so it can be used like:
25
 * <[aConditionViewHelperName] .... then="condition true" else="condition false" />,
26
 * or as well use the "then" and "else" child nodes.
27
 *
28
 * @see \TYPO3Fluid\Fluid\ViewHelpers\IfViewHelper for a more detailed explanation and a simple usage example.
29
 * Make sure to NOT OVERRIDE the constructor.
30
 */
31
abstract class AbstractConditionViewHelper extends AbstractViewHelper
32
{
33
    protected $escapeOutput = false;
34
35
    /**
36
     * @return void
37
     */
38
    public function initializeArguments()
39
    {
40
        $this->registerArgument('then', 'mixed', 'Value to be returned if the condition if met.');
41
        $this->registerArgument('else', 'mixed', 'Value to be returned if the condition if not met.');
42
    }
43
44
    /**
45
     * Renders <f:then> child if $condition is true, otherwise renders <f:else> child.
46
     * Method which only gets called if the template is not compiled. For static calling,
47
     * the then/else nodes are converted to closures and condition evaluation closures.
48
     *
49
     * @return mixed
50
     */
51
    public function render()
52
    {
53
        if ($this->condition()) {
54
            return $this->renderThenChild();
55
        }
56
        return $this->renderElseChild();
57
    }
58
59
    /**
60
     * Override this method for a custom decision whether or not to render the ViewHelper
61
     *
62
     * @return bool
63
     */
64
    protected function condition(): bool
65
    {
66
        return true;
67
    }
68
69
    /**
70
     * Returns value of "then" attribute.
71
     * If then attribute is not set, iterates through child nodes and renders ThenViewHelper.
72
     * If then attribute is not set and no ThenViewHelper and no ElseViewHelper is found, all child nodes are rendered
73
     *
74
     * @return mixed rendered ThenViewHelper or contents of <f:if> if no ThenViewHelper was found
75
     */
76
    protected function renderThenChild()
77
    {
78
        if ($this->hasArgument('then')) {
79
            return $this->arguments['then'];
80
        }
81
        $elseViewHelperEncountered = false;
82
        foreach ($this->getChildren() as $childNode) {
83
            if ($childNode instanceof ThenViewHelper) {
84
                $data = $childNode->evaluate($this->renderingContext);
85
                return $data;
86
            }
87
            if ($childNode instanceof ElseViewHelper) {
88
                $elseViewHelperEncountered = true;
89
            }
90
        }
91
92
        if ($elseViewHelperEncountered) {
93
            return null;
94
        }
95
96
        return $this->evaluateChildren($this->renderingContext);
97
    }
98
99
    /**
100
     * Returns value of "else" attribute.
101
     * If else attribute is not set, iterates through child nodes and renders ElseViewHelper.
102
     * If else attribute is not set and no ElseViewHelper is found, an empty string will be returned.
103
     *
104
     * @return string rendered ElseViewHelper or an empty string if no ThenViewHelper was found
105
     */
106
    protected function renderElseChild()
107
    {
108
        if ($this->hasArgument('else')) {
109
            return $this->arguments['else'];
110
        }
111
112
        /** @var ComponentInterface|null $elseNode */
113
        $elseNode = null;
0 ignored issues
show
Unused Code introduced by
$elseNode is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
114
        foreach ($this->getChildren() as $childNode) {
115
            if ($childNode instanceof ElseViewHelper) {
116
                $conditionArgument = $childNode->getArguments()->setRenderingContext($this->renderingContext)['if'];
117
                if ($conditionArgument) {
118
                    return $childNode->evaluate($this->renderingContext);
119
                }
120
            }
121
        }
122
123
        return null;
124
    }
125
}
126