|
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\SymfonyGenerics\Arguments\ArgumentFactory; |
|
14
|
|
|
|
|
15
|
|
|
use Addiks\SymfonyGenerics\Arguments\ArgumentFactory\ArgumentFactory; |
|
16
|
|
|
use Addiks\SymfonyGenerics\Arguments\Argument; |
|
17
|
|
|
use Addiks\SymfonyGenerics\Arguments\ArgumentCall; |
|
18
|
|
|
use Webmozart\Assert\Assert; |
|
19
|
|
|
use Addiks\SymfonyGenerics\Services\ArgumentCompilerInterface; |
|
20
|
|
|
use Addiks\SymfonyGenerics\Arguments\NullArgument; |
|
21
|
|
|
use Addiks\SymfonyGenerics\Arguments\ObjectInstanciationArgument; |
|
22
|
|
|
|
|
23
|
|
|
final class ArgumentCallFactory implements ArgumentFactory |
|
24
|
|
|
{ |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var ArgumentCompilerInterface |
|
28
|
|
|
*/ |
|
29
|
|
|
private $argumentCompiler; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var ArgumentFactory |
|
33
|
|
|
*/ |
|
34
|
|
|
private $argumentFactory; |
|
35
|
|
|
|
|
36
|
25 |
|
public function __construct( |
|
37
|
|
|
ArgumentCompilerInterface $argumentCompiler, |
|
38
|
|
|
ArgumentFactory $argumentFactory |
|
39
|
|
|
) { |
|
40
|
25 |
|
$this->argumentCompiler = $argumentCompiler; |
|
41
|
25 |
|
$this->argumentFactory = $argumentFactory; |
|
42
|
25 |
|
} |
|
43
|
|
|
|
|
44
|
13 |
|
public function understandsString(string $source): bool |
|
45
|
|
|
{ |
|
46
|
13 |
|
return 1 === preg_match("/^[^\:]+\:\:[a-zA-Z0-9_-]+/is", $source); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
4 |
|
public function understandsArray(array $source): bool |
|
50
|
|
|
{ |
|
51
|
4 |
|
return isset($source['method']) && isset($source['callee']); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
6 |
|
public function createArgumentFromString(string $source): Argument |
|
55
|
|
|
{ |
|
56
|
6 |
|
Assert::true($this->understandsString($source)); |
|
57
|
|
|
|
|
58
|
|
|
/** @var array<Argument> $arguments */ |
|
59
|
3 |
|
$arguments = array(); |
|
60
|
|
|
|
|
61
|
|
|
/** @var int|bool $argumentsPosition */ |
|
62
|
3 |
|
$argumentsPosition = strpos($source, '('); |
|
63
|
|
|
|
|
64
|
|
|
/** @var string $sourceWithoutArguments */ |
|
65
|
3 |
|
$sourceWithoutArguments = $source; |
|
66
|
|
|
|
|
67
|
3 |
|
if (is_int($argumentsPosition)) { |
|
68
|
|
|
/** @var string $argumentsSources */ |
|
69
|
2 |
|
$argumentsSources = substr($source, $argumentsPosition + 1); |
|
70
|
2 |
|
$argumentsSources = str_replace(')', '', $argumentsSources); |
|
71
|
|
|
|
|
72
|
2 |
|
foreach (explode(',', $argumentsSources) as $argumentsSource) { |
|
73
|
2 |
|
$arguments[] = $this->argumentFactory->createArgumentFromString(trim($argumentsSource)); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
2 |
|
$sourceWithoutArguments = substr($source, 0, $argumentsPosition); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
3 |
|
[$calleeSource, $methodName] = explode('::', $sourceWithoutArguments); |
|
|
|
|
|
|
80
|
|
|
|
|
81
|
|
|
/** @var Argument $callee */ |
|
82
|
3 |
|
$callee = $this->argumentFactory->createArgumentFromString($calleeSource); |
|
|
|
|
|
|
83
|
|
|
|
|
84
|
3 |
|
return new ArgumentCall($this->argumentCompiler, $callee, $methodName, $arguments); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
8 |
|
public function createArgumentFromArray(array $source): Argument |
|
88
|
|
|
{ |
|
89
|
8 |
|
Assert::keyExists($source, 'method'); |
|
90
|
6 |
|
Assert::keyExists($source, 'callee'); |
|
91
|
|
|
|
|
92
|
|
|
/** @var Argument $callee */ |
|
93
|
5 |
|
$callee = null; |
|
|
|
|
|
|
94
|
|
|
|
|
95
|
5 |
|
if (is_array($source['callee'])) { |
|
96
|
2 |
|
$callee = $this->argumentFactory->createArgumentFromArray($source['callee']); |
|
97
|
|
|
|
|
98
|
|
|
} else { |
|
99
|
3 |
|
$callee = $this->argumentFactory->createArgumentFromString($source['callee']); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** @var array<Argument> $arguments */ |
|
103
|
5 |
|
$arguments = array(); |
|
104
|
|
|
|
|
105
|
5 |
|
if (isset($source['arguments'])) { |
|
106
|
3 |
|
foreach ($source['arguments'] as $argumentsSource) { |
|
107
|
|
|
/** @var array|string $argumentsSource */ |
|
108
|
|
|
|
|
109
|
3 |
|
if (is_array($argumentsSource)) { |
|
110
|
1 |
|
$arguments[] = $this->argumentFactory->createArgumentFromArray($argumentsSource); |
|
111
|
|
|
|
|
112
|
2 |
|
} elseif (is_null($argumentsSource)) { |
|
113
|
|
|
$arguments[] = new NullArgument(); |
|
114
|
|
|
|
|
115
|
|
|
} else { |
|
116
|
3 |
|
$arguments[] = $this->argumentFactory->createArgumentFromString($argumentsSource); |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** @var string $methodName */ |
|
122
|
5 |
|
$methodName = $source['method']; |
|
123
|
|
|
|
|
124
|
5 |
|
if ($methodName === '__construct') { |
|
125
|
|
|
return new ObjectInstanciationArgument($callee, $arguments); |
|
126
|
|
|
|
|
127
|
|
|
} else { |
|
128
|
5 |
|
return new ArgumentCall($this->argumentCompiler, $callee, $source['method'], $arguments); |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
} |
|
133
|
|
|
|
This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.
The variable may have been renamed without also renaming all references.