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(); |
|
|
|
|
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; |
|
|
|
|
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; |
|
|
|
|
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return $this->compiledItems; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
} |
86
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
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.