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
|
|
|
|
20
|
|
|
final class ArgumentCallFactory implements ArgumentFactory |
21
|
|
|
{ |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var ArgumentFactory |
25
|
|
|
*/ |
26
|
|
|
private $argumentFactory; |
27
|
|
|
|
28
|
|
|
public function __construct( |
29
|
|
|
ArgumentFactory $argumentFactory |
30
|
|
|
) { |
31
|
|
|
$this->argumentFactory = $argumentFactory; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function understandsString(string $source): bool |
35
|
|
|
{ |
36
|
|
|
return strpos($source, '::') > 0; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function understandsArray(array $source): bool |
40
|
|
|
{ |
41
|
|
|
return isset($source['method']) && isset($source['callee']); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function createArgumentFromString(string $source): Argument |
45
|
|
|
{ |
46
|
|
|
/** @var array<Argument> $arguments */ |
47
|
|
|
$arguments = array(); |
48
|
|
|
|
49
|
|
|
Assert::contains($source, '::'); |
50
|
|
|
|
51
|
|
|
/** @var int|bool $argumentsPosition */ |
52
|
|
|
$argumentsPosition = strpos($source, '('); |
53
|
|
|
|
54
|
|
|
/** @var string $sourceWithoutArguments */ |
55
|
|
|
$sourceWithoutArguments = $source; |
56
|
|
|
|
57
|
|
|
if (is_int($argumentsPosition)) { |
58
|
|
|
/** @var string $argumentsSources */ |
59
|
|
|
$argumentsSources = substr($source, $argumentsPosition + 1); |
60
|
|
|
$argumentsSources = str_replace(')', '', $argumentsSources); |
61
|
|
|
|
62
|
|
|
foreach (explode(',', $argumentsSources) as $argumentsSource) { |
63
|
|
|
$arguments[] = $this->argumentFactory->createArgumentFromString($argumentsSource); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$sourceWithoutArguments = substr($source, 0, $argumentsPosition); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
Assert::contains($sourceWithoutArguments, '::'); |
70
|
|
|
|
71
|
|
|
[$calleeSource, $methodName] = explode('::', $sourceWithoutArguments); |
|
|
|
|
72
|
|
|
|
73
|
|
|
/** @var Argument $callee */ |
74
|
|
|
$callee = $this->argumentFactory->createArgumentFromString($calleeSource); |
|
|
|
|
75
|
|
|
|
76
|
|
|
return new ArgumentCall($callee, $methodName, $arguments); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function createArgumentFromArray(array $source): Argument |
80
|
|
|
{ |
81
|
|
|
Assert::keyExists($source, 'method'); |
82
|
|
|
Assert::keyExists($source, 'callee'); |
83
|
|
|
|
84
|
|
|
/** @var Argument $callee */ |
85
|
|
|
$callee = null; |
|
|
|
|
86
|
|
|
|
87
|
|
|
if (is_array($source['callee'])) { |
88
|
|
|
$callee = $this->argumentFactory->createArgumentFromArray($source['callee']); |
89
|
|
|
|
90
|
|
|
} else { |
91
|
|
|
$callee = $this->argumentFactory->createArgumentFromString($source['callee']); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** @var array<Argument> $arguments */ |
95
|
|
|
$arguments = array(); |
96
|
|
|
|
97
|
|
|
if (isset($source['arguments'])) { |
98
|
|
|
foreach ($source['arguments'] as $argumentsSource) { |
99
|
|
|
/** @var array|string $argumentsSource */ |
100
|
|
|
|
101
|
|
|
if (is_array($argumentsSource)) { |
102
|
|
|
$arguments[] = $this->argumentFactory->createArgumentFromArray($argumentsSource); |
103
|
|
|
|
104
|
|
|
} else { |
105
|
|
|
$arguments[] = $this->argumentFactory->createArgumentFromString($argumentsSource); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return new ArgumentCall($callee, $source['method'], $arguments); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
} |
114
|
|
|
|
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.