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

EntryNode::onOpen()   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 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
namespace TYPO3Fluid\Fluid\Core\Parser\SyntaxTree;
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\AbstractComponent;
11
use TYPO3Fluid\Fluid\Component\ComponentInterface;
12
use TYPO3Fluid\Fluid\Component\EmbeddedComponentInterface;
13
use TYPO3Fluid\Fluid\Component\TransparentComponentInterface;
14
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
15
16
/**
17
 * Entry node. Used to represent either the root
18
 * entry point of a template tree, or a section
19
 * within the template tree. Common for EntryNode
20
 * is that:
21
 *
22
 * - It supports arguments that become variables
23
 * - It supports setting the name of the node which
24
 *   allows it to be returned from getNamedChild.
25
 * - It is an EmbeddedComponent which means that it
26
 *   is only rendered when explicitly calling the
27
 *   evaluate() method on the instance itself, it
28
 *   is not rendered when rendering the parent node.
29
 *
30
 * EntryNode instances can be resolved directly
31
 * from getTypedChildren to extract all sections
32
 * within a tree; or getNamedChild can be used
33
 * to extract an EntryNode (or an EntryNode child
34
 * of another EntryNode to any nesting depth by
35
 * using dotted path as argument for getNamedChild).
36
 */
37
class EntryNode extends AbstractComponent implements EmbeddedComponentInterface, TransparentComponentInterface
38
{
39
    protected $escapeOutput = false;
40
41
    public function setName(?string $name): ComponentInterface
42
    {
43
        $this->name = $name;
44
        return $this;
45
    }
46
47
    public function evaluate(RenderingContextInterface $renderingContext)
48
    {
49
        $renderingContext = clone $renderingContext;
50
        $renderingContext->setVariableProvider(
51
            $renderingContext->getVariableProvider()->getScopeCopy(
52
                $this->getArguments()->setRenderingContext($renderingContext)->getArrayCopy()
53
                + $renderingContext->getVariableProvider()->getAll()
54
            )
55
        );
56
        return parent::evaluate($renderingContext);
57
    }
58
59
    public function onOpen(RenderingContextInterface $renderingContext): ComponentInterface
60
    {
61
        $this->getArguments()->setRenderingContext($renderingContext)->validate();
62
        return parent::onOpen($renderingContext);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return parent::onOpen($renderingContext); (TYPO3Fluid\Fluid\Core\Parser\SyntaxTree\EntryNode) 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...
63
    }
64
}
65