-
Notifications
You must be signed in to change notification settings - Fork 461
/
MagicMethodTypeHintsPass.php
205 lines (179 loc) · 5.3 KB
/
MagicMethodTypeHintsPass.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
<?php
/**
* Mockery (https://docs.mockery.io/)
*
* @copyright https://github.com/mockery/mockery/blob/HEAD/COPYRIGHT.md
* @license https://github.com/mockery/mockery/blob/HEAD/LICENSE BSD 3-Clause License
* @link https://github.com/mockery/mockery for the canonical source repository
*/
namespace Mockery\Generator\StringManipulation\Pass;
use Mockery\Generator\Method;
use Mockery\Generator\MockConfiguration;
use Mockery\Generator\Parameter;
use Mockery\Generator\TargetClassInterface;
use function array_filter;
use function array_merge;
use function end;
use function in_array;
use function is_array;
use function preg_match;
use function preg_match_all;
use function preg_replace;
use function rtrim;
use function sprintf;
class MagicMethodTypeHintsPass implements Pass
{
/**
* @var array
*/
private $mockMagicMethods = [
'__construct',
'__destruct',
'__call',
'__callStatic',
'__get',
'__set',
'__isset',
'__unset',
'__sleep',
'__wakeup',
'__toString',
'__invoke',
'__set_state',
'__clone',
'__debugInfo',
];
/**
* Apply implementation.
*
* @param string $code
* @param MockConfiguration $config
*
* @return string
*/
public function apply($code, MockConfiguration $config)
{
$magicMethods = $this->getMagicMethods($config->getTargetClass());
foreach ($config->getTargetInterfaces() as $interface) {
$magicMethods = array_merge($magicMethods, $this->getMagicMethods($interface));
}
foreach ($magicMethods as $method) {
$code = $this->applyMagicTypeHints($code, $method);
}
return $code;
}
/**
* Returns the magic methods within the
* passed DefinedTargetClass.
*
* @param TargetClassInterface $class
*
* @return array
*/
public function getMagicMethods(?TargetClassInterface $class = null)
{
if (! $class instanceof TargetClassInterface) {
return [];
}
return array_filter($class->getMethods(), function (Method $method) {
return in_array($method->getName(), $this->mockMagicMethods, true);
});
}
protected function renderTypeHint(Parameter $param)
{
$typeHint = $param->getTypeHint();
return $typeHint === null ? '' : sprintf('%s ', $typeHint);
}
/**
* Applies type hints of magic methods from
* class to the passed code.
*
* @param int $code
* @param Method $method
*
* @return string
*/
private function applyMagicTypeHints($code, Method $method)
{
if ($this->isMethodWithinCode($code, $method)) {
$namedParameters = $this->getOriginalParameters($code, $method);
$code = preg_replace(
$this->getDeclarationRegex($method->getName()),
$this->getMethodDeclaration($method, $namedParameters),
$code
);
}
return $code;
}
/**
* Returns a regex string used to match the
* declaration of some method.
*
* @param string $methodName
*
* @return string
*/
private function getDeclarationRegex($methodName)
{
return sprintf('/public\s+(?:static\s+)?function\s+%s\s*\(.*\)\s*(?=\{)/i', $methodName);
}
/**
* Gets the declaration code, as a string, for the passed method.
*
* @param Method $method
* @param array $namedParameters
*
* @return string
*/
private function getMethodDeclaration(Method $method, array $namedParameters)
{
$declaration = 'public';
$declaration .= $method->isStatic() ? ' static' : '';
$declaration .= ' function ' . $method->getName() . '(';
foreach ($method->getParameters() as $index => $parameter) {
$declaration .= $this->renderTypeHint($parameter);
$name = $namedParameters[$index] ?? $parameter->getName();
$declaration .= '$' . $name;
$declaration .= ',';
}
$declaration = rtrim($declaration, ',');
$declaration .= ') ';
$returnType = $method->getReturnType();
if ($returnType !== null) {
$declaration .= sprintf(': %s', $returnType);
}
return $declaration;
}
/**
* Returns the method original parameters, as they're
* described in the $code string.
*
* @param int $code
* @param Method $method
*
* @return array
*/
private function getOriginalParameters($code, Method $method)
{
$matches = [];
$parameterMatches = [];
preg_match($this->getDeclarationRegex($method->getName()), $code, $matches);
if ($matches !== []) {
preg_match_all('/(?<=\$)(\w+)+/i', $matches[0], $parameterMatches);
}
$groupMatches = end($parameterMatches);
return is_array($groupMatches) ? $groupMatches : [$groupMatches];
}
/**
* Checks if the method is declared within code.
*
* @param int $code
* @param Method $method
*
* @return boolean
*/
private function isMethodWithinCode($code, Method $method)
{
return preg_match($this->getDeclarationRegex($method->getName()), $code) === 1;
}
}