Skip to content

Commit ef8d03e

Browse files
committed
Support reference params in interceptors
1 parent 6d2458d commit ef8d03e

File tree

2 files changed

+60
-1
lines changed

2 files changed

+60
-1
lines changed

src/ProxyManager/ProxyGenerator/AccessInterceptorValueHolder/MethodGenerator/Util/InterceptorGenerator.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ public static function createInterceptedMethodBody(
6666

6767
foreach ($method->getParameters() as $parameter) {
6868
$parameterName = $parameter->getName();
69-
$params[] = var_export($parameterName, true) . ' => $' . $parameter->getName();
69+
$symbol = $parameter->getPassedByReference() ? '&$' : '$';
70+
$params[] = var_export($parameterName, true) . ' => ' . $symbol . $parameterName;
7071
}
7172

7273
$paramsString = 'array(' . implode(', ', $params) . ')';

tests/ProxyManagerTest/ProxyGenerator/AccessInterceptorValueHolder/MethodGenerator/Util/InterceptorGeneratorTest.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,64 @@ public function testInterceptorGeneratorWithNonVoidOriginalMethod(): void
175175
}
176176
}
177177
178+
return $returnValue;
179+
PHP;
180+
// @codingStandardsIgnoreEnd
181+
182+
self::assertSame(
183+
$expected,
184+
InterceptorGenerator::createInterceptedMethodBody(
185+
'$returnValue = "foo";',
186+
$method,
187+
$valueHolder,
188+
$prefixInterceptors,
189+
$suffixInterceptors,
190+
new ReflectionMethod(BaseClass::class, 'publicMethod')
191+
)
192+
);
193+
}
194+
195+
public function testInterceptorGeneratorWithReferences(): void
196+
{
197+
$method = $this->createMock(MethodGenerator::class);
198+
$bar = $this->createMock(ParameterGenerator::class);
199+
$baz = $this->createMock(ParameterGenerator::class);
200+
$valueHolder = $this->createMock(PropertyGenerator::class);
201+
$prefixInterceptors = $this->createMock(PropertyGenerator::class);
202+
$suffixInterceptors = $this->createMock(PropertyGenerator::class);
203+
204+
$bar->method('getName')->willReturn('bar');
205+
$bar->method('getPassedByReference')->willReturn(false);
206+
$baz->method('getName')->willReturn('baz');
207+
$baz->method('getPassedByReference')->willReturn(true);
208+
$method->method('getName')->willReturn('fooMethod');
209+
$method->method('getParameters')->will(self::returnValue([$bar, $baz]));
210+
$valueHolder->method('getName')->willReturn('foo');
211+
$prefixInterceptors->method('getName')->willReturn('pre');
212+
$suffixInterceptors->method('getName')->willReturn('post');
213+
214+
// @codingStandardsIgnoreStart
215+
$expected = <<<'PHP'
216+
if (isset($this->pre['fooMethod'])) {
217+
$returnEarly = false;
218+
$prefixReturnValue = $this->pre['fooMethod']->__invoke($this, $this->foo, 'fooMethod', array('bar' => $bar, 'baz' => &$baz), $returnEarly);
219+
220+
if ($returnEarly) {
221+
return $prefixReturnValue;
222+
}
223+
}
224+
225+
$returnValue = "foo";
226+
227+
if (isset($this->post['fooMethod'])) {
228+
$returnEarly = false;
229+
$suffixReturnValue = $this->post['fooMethod']->__invoke($this, $this->foo, 'fooMethod', array('bar' => $bar, 'baz' => &$baz), $returnValue, $returnEarly);
230+
231+
if ($returnEarly) {
232+
return $suffixReturnValue;
233+
}
234+
}
235+
178236
return $returnValue;
179237
PHP;
180238
// @codingStandardsIgnoreEnd

0 commit comments

Comments
 (0)