Skip to content

Commit d2c49f7

Browse files
committed
Avoid unwrapping object when proxied object method returns
1 parent 9d9c8d8 commit d2c49f7

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

src/Isolate/LazyObjects/Proxy/Adapter/OcramiusProxyManager/MethodGenerator/MethodProxy.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,11 @@ public static function generateMethod(
3737
. 'if ($this->hasMethodReplacement("' . $method->getName() . '")) {' . "\n"
3838
. ' return $this->getMethodReplacement("' . $method->getName() .'")->getReplacement()->execute($this, "' . $method->getName() . '", ' . $paramsString . ');' . "\n"
3939
. '}' . "\n\n"
40-
. 'return $this->' . $wrappedObjectProperty->getName() . '->' . $method->getName() . '(' . implode(', ', $forwardedParams) . ');';
40+
. '$result = $this->' . $wrappedObjectProperty->getName() . '->' . $method->getName() . '(' . implode(', ', $forwardedParams) . ');' . "\n"
41+
. 'if ($result === $this->' . $wrappedObjectProperty->getName() . ') {' ."\n"
42+
. ' return $this;' . "\n"
43+
. "}\n\n"
44+
. 'return $result;';
4145

4246

4347
$method->setDocblock('{@inheritDoc}');

tests/Isolate/LazyObjects/Tests/Double/EntityFake.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ public function removeItem($item)
2828
}
2929
}
3030

31+
public function getThis()
32+
{
33+
return $this;
34+
}
35+
3136
public function __sleep()
3237
{
3338
return ['items'];

tests/Isolate/LazyObjects/Tests/WrapperTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,17 @@ function test_lazy_objects_serialization()
162162
$this->assertSame($replacementResult, $unserialized->getItems());
163163
}
164164

165+
function test_not_unwrapping_object_when_method_returns_this()
166+
{
167+
$entity = new EntityFake();
168+
169+
$entityProxyDefinition = new Definition(new ClassName(get_class($entity)), [], []);
170+
171+
$wrapper = $this->createWrapper($entityProxyDefinition);
172+
$proxy = $wrapper->wrap($entity);
173+
174+
$this->assertSame($proxy, $proxy->getThis());
175+
}
165176

166177
/**
167178
* @param $entityProxyDefinition

0 commit comments

Comments
 (0)