Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DCOM-194] Fixed proxying magic getter with reference #278

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 14 additions & 1 deletion lib/Doctrine/Common/Proxy/ProxyGenerator.php
Expand Up @@ -400,12 +400,25 @@ private function generateMagicGet(ClassMetadata $class)
}

$inheritDoc = $hasParentGet ? '{@inheritDoc}' : '';


$magicGet = <<<EOT
/**
* $inheritDoc
* @param string \$name
*/
public function __get(\$name)
public function
EOT;

$magicGet .= ' ';


if ($hasParentGet && $class->getReflectionClass()->getMethod('__get')->returnsReference()) {
$magicGet .= '&';
}

$magicGet .= <<<EOT
__get(\$name)
{

EOT;
Expand Down
26 changes: 26 additions & 0 deletions tests/Doctrine/Tests/Common/Proxy/MagicGetByRefClass.php
@@ -0,0 +1,26 @@
<?php

namespace Doctrine\Tests\Common\Proxy;

/**
* Test asset class
*/
class MagicGetByRefClass
{
public $valueField;

/**
* @param $name
*
* @return string
* @throws \BadMethodCallException
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oops, this shouldn't be here, will fix

*/
public function &__get($name)
{
if ($name === 'value') {
return $this->valueField;
}

return 'not defined';
}
}
25 changes: 24 additions & 1 deletion tests/Doctrine/Tests/Common/Proxy/ProxyMagicMethodsTest.php
Expand Up @@ -94,6 +94,29 @@ function (Proxy $proxy, $method, $params) use (&$counter) {
$this->assertSame(3, $counter);
}

public function testInheritedMagicGetByRef()
{
$proxyClassName = $this->generateProxyClass(__NAMESPACE__ . '\\MagicGetByRefClass');
$proxy = new $proxyClassName();

$proxy->valueField = 123;

$this->assertSame(123, $proxy->valueField);
$this->assertSame(123, $proxy->value);

$proxy->valueField = array(1, 2);

$this->assertSame(1, $proxy->value[0]);
$this->assertSame(2, $proxy->value[1]);

$proxy->value[0] = 3;
$proxy->value[2] = 1;

$this->assertSame(3, $proxy->value[0]);
$this->assertSame(2, $proxy->value[1]);
$this->assertSame(1, $proxy->value[2]);
}

public function testInheritedMagicSet()
{
$proxyClassName = $this->generateProxyClass(__NAMESPACE__ . '\\MagicSetClass');
Expand Down Expand Up @@ -282,4 +305,4 @@ private function generateProxyClass($className)

return $proxyClassName;
}
}
}