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

ArgumentCollection::offsetSet()   C

Complexity

Conditions 13
Paths 4

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 13
nc 4
nop 2
dl 0
loc 14
rs 6.6166
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Parser\SyntaxTree\ArrayNode;
13
use TYPO3Fluid\Fluid\Core\Parser\SyntaxTree\BooleanNode;
14
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
15
16
/**
17
 * Argument Collection
18
 *
19
 * Acts as container around a set of arguments and associated
20
 * ArgumentDefinition and their values.
21
 *
22
 * Contains the API used for validating and converting arguments.
23
 */
24
class ArgumentCollection extends \ArrayObject
25
{
26
    /**
27
     * @var ArgumentDefinition[]
28
     */
29
    protected $definitions = [];
30
31
    /**
32
     * @var RenderingContextInterface
33
     */
34
    protected $renderingContext;
35
36
    public function setRenderingContext(RenderingContextInterface $renderingContext): self
37
    {
38
        $this->renderingContext = $renderingContext;
39
        return $this;
40
    }
41
42
    public function getRenderingContext(): RenderingContextInterface
43
    {
44
        return $this->renderingContext;
45
    }
46
47
    public function getDefinitions(): iterable
48
    {
49
        return $this->definitions;
50
    }
51
52
    public function assignAll(iterable $values): ArgumentCollection
53
    {
54
        foreach ($values as $name => $value) {
55
            $this[$name] = $value;
56
        }
57
        return $this;
58
    }
59
60
    public function getAllRaw(): iterable
61
    {
62
        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...
63
    }
64
65
    public function getRaw(string $argumentName)
66
    {
67
        $value = $this[$argumentName] ?? null;
68
        return $value;
69
    }
70
71
    public function addDefinition(ArgumentDefinition $definition): ArgumentCollection
72
    {
73
        $argumentName = $definition->getName();
74
        $this->definitions[$argumentName] = $definition;
75
        return $this;
76
    }
77
78
    /**
79
     * @param iterable|ArgumentDefinition[] $definitions
80
     * @return ArgumentCollection
81
     */
82
    public function setDefinitions(iterable $definitions): ArgumentCollection
83
    {
84
        $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...
85
        return $this;
86
    }
87
88
    public function isArgumentBoolean(string $argumentName): bool
89
    {
90
        return isset($this->definitions[$argumentName]) && $this->definitions[$argumentName]->getType() === 'boolean';
91
    }
92
93
    public function toArrayNode(): ArrayNode
94
    {
95
        return new ArrayNode((array) $this->getAllRaw());
96
    }
97
98
    public function offsetSet($index, $value)
99
    {
100
        if (isset($this->definitions[$index]) && !$value instanceof BooleanNode && $this->definitions[$index]->getType() === 'boolean' && $value !== false && $value !== true) {
101
            // Note: a switch() was not used here because it makes PHP attempt type coercion and we need strict comparisons.
102
            if ($value === 1 || $value === 'true' || $value === 'TRUE') {
103
                $value = true;
104
            } elseif ($value === null || $value === 0 || $value === 'false' || $value === 'FALSE') {
105
                $value = false;
106
            } else {
107
                $value = new BooleanNode($value);
108
            }
109
        }
110
        parent::offsetSet($index, $value);
111
    }
112
113
    public function offsetGet($offset)
114
    {
115
        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...
116
            return $this->definitions[$offset]->getDefaultValue();
117
        }
118
        $value = parent::offsetGet($offset);
119
        if ($value instanceof ComponentInterface) {
120
            $value = $value->evaluate($this->renderingContext);
121
        }
122
        return $value;
123
    }
124
125
    public function getArrayCopy(): array
126
    {
127
        $data = [];
128
        foreach (parent::getArrayCopy() + $this->definitions as $name => $_) {
129
            $data[$name] = $this[$name];
130
        }
131
        return $data;
132
    }
133
134
    /**
135
     * Creates arguments by padding with missing+optional arguments
136
     * and casting or creating BooleanNode where appropriate. Input
137
     * array may not contain all arguments - output array will.
138
     */
139
    public function validate(): self
140
    {
141
        $missingArguments = [];
142
        foreach ($this->definitions as $name => $definition) {
143
            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...
144
                // Required but missing argument, causes failure (delayed, to report all missing arguments at once)
145
                $missingArguments[] = $name;
146
            }
147
        }
148
        if (!empty($missingArguments)) {
149
            throw new Exception('Required argument(s) not provided: ' . implode(', ', $missingArguments), 1558533510);
150
        }
151
        return $this;
152
    }
153
}
154