Skip to content

Commit

Permalink
Merge pull request #900 from doctrine/3.0.x-merge-up-into-master_5eda…
Browse files Browse the repository at this point in the history
…7af9b7ab38.58753752

Merge release 3.0.2 into master
  • Loading branch information
greg0ire committed Jun 5, 2020
2 parents 8a3bd82 + a3c6479 commit c799163
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 24 deletions.
47 changes: 23 additions & 24 deletions lib/Doctrine/Common/Proxy/ProxyGenerator.php
Expand Up @@ -447,7 +447,7 @@ private function generateMagicGet(ClassMetadata $class)
$methodParameters = $methodReflection->getParameters();
$name = '$' . $methodParameters[0]->getName();

$parametersString = $this->buildParametersString($methodReflection->getParameters());
$parametersString = $this->buildParametersString($methodReflection->getParameters(), ['name']);
$returnTypeHint = $this->getMethodReturnType($methodReflection);
}

Expand All @@ -458,24 +458,23 @@ private function generateMagicGet(ClassMetadata $class)
$magicGet = <<<EOT
/**
* $inheritDoc
* @param string $name
* @param string \$name
*/
public function {$returnReference}__get($parametersString)$returnTypeHint
{
EOT;

if ( ! empty($lazyPublicProperties)) {
$magicGet .= sprintf(<<<'EOT'
if (\array_key_exists(%s, self::$lazyPropertiesNames)) {
$this->__initializer__ && $this->__initializer__->__invoke($this, '__get', [%s]);
EOT
, $name, $name);
$magicGet .= <<<'EOT'
if (\array_key_exists($name, self::$lazyPropertiesNames)) {
$this->__initializer__ && $this->__initializer__->__invoke($this, '__get', [$name]);
EOT;

if ($returnTypeHint === ': void') {
$magicGet .= "\n return;";
} else {
$magicGet .= "\n return \$this->$name;";
$magicGet .= "\n return \$this->\$name;";
}

$magicGet .= <<<'EOT'
Expand All @@ -487,24 +486,21 @@ public function {$returnReference}__get($parametersString)$returnTypeHint
}

if ($hasParentGet) {
$magicGet .= sprintf(<<<'EOT'
$this->__initializer__ && $this->__initializer__->__invoke($this, '__get', [%s]);
EOT
, $name);
$magicGet .= <<<'EOT'
$this->__initializer__ && $this->__initializer__->__invoke($this, '__get', [$name]);
EOT;

if ($returnTypeHint === ': void') {
$magicGet .= sprintf(<<<'EOT'
$magicGet .= <<<'EOT'
parent::__get(%s);
parent::__get($name);
return;
EOT
, $name);
EOT;
} else {
$magicGet .= sprintf(<<<'EOT'
$magicGet .= <<<'EOT'
return parent::__get(%s);
EOT
, $name);
return parent::__get($name);
EOT;
}
} else {
$magicGet .= sprintf(<<<EOT
Expand Down Expand Up @@ -533,7 +529,7 @@ private function generateMagicSet(ClassMetadata $class)

if ($hasParentSet) {
$methodReflection = $class->getReflectionClass()->getMethod('__set');
$parametersString = $this->buildParametersString($methodReflection->getParameters());
$parametersString = $this->buildParametersString($methodReflection->getParameters(), ['name', 'value']);
$returnTypeHint = $this->getMethodReturnType($methodReflection);
}

Expand Down Expand Up @@ -597,7 +593,7 @@ private function generateMagicIsset(ClassMetadata $class)

if ($hasParentIsset) {
$methodReflection = $class->getReflectionClass()->getMethod('__isset');
$parametersString = $this->buildParametersString($methodReflection->getParameters());
$parametersString = $this->buildParametersString($methodReflection->getParameters(), ['name']);
$returnTypeHint = $this->getMethodReturnType($methodReflection);
}

Expand Down Expand Up @@ -983,15 +979,18 @@ private function getLazyLoadedPublicProperties(ClassMetadata $class)

/**
* @param \ReflectionParameter[] $parameters
* @param string[] $renameParameters
*
* @return string
*/
private function buildParametersString(array $parameters)
private function buildParametersString(array $parameters, array $renameParameters = [])
{
$parameterDefinitions = [];

/* @var $param \ReflectionParameter */
$i = -1;
foreach ($parameters as $param) {
$i++;
$parameterDefinition = '';

if ($parameterType = $this->getParameterType($param)) {
Expand All @@ -1006,7 +1005,7 @@ private function buildParametersString(array $parameters)
$parameterDefinition .= '...';
}

$parameterDefinition .= '$' . $param->getName();
$parameterDefinition .= '$' . ($renameParameters ? $renameParameters[$i] : $param->getName());

if ($param->isDefaultValueAvailable()) {
$parameterDefinition .= ' = ' . var_export($param->getDefaultValue(), true);
Expand Down
@@ -0,0 +1,43 @@
<?php

namespace Doctrine\Tests\Common\Proxy;

/**
* Test asset class
*/
class MagicSetClassWithScalarTypeAndRenamedParameters
{
/**
* @var string
*/
public $id = 'id';

/**
* @var string
*/
public $publicField = 'publicField';

/**
* @var string|null
*/
public $testAttribute;

/**
* @param string $n
* @param mixed $val
*
* @throws \BadMethodCallException
*/
public function __set($n, $val)
{
if ($n === 'test') {
$this->testAttribute = $val;
}

if ($n === 'publicField' || $n === 'id') {
throw new \BadMethodCallException('Should never be called for "publicField" or "id"');
}

$this->testAttribute = $val;
}
}
29 changes: 29 additions & 0 deletions tests/Doctrine/Tests/Common/Proxy/ProxyMagicMethodsTest.php
Expand Up @@ -238,6 +238,35 @@ function (Proxy $proxy, $method, $params) use (&$counter) {
self::assertSame(3, $counter);
}

public function testInheritedMagicSetWithScalarTypeAndRenamedParameters()
{
$proxyClassName = $this->generateProxyClass(MagicSetClassWithScalarTypeAndRenamedParameters::class);
$proxy = new $proxyClassName(
function (Proxy $proxy, $method, $params) use (&$counter) {
if ( ! in_array($params[0], ['publicField', 'test', 'notDefined'])) {
throw new InvalidArgumentException('Unexpected access to field "' . $params[0] . '"');
}

$counter += 1;
}
);

self::assertSame('id', $proxy->id);

$proxy->publicField = 'publicFieldValue';

self::assertSame('publicFieldValue', $proxy->publicField);

$proxy->test = 'testValue';

self::assertSame('testValue', $proxy->testAttribute);

$proxy->notDefined = 'not defined';

self::assertSame('not defined', $proxy->testAttribute);
self::assertSame(3, $counter);
}

public function testInheritedMagicSleep()
{
$proxyClassName = $this->generateProxyClass(MagicSleepClass::class);
Expand Down

0 comments on commit c799163

Please sign in to comment.