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

ArgumentCollection::setDefinitions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
namespace TYPO3Fluid\Fluid\Component\Argument;
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\Core\Parser\Exception;
12
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
13
14
/**
15
 * Argument Collection
16
 *
17
 * Acts as container around a set of arguments and associated
18
 * ArgumentDefinition and their values.
19
 *
20
 * Contains the API used for validating and converting arguments.
21
 */
22
class ArgumentCollection extends \ArrayObject
23
{
24
    /**
25
     * @var ArgumentDefinition[]
26
     */
27
    protected $definitions = [];
28
29
    /**
30
     * @var RenderingContextInterface
31
     */
32
    protected $renderingContext;
33
34
    public function setRenderingContext(RenderingContextInterface $renderingContext): self
35
    {
36
        $this->renderingContext = $renderingContext;
37
        return $this;
38
    }
39
40
    public function getRenderingContext(): RenderingContextInterface
41
    {
42
        return $this->renderingContext;
43
    }
44
45
    public function getDefinitions(): iterable
46
    {
47
        return $this->definitions;
48
    }
49
50
    public function assignAll(iterable $values): ArgumentCollection
51
    {
52
        foreach ($values as $name => $value) {
53
            $this[$name] = $value;
54
        }
55
        return $this;
56
    }
57
58
    public function getAllRaw(): iterable
59
    {
60
        return parent::getArrayCopy();
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (getArrayCopy() instead of getAllRaw()). Are you sure this is correct? If so, you might want to change this to $this->getArrayCopy().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
61
    }
62
63
    public function getRaw(string $argumentName)
64
    {
65
        $value = $this[$argumentName] ?? null;
66
        return $value;
67
    }
68
69
    public function addDefinition(ArgumentDefinition $definition): ArgumentCollection
70
    {
71
        $argumentName = $definition->getName();
72
        $this->definitions[$argumentName] = $definition;
73
        return $this;
74
    }
75
76
    /**
77
     * @param iterable|ArgumentDefinition[] $definitions
78
     * @return ArgumentCollection
79
     */
80
    public function setDefinitions(iterable $definitions): ArgumentCollection
81
    {
82
        $this->definitions = $definitions;
0 ignored issues
show
Documentation Bug introduced by
It seems like $definitions can also be of type object<TYPO3Fluid\Fluid\...nent\Argument\iterable>. However, the property $definitions is declared as type array<integer,object<TYP...nt\ArgumentDefinition>>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
83
        return $this;
84
    }
85
86
    public function offsetGet($offset)
87
    {
88
        if (isset($this->definitions[$offset]) && !parent::offsetExists($offset)) {
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (offsetExists() instead of offsetGet()). Are you sure this is correct? If so, you might want to change this to $this->offsetExists().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
89
            return $this->definitions[$offset]->getDefaultValue();
90
        }
91
        $value = parent::offsetGet($offset);
92
        if ($value instanceof ComponentInterface) {
93
            $value = $value->evaluate($this->renderingContext);
94
        }
95
        return $value;
96
    }
97
98
    public function getArrayCopy(): array
99
    {
100
        $data = [];
101
        foreach (parent::getArrayCopy() + $this->definitions as $name => $_) {
102
            $data[$name] = $this[$name];
103
        }
104
        return $data;
105
    }
106
107
    /**
108
     * Creates arguments by padding with missing+optional arguments
109
     * and casting or creating BooleanNode where appropriate. Input
110
     * array may not contain all arguments - output array will.
111
     */
112
    public function validate(): self
113
    {
114
        $missingArguments = [];
115
        foreach ($this->definitions as $name => $definition) {
116
            if ($definition->isRequired() && !parent::offsetExists($name)) {
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (offsetExists() instead of validate()). Are you sure this is correct? If so, you might want to change this to $this->offsetExists().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
117
                // Required but missing argument, causes failure (delayed, to report all missing arguments at once)
118
                $missingArguments[] = $name;
119
            }
120
        }
121
        if (!empty($missingArguments)) {
122
            throw new Exception('Required argument(s) not provided: ' . implode(', ', $missingArguments), 1558533510);
123
        }
124
        return $this;
125
    }
126
}
127