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

EntryNode   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setName() 0 5 1
A evaluate() 0 10 1
A onClose() 0 28 5
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\Argument\ArgumentDefinition;
12
use TYPO3Fluid\Fluid\Component\ComponentInterface;
13
use TYPO3Fluid\Fluid\Component\EmbeddedComponentInterface;
14
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
15
use TYPO3Fluid\Fluid\ViewHelpers\ExtendViewHelper;
16
use TYPO3Fluid\Fluid\ViewHelpers\ParameterViewHelper;
17
18
/**
19
 * Entry node. Used to represent either the root
20
 * entry point of a template tree, or a section
21
 * within the template tree. Common for EntryNode
22
 * is that:
23
 *
24
 * - It supports arguments that become variables
25
 * - It supports setting the name of the node which
26
 *   allows it to be returned from getNamedChild.
27
 * - It is an EmbeddedComponent which means that it
28
 *   is only rendered when explicitly calling the
29
 *   evaluate() method on the instance itself, it
30
 *   is not rendered when rendering the parent node.
31
 *
32
 * EntryNode instances can be resolved directly
33
 * from getTypedChildren to extract all sections
34
 * within a tree; or getNamedChild can be used
35
 * to extract an EntryNode (or an EntryNode child
36
 * of another EntryNode to any nesting depth by
37
 * using dotted path as argument for getNamedChild).
38
 */
39
class EntryNode extends AbstractComponent implements EmbeddedComponentInterface
40
{
41
    protected $escapeOutput = false;
42
43
    public function setName(?string $name): ComponentInterface
44
    {
45
        $this->name = $name;
46
        return $this;
47
    }
48
49
    public function evaluate(RenderingContextInterface $renderingContext)
50
    {
51
        $renderingContext = clone $renderingContext;
52
        $renderingContext->setVariableProvider(
53
            $renderingContext->getVariableProvider()->getScopeCopy(
54
                $this->getArguments()->setRenderingContext($renderingContext)->getArrayCopy()
55
            )
56
        );
57
        return parent::evaluate($renderingContext);
58
    }
59
60
    public function onClose(RenderingContextInterface $renderingContext): ComponentInterface
61
    {
62
        $this->getArguments()->setRenderingContext($renderingContext);
63
        foreach ($this->getChildren() as $child) {
64
            if ($child instanceof ExtendViewHelper) {
65
                $atom = $child->getAtom();
66
                foreach ($atom->getArguments()->getDefinitions() as $definition) {
67
                    $this->getArguments()->addDefinition($definition);
68
                }
69
            } elseif ($child instanceof ParameterViewHelper) {
70
                // The child is a parameter declaration. Use the Component's argument values to create and
71
                // add a new ArgumentDefinition to this component.
72
                $arguments = $this->getArguments();
73
                $context = $arguments->getRenderingContext();
74
                $parameters = $child->getArguments()->setRenderingContext($context);
75
                $arguments->addDefinition(
76
                    new ArgumentDefinition(
77
                        $parameters['name'],
78
                        $parameters['type'],
79
                        $parameters['description'] ?? 'Argument ' . $parameters['name'],
80
                        $parameters['required'],
81
                        $parameters['default'] ?? null
82
                    )
83
                );
84
            }
85
        }
86
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (TYPO3Fluid\Fluid\Core\Parser\SyntaxTree\EntryNode) is incompatible with the return type declared by the interface TYPO3Fluid\Fluid\Compone...onentInterface::onClose 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...
87
    }
88
}
89