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

AtomNode   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
dl 0
loc 32
rs 10
c 0
b 0
f 0
wmc 4
lcom 1
cbo 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setName() 0 5 1
A setFile() 0 5 1
A evaluate() 0 7 1
A onOpen() 0 5 1
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\Core\Rendering\RenderingContextInterface;
13
14
/**
15
 * Atom Node
16
 *
17
 * Represents an Atom used in a template.
18
 */
19
class AtomNode extends AbstractComponent
20
{
21
    protected $escapeOutput = false;
22
23
    protected $file;
24
    
25
    public function setName(?string $name): ComponentInterface
26
    {
27
        $this->name = $name;
28
        return $this;
29
    }
30
31
    public function setFile($file): ComponentInterface
32
    {
33
        $this->file = $file;
34
        return $this;
35
    }
36
37
    public function evaluate(RenderingContextInterface $renderingContext)
38
    {
39
        $atom = $renderingContext->getTemplateParser()->parseFile($this->file);
40
        $arguments = clone $atom->getArguments();
41
        $arguments->assignAll($this->getArguments()->getAllRaw() + $renderingContext->getVariableProvider()->getAll());
0 ignored issues
show
Unused Code introduced by
The call to the method TYPO3Fluid\Fluid\Compone...Collection::assignAll() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
42
        return $atom->setArguments($arguments)->evaluate($renderingContext);
43
    }
44
45
    public function onOpen(RenderingContextInterface $renderingContext): ComponentInterface
46
    {
47
        $this->getArguments()->setRenderingContext($renderingContext);
48
        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\AtomNode) 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...
49
    }
50
}
51