Passed
Push — master ( 621a9a...2a422d )
by Gerrit
05:17
created

compileIterativeArgument()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 37

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
nc 5
nop 2
dl 0
loc 37
ccs 0
cts 16
cp 0
crap 20
rs 9.328
c 0
b 0
f 0
1
<?php
2
/**
3
 * Copyright (C) 2019 Gerrit Addiks.
4
 * This package (including this file) was released under the terms of the GPL-3.0.
5
 * You should have received a copy of the GNU General Public License along with this program.
6
 * If not, see <http://www.gnu.org/licenses/> or send me a mail so i can send you a copy.
7
 *
8
 * @license GPL-3.0
9
 *
10
 * @author Gerrit Addiks <[email protected]>
11
 */
12
13
namespace Addiks\SymfonyGenerics\Services;
14
15
use Addiks\SymfonyGenerics\Services\IteratingArgumentCompilerInterface;
16
use Addiks\SymfonyGenerics\Services\ArgumentCompilerInterface;
17
use Webmozart\Assert\Assert;
18
19
final class IteratingArgumentCompiler implements IteratingArgumentCompilerInterface
20
{
21
22
    /** @var ArgumentCompilerInterface */
23
    private $argumentCompiler;
24
25
    /** @var mixed */
26
    private $itemArguments;
27
28
    /** @var bool */
29
    private $argumentIsArray;
30
31
    public function __construct(
32
        ArgumentCompilerInterface $argumentCompiler,
33
        array $itemArguments,
34
        bool $argumentIsArray = false
35
    ) {
36
        Assert::null($this->argumentCompiler);
37
38
        if ($argumentIsArray) {
39
            Assert::isArray($itemArguments);
40
        }
41
42
        $this->argumentCompiler = $argumentCompiler;
43
        $this->itemArguments = $itemArguments;
44
        $this->argumentIsArray = $argumentIsArray;
45
    }
46
47
    public function compileIterativeArgument(
48
        array $items,
49
        array $additionalData = array()
50
    ): array {
51
        /** @var array<int, mixed> $compiledItems */
52
        $compiledItems = array();
0 ignored issues
show
Unused Code introduced by
$compiledItems is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
53
54
        /** @var mixed $item */
55
        foreach ($items as $key => $item) {
56
            /** @var array<string, mixed> $additionalItemData */
57
            $additionalItemData = array_merge($additionalData, ['item' => $item]);
58
59
            if (is_array($item)) {
60
                $additionalItemData = array_merge($additionalItemData, $item);
61
            }
62
63
            /** @var mixed $compiledItem */
64
            $compiledItem = null;
0 ignored issues
show
Unused Code introduced by
$compiledItem is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
65
66
            if ($this->argumentIsArray) {
67
                $compiledItem = $this->argumentCompiler->buildArguments(
68
                    $this->itemArguments,
69
                    $additionalItemData
70
                );
71
72
            } else {
73
                $compiledItem = $this->argumentCompiler->buildArgument(
74
                    $this->itemArguments,
75
                    $additionalItemData
76
                );
77
            }
78
79
            $this->compiledItems[$key] = $compiledItem;
0 ignored issues
show
Bug introduced by
The property compiledItems does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
80
        }
81
82
        return $this->compiledItems;
83
    }
84
85
}
86