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

AbstractViewHelper::overrideArgument()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 5
dl 0
loc 5
rs 10
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 Closure;
11
use TYPO3Fluid\Fluid\Component\AbstractComponent;
12
use TYPO3Fluid\Fluid\Component\Argument\ArgumentCollection;
13
use TYPO3Fluid\Fluid\Component\Argument\ArgumentDefinition;
14
use TYPO3Fluid\Fluid\Component\ComponentInterface;
15
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
16
17
/**
18
 * The abstract base class for all view helpers.
19
 */
20
abstract class AbstractViewHelper extends AbstractComponent
21
{
22
    /**
23
     * @var RenderingContextInterface
24
     */
25
    protected $renderingContext;
26
27
    protected $escapeOutput = true;
28
29
    /**
30
     * Execute via Component API implementation.
31
     *
32
     * @param RenderingContextInterface $renderingContext
33
     * @return mixed
34
     */
35
    public function evaluate(RenderingContextInterface $renderingContext)
36
    {
37
        $this->renderingContext = $renderingContext;
38
        $this->getArguments()->setRenderingContext($renderingContext);
39
        return $this->callRenderMethod();
0 ignored issues
show
Deprecated Code introduced by
The method TYPO3Fluid\Fluid\Core\Vi...per::callRenderMethod() has been deprecated with message: Will be removed and no longer called in Fluid 4.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
40
    }
41
42
    public function onOpen(RenderingContextInterface $renderingContext): ComponentInterface
43
    {
44
        $this->getArguments()->setRenderingContext($renderingContext);
45
        return parent::onOpen($renderingContext);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return parent::onOpen($renderingContext); (TYPO3Fluid\Fluid\Core\Vi...lper\AbstractViewHelper) is incompatible with the return type declared by the interface TYPO3Fluid\Fluid\Compone...ponentInterface::onOpen of type self.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
46
    }
47
48
    public function getArguments(): ArgumentCollection
49
    {
50
        if ($this->arguments === null) {
51
            $this->arguments = new ArgumentCollection();
52
            $this->initializeArguments();
53
        }
54
        return $this->arguments;
55
    }
56
57
    /**
58
     * Register a new argument. Call this method from your ViewHelper subclass
59
     * inside the initializeArguments() method.
60
     *
61
     * @param string $name Name of the argument
62
     * @param string $type Type of the argument
63
     * @param string $description Description of the argument
64
     * @param boolean $required If TRUE, argument is required. Defaults to FALSE.
65
     * @param mixed $defaultValue Default value of argument
66
     * @return AbstractViewHelper $this, to allow chaining.
67
     * @throws Exception
68
     */
69
    protected function registerArgument(string $name, string $type, string $description, bool $required = false, $defaultValue = null): self
70
    {
71
        $this->getArguments()->addDefinition(new ArgumentDefinition($name, $type, $description, $required, $defaultValue));
72
        return $this;
73
    }
74
75
    /**
76
     * Overrides a registered argument. Call this method from your ViewHelper subclass
77
     * inside the initializeArguments() method if you want to override a previously registered argument.
78
     *
79
     * @param string $name Name of the argument
80
     * @param string $type Type of the argument
81
     * @param string $description Description of the argument
82
     * @param boolean $required If TRUE, argument is required. Defaults to FALSE.
83
     * @param mixed $defaultValue Default value of argument
84
     * @return AbstractViewHelper $this, to allow chaining.
85
     * @throws Exception
86
     * @see registerArgument()
87
     * @deprecated Will be removed in Fluid 4.0
88
     */
89
    protected function overrideArgument(string $name, string $type, string $description, bool $required = false, $defaultValue = null): self
90
    {
91
        $this->getArguments()->addDefinition(new ArgumentDefinition($name, $type, $description, $required, $defaultValue));
92
        return $this;
93
    }
94
95
    /**
96
     * Call the render() method and handle errors.
97
     *
98
     * @return mixed the rendered ViewHelper
99
     * @throws Exception
100
     * @deprecated Will be removed and no longer called in Fluid 4.0
101
     */
102
    protected function callRenderMethod()
103
    {
104
        if (method_exists($this, 'render')) {
105
            return call_user_func([$this, 'render']);
106
        }
107
        if (method_exists($this, 'renderStatic')) {
108
            // Method is safe to call - will not recurse through ViewHelperInvoker via the default
109
            // implementation of renderStatic() on this class.
110
            return call_user_func_array([static::class, 'renderStatic'], [$this->arguments->getArrayCopy(), $this->buildRenderChildrenClosure(), $this->arguments->getRenderingContext()]);
0 ignored issues
show
Deprecated Code introduced by
The method TYPO3Fluid\Fluid\Core\Vi...RenderChildrenClosure() has been deprecated with message: Will be removed and not called in Fluid 4.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
111
        }
112
        return $this->renderChildren();
113
    }
114
115
    /**
116
     * Helper method which triggers the rendering of everything between the
117
     * opening and the closing tag.
118
     *
119
     * @return mixed The finally rendered child nodes.
120
     */
121
    protected function renderChildren()
122
    {
123
        return $this->evaluateChildren($this->renderingContext);
124
    }
125
126
    /**
127
     * Helper which is mostly needed when calling renderStatic() from within
128
     * render().
129
     *
130
     * No public API yet.
131
     *
132
     * @deprecated Will be removed and not called in Fluid 4.0
133
     * @return Closure
134
     */
135
    protected function buildRenderChildrenClosure()
136
    {
137
        $self = clone $this;
138
        $renderChildrenClosure = function () use ($self) {
139
            return $self->renderChildren();
140
        };
141
        return $renderChildrenClosure;
142
    }
143
144
    /**
145
     * Initialize all arguments. You need to override this method and call
146
     * $this->registerArgument(...) inside this method, to register all your arguments.
147
     *
148
     * @return void
149
     */
150
    protected function initializeArguments()
151
    {
152
    }
153
154
    public function allowUndeclaredArgument(string $argumentName): bool
155
    {
156
        return false;
157
    }
158
159
    protected function hasArgument(string $argumentName): bool
160
    {
161
        return $this->getArguments()->getRaw($argumentName) !== null;
162
    }
163
}
164