Completed
Pull Request — master (#470)
by Claus
06:03
created

AtomViewHelper::initializeArguments()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
namespace TYPO3Fluid\Fluid\ViewHelpers;
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\Component\Error\ChildNotFoundException;
12
use TYPO3Fluid\Fluid\Core\Parser\SyntaxTree\RootNode;
13
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
14
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
15
16
/**
17
 * Renders an Atom. Usually not used directly; instead, Atoms should
18
 * be registered as a namespace and will masquerace as this ViewHelper.
19
 */
20
class AtomViewHelper extends AbstractViewHelper
21
{
22
    protected $escapeOutput = false;
23
24
    /**
25
     * @return void
26
     */
27
    public function initializeArguments()
28
    {
29
        parent::initializeArguments();
30
        $this->registerArgument('atom', 'mixed', 'Atom name or instance', true);
31
        $this->registerArgument('file', 'mixed', 'Atom file name, overrides "atom" if both are provided');
32
        $this->registerArgument('section', 'string', 'Optional name or dotted-name path to section to render from inside Atom');
33
        $this->registerArgument('optional', 'boolean', 'If Atom is not found and optional is true, does not throw exception error', false, false);
34
    }
35
36
    public function evaluate(RenderingContextInterface $renderingContext)
37
    {
38
        $arguments = $this->getArguments()->setRenderingContext($renderingContext)->getArrayCopy();
39
        $optional = (boolean) ($arguments['optional'] ?? false);
40
        $default = $arguments['default'] ?? null;
41
42
        try {
43
            if (isset($arguments['file'])) {
44
                $component = $renderingContext->getTemplateParser()->parseFile($arguments['file']);
45
            } elseif (isset($arguments['atom'])) {
46
                $component = $arguments['atom'] instanceof ComponentInterface ? $arguments['atom'] : $renderingContext->getViewHelperResolver()->resolveAtom(...explode(':', $arguments['atom']));
0 ignored issues
show
Bug introduced by
The call to resolveAtom() misses a required argument $name.

This check looks for function calls that miss required arguments.

Loading history...
Documentation introduced by
explode(':', $arguments['atom']) is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
47
            } else {
48
                $component = $this;
49
            }
50
        } catch (ChildNotFoundException $exception) {
51
            if ($optional) {
52
                return $default;
53
            }
54
            throw $exception;
55
        }
56
57
        if (!empty($arguments['section'])) {
58
            $component = $component->getNamedChild($arguments['section']);
59
        }
60
        $component->getArguments()->assignAll($arguments);
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...
61
        return $component->evaluate($renderingContext);
62
    }
63
64
    public function allowUndeclaredArgument(string $argumentName): bool
65
    {
66
        return true;
67
    }
68
}
69