Completed
Pull Request — master (#470)
by Claus
01:30
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\Error\ChildNotFoundException;
11
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
12
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
13
14
/**
15
 * Renders an Atom. Usually not used directly; instead, Atoms should
16
 * be registered as a namespace and will masquerace as this ViewHelper.
17
 */
18
class AtomViewHelper extends AbstractViewHelper
19
{
20
    protected $escapeOutput = false;
21
22
    /**
23
     * @return void
24
     */
25
    public function initializeArguments()
26
    {
27
        parent::initializeArguments();
28
        $this->registerArgument('atom', 'mixed', 'Atom name or instance', true);
29
        $this->registerArgument('file', 'mixed', 'Atom file name, overrides "atom" if both are provided');
30
        $this->registerArgument('section', 'string', 'Optional name or dotted-name path to section to render from inside Atom');
31
        $this->registerArgument('optional', 'boolean', 'If Atom is not found and optional is true, does not throw exception error', false, false);
32
    }
33
34
    public function evaluate(RenderingContextInterface $renderingContext)
35
    {
36
        $arguments = $this->getArguments()->setRenderingContext($renderingContext)->getArrayCopy();
37
        $optional = (boolean) ($arguments['optional'] ?? false);
38
        $default = $arguments['default'] ?? null;
39
40
        try {
41
            if (isset($arguments['file'])) {
42
                $component = $renderingContext->getTemplateParser()->parseFile($arguments['file']);
43
            } elseif (isset($arguments['atom'])) {
44
                $component = $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...
45
            } else {
46
                return $this->evaluateChildren($renderingContext);
47
            }
48
        } catch (ChildNotFoundException $exception) {
49
            if ($optional) {
50
                return $default;
51
            }
52
            throw $exception;
53
        }
54
55
        if (!empty($arguments['section'])) {
56
            $component = $component->getNamedChild($arguments['section']);
57
        }
58
        $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...
59
        return $component->evaluate($renderingContext);
60
    }
61
62
    public function allowUndeclaredArgument(string $argumentName): bool
63
    {
64
        return true;
65
    }
66
}
67