Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #281 from doctrine/hotfix/DCOM-194

DCOM-194
  • Loading branch information...
commit d658ec7a03f6475eff0dd1eb940bdedd862e4b96 2 parents 2286642 + 5c450d4
@Ocramius Ocramius authored
View
17 lib/Doctrine/Common/Proxy/ProxyGenerator.php
@@ -393,19 +393,30 @@ public function __construct($initializer = null, $cloner = null)
private function generateMagicGet(ClassMetadata $class)
{
$lazyPublicProperties = array_keys($this->getLazyLoadedPublicProperties($class));
- $hasParentGet = $class->getReflectionClass()->hasMethod('__get');
+ $reflectionClass = $class->getReflectionClass();
+ $hasParentGet = false;
+ $returnReference = '';
+ $inheritDoc = '';
+
+ if ($reflectionClass->hasMethod('__get')) {
+ $hasParentGet = true;
+ $inheritDoc = '{@inheritDoc}';
+
+ if ($reflectionClass->getMethod('__get')->returnsReference()) {
+ $returnReference = '& ';
+ }
+ }
if (empty($lazyPublicProperties) && ! $hasParentGet) {
return '';
}
- $inheritDoc = $hasParentGet ? '{@inheritDoc}' : '';
$magicGet = <<<EOT
/**
* $inheritDoc
* @param string \$name
*/
- public function __get(\$name)
+ public function {$returnReference}__get(\$name)
{
EOT;
View
51 tests/Doctrine/Tests/Common/Proxy/MagicGetByRefClass.php
@@ -0,0 +1,51 @@
+<?php
+/*
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * This software consists of voluntary contributions made by many individuals
+ * and is licensed under the MIT license. For more information, see
+ * <http://www.doctrine-project.org>.
+ */
+
+namespace Doctrine\Tests\Common\Proxy;
+
+use InvalidArgumentException;
+
+/**
+ * Test asset class
+ *
+ * @since 2.4
+ */
+class MagicGetByRefClass
+{
+ /**
+ * @var mixed
+ */
+ public $valueField;
+
+ /**
+ * @param string $name
+ *
+ * @return mixed
+ *
+ * @throws \InvalidArgumentException
+ */
+ public function & __get($name)
+ {
+ if ($name === 'value') {
+ return $this->valueField;
+ }
+
+ throw new InvalidArgumentException();
+ }
+}
View
24 tests/Doctrine/Tests/Common/Proxy/ProxyMagicMethodsTest.php
@@ -94,6 +94,28 @@ function (Proxy $proxy, $method, $params) use (&$counter) {
$this->assertSame(3, $counter);
}
+ /**
+ * @group DCOM-194
+ */
+ public function testInheritedMagicGetByRef()
+ {
+ $proxyClassName = $this->generateProxyClass(__NAMESPACE__ . '\\MagicGetByRefClass');
+ /* @var $proxy \Doctrine\Tests\Common\Proxy\MagicGetByRefClass */
+ $proxy = new $proxyClassName();
+ $proxy->valueField = 123;
+ $value = & $proxy->__get('value');
+
+ $this->assertSame(123, $value);
+
+ $value = 456;
+
+ $this->assertSame(456, $proxy->__get('value'), 'Value was fetched by reference');
+
+ $this->setExpectedException('InvalidArgumentException');
+
+ $undefined = $proxy->nonExisting;
+ }
+
public function testInheritedMagicSet()
{
$proxyClassName = $this->generateProxyClass(__NAMESPACE__ . '\\MagicSetClass');
@@ -282,4 +304,4 @@ private function generateProxyClass($className)
return $proxyClassName;
}
-}
+}
Please sign in to comment.
Something went wrong with that request. Please try again.