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