1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright (C) 2018 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\RDMBundle\ValueResolver; |
14
|
|
|
|
15
|
|
|
use Addiks\RDMBundle\ValueResolver\CallDefinitionExecuterInterface; |
16
|
|
|
use Addiks\RDMBundle\Mapping\CallDefinitionInterface; |
17
|
|
|
use Addiks\RDMBundle\Mapping\MappingInterface; |
18
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
19
|
|
|
use Addiks\RDMBundle\ValueResolver\ValueResolverInterface; |
20
|
|
|
|
21
|
|
|
final class CallDefinitionExecuter implements CallDefinitionExecuterInterface |
22
|
|
|
{ |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var ContainerInterface |
26
|
|
|
*/ |
27
|
|
|
private $container; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var ValueResolverInterface |
31
|
|
|
*/ |
32
|
|
|
private $argumentResolver; |
33
|
|
|
|
34
|
7 |
|
public function __construct( |
35
|
|
|
ContainerInterface $container, |
36
|
|
|
ValueResolverInterface $argumentResolver |
37
|
|
|
) { |
38
|
7 |
|
$this->container = $container; |
39
|
7 |
|
$this->argumentResolver = $argumentResolver; |
40
|
7 |
|
} |
41
|
|
|
|
42
|
3 |
|
public function executeCallDefinition( |
43
|
|
|
CallDefinitionInterface $callDefinition, |
44
|
|
|
$entity, |
45
|
|
|
array $dataFromAdditionalColumns |
46
|
|
|
) { |
47
|
|
|
/** @var mixed $result */ |
48
|
3 |
|
$result = null; |
|
|
|
|
49
|
|
|
|
50
|
|
|
/** @var string $objectReference */ |
51
|
3 |
|
$objectReference = $callDefinition->getObjectReference(); |
52
|
|
|
|
53
|
|
|
/** @var string $routineName */ |
54
|
3 |
|
$routineName = $callDefinition->getRoutineName(); |
55
|
|
|
|
56
|
|
|
/** @var array<MappingInterface> $argumentMappings */ |
57
|
3 |
|
$argumentMappings = $callDefinition->getArgumentMappings(); |
58
|
|
|
|
59
|
|
|
/** @var null|object|string $callee */ |
60
|
3 |
|
$callee = $this->resolveCallee($objectReference, $entity); |
61
|
|
|
|
62
|
|
|
/** @var array<mixed> $arguments */ |
63
|
3 |
|
$arguments = $this->resolveArguments( |
64
|
3 |
|
$argumentMappings, |
65
|
3 |
|
$entity, |
66
|
3 |
|
$dataFromAdditionalColumns |
67
|
|
|
); |
68
|
|
|
|
69
|
3 |
|
if (is_null($callee)) { |
70
|
|
|
$result = call_user_func_array($routineName, $arguments); |
71
|
|
|
|
72
|
3 |
|
} elseif (is_string($callee)) { |
73
|
1 |
|
$result = call_user_func_array("{$callee}::{$routineName}", $arguments); |
74
|
|
|
|
75
|
|
|
} else { |
76
|
2 |
|
$result = call_user_func_array([$callee, $routineName], $arguments); |
77
|
|
|
} |
78
|
|
|
|
79
|
3 |
|
return $result; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param object $entity |
84
|
|
|
* |
85
|
|
|
* (This return type should be nullable, but there seems to be a bug in current version psalm preventing it.) |
86
|
|
|
* |
87
|
|
|
* @return object|string |
88
|
|
|
*/ |
89
|
3 |
|
private function resolveCallee(string $objectReference, $entity) { |
90
|
|
|
/** @var object|string $callee */ |
91
|
3 |
|
$callee = null; |
92
|
|
|
|
93
|
3 |
|
if (in_array($objectReference, ['self', 'this', '$this'])) { |
94
|
1 |
|
$callee = $entity; |
95
|
|
|
|
96
|
2 |
|
} elseif ($objectReference[0] === '@') { |
97
|
|
|
/** @var string $serviceId */ |
98
|
1 |
|
$serviceId = substr($objectReference, 1); |
99
|
|
|
|
100
|
1 |
|
$callee = $this->container->get($serviceId); |
101
|
|
|
|
102
|
1 |
|
} elseif (class_exists($objectReference)) { |
103
|
1 |
|
$callee = $objectReference; |
104
|
|
|
} |
105
|
|
|
|
106
|
3 |
|
return $callee; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param array<MappingInterface> $argumentMappings |
111
|
|
|
* @param object $entity |
112
|
|
|
* @param array<scalar> $dataFromAdditionalColumns |
113
|
|
|
* |
114
|
|
|
* @return array<mixed> |
115
|
|
|
*/ |
116
|
3 |
|
private function resolveArguments( |
117
|
|
|
array $argumentMappings, |
118
|
|
|
$entity, |
119
|
|
|
array $dataFromAdditionalColumns |
120
|
|
|
): array { |
121
|
|
|
/** @var array<mixed> $arguments */ |
122
|
3 |
|
$arguments = array(); |
123
|
|
|
|
124
|
3 |
|
foreach ($argumentMappings as $argumentMapping) { |
125
|
|
|
/** @var MappingInterface $argumentMapping */ |
126
|
|
|
|
127
|
1 |
|
$arguments[] = $this->argumentResolver->resolveValue( |
128
|
1 |
|
$argumentMapping, |
129
|
1 |
|
$entity, |
130
|
1 |
|
$dataFromAdditionalColumns |
131
|
|
|
); |
132
|
|
|
} |
133
|
|
|
|
134
|
3 |
|
return $arguments; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
} |
138
|
|
|
|
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.