Skip to content

Commit

Permalink
Merge branch '2.13.x' into 3.0.x
Browse files Browse the repository at this point in the history
* 2.13.x:
  Update wrong deprecation message in class loader
  Un-deprecate proxy logic
  Fix proxies with return types in magic methods
  • Loading branch information
alcaeus committed May 30, 2020
2 parents 05ab204 + 6902faf commit 402a424
Show file tree
Hide file tree
Showing 19 changed files with 475 additions and 53 deletions.
2 changes: 1 addition & 1 deletion lib/Doctrine/Common/ClassLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
* @author Roman Borschel <[email protected]>
* @since 2.0
*
* @deprecated The ClassLoader is deprecated and will be removed in version 3.0 of doctrine/common.
* @deprecated The ClassLoader is deprecated and will be removed in version 4.0 of doctrine/common.
*/
class ClassLoader
{
Expand Down
2 changes: 0 additions & 2 deletions lib/Doctrine/Common/Proxy/AbstractProxyFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
* Abstract factory for proxy objects.
*
* @author Benjamin Eberlei <[email protected]>
*
* @deprecated The Doctrine\Common\Proxy component is deprecated, please use ocramius/proxy-manager instead.
*/
abstract class AbstractProxyFactory
{
Expand Down
2 changes: 0 additions & 2 deletions lib/Doctrine/Common/Proxy/Autoloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
* @author Benjamin Eberlei <[email protected]>
*
* @internal
*
* @deprecated The Doctrine\Common\Proxy component is deprecated, please use ocramius/proxy-manager instead.
*/
class Autoloader
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
* @link www.doctrine-project.org
* @since 2.4
* @author Marco Pivetta <[email protected]>
*
* @deprecated The Doctrine\Common\Proxy component is deprecated, please use ocramius/proxy-manager instead.
*/
class InvalidArgumentException extends BaseInvalidArgumentException implements ProxyException
{
Expand Down
2 changes: 0 additions & 2 deletions lib/Doctrine/Common/Proxy/Exception/OutOfBoundsException.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
*
* @link www.doctrine-project.org
* @author Fredrik Wendel <[email protected]>
*
* @deprecated The Doctrine\Common\Proxy component is deprecated, please use ocramius/proxy-manager instead.
*/
class OutOfBoundsException extends BaseOutOfBoundsException implements ProxyException
{
Expand Down
2 changes: 0 additions & 2 deletions lib/Doctrine/Common/Proxy/Exception/ProxyException.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
* @link www.doctrine-project.org
* @since 2.4
* @author Marco Pivetta <[email protected]>
*
* @deprecated The Doctrine\Common\Proxy component is deprecated, please use ocramius/proxy-manager instead.
*/
interface ProxyException
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
* @link www.doctrine-project.org
* @since 2.4
* @author Marco Pivetta <[email protected]>
*
* @deprecated The Doctrine\Common\Proxy component is deprecated, please use ocramius/proxy-manager instead.
*/
class UnexpectedValueException extends BaseUnexpectedValueException implements ProxyException
{
Expand Down
2 changes: 0 additions & 2 deletions lib/Doctrine/Common/Proxy/Proxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
* @author Roman Borschel <[email protected]>
* @author Marco Pivetta <[email protected]>
* @since 2.4
*
* @deprecated The Doctrine\Common\Proxy component is deprecated, please use ocramius/proxy-manager instead.
*/
interface Proxy extends BaseProxy
{
Expand Down
2 changes: 0 additions & 2 deletions lib/Doctrine/Common/Proxy/ProxyDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
* Definition structure how to create a proxy.
*
* @author Benjamin Eberlei <[email protected]>
*
* @deprecated The Doctrine\Common\Proxy component is deprecated, please use ocramius/proxy-manager instead.
*/
class ProxyDefinition
{
Expand Down
111 changes: 77 additions & 34 deletions lib/Doctrine/Common/Proxy/ProxyGenerator.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php
namespace Doctrine\Common\Proxy;

use Doctrine\Persistence\Mapping\ClassMetadata;
use Doctrine\Common\Proxy\Exception\InvalidArgumentException;
use Doctrine\Common\Proxy\Exception\UnexpectedValueException;
use Doctrine\Common\Util\ClassUtils;
use Doctrine\Persistence\Mapping\ClassMetadata;
use function array_map;
use function method_exists;

Expand All @@ -14,8 +14,6 @@
*
* @author Marco Pivetta <[email protected]>
* @since 2.4
*
* @deprecated The Doctrine\Common\Proxy component is deprecated, please use ocramius/proxy-manager instead.
*/
class ProxyGenerator
{
Expand Down Expand Up @@ -433,14 +431,24 @@ private function generateMagicGet(ClassMetadata $class)
$hasParentGet = false;
$returnReference = '';
$inheritDoc = '';
$name = '$name';
$parametersString = '$name';
$returnTypeHint = null;

if ($reflectionClass->hasMethod('__get')) {
$hasParentGet = true;
$inheritDoc = '{@inheritDoc}';
$hasParentGet = true;
$inheritDoc = '{@inheritDoc}';
$methodReflection = $reflectionClass->getMethod('__get');

if ($reflectionClass->getMethod('__get')->returnsReference()) {
if ($methodReflection->returnsReference()) {
$returnReference = '& ';
}

$methodParameters = $methodReflection->getParameters();
$name = '$' . $methodParameters[0]->getName();

$parametersString = $this->buildParametersString($methodReflection->getParameters());
$returnTypeHint = $this->getMethodReturnType($methodReflection);
}

if (empty($lazyPublicProperties) && ! $hasParentGet) {
Expand All @@ -450,42 +458,63 @@ private function generateMagicGet(ClassMetadata $class)
$magicGet = <<<EOT
/**
* $inheritDoc
* @param string \$name
* @param string $name
*/
public function {$returnReference}__get(\$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);

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

$magicGet .= <<<'EOT'
if (\array_key_exists($name, self::$lazyPropertiesNames)) {
$this->__initializer__ && $this->__initializer__->__invoke($this, '__get', [$name]);
return $this->$name;
}


EOT;
}

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

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

if ($returnTypeHint === ': void') {
$magicGet .= sprintf(<<<'EOT'
parent::__get(%s);
return;
EOT
, $name);
} else {
$magicGet .= sprintf(<<<'EOT'
return parent::__get(%s);
EOT
, $name);
}
} else {
$magicGet .= <<<'EOT'
trigger_error(sprintf('Undefined property: %s::$%s', __CLASS__, $name), E_USER_NOTICE);
$magicGet .= sprintf(<<<EOT
trigger_error(sprintf('Undefined property: %%s::$%%s', __CLASS__, %s), E_USER_NOTICE);
EOT;
EOT
, $name);
}

$magicGet .= " }";

return $magicGet;
return $magicGet . "\n }";
}

/**
Expand All @@ -499,22 +528,31 @@ private function generateMagicSet(ClassMetadata $class)
{
$lazyPublicProperties = $this->getLazyLoadedPublicPropertiesNames($class);
$hasParentSet = $class->getReflectionClass()->hasMethod('__set');
$parametersString = '$name, $value';
$returnTypeHint = null;

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

if (empty($lazyPublicProperties) && ! $hasParentSet) {
return '';
}

$inheritDoc = $hasParentSet ? '{@inheritDoc}' : '';
$magicSet = <<<EOT
$magicSet = sprintf(<<<'EOT'
/**
* $inheritDoc
* @param string \$name
* @param mixed \$value
* %s
* @param string $name
* @param mixed $value
*/
public function __set(\$name, \$value)
public function __set(%s)%s
{

EOT;
EOT
, $inheritDoc, $parametersString, $returnTypeHint);

if ( ! empty($lazyPublicProperties)) {
$magicSet .= <<<'EOT'
Expand All @@ -540,9 +578,7 @@ public function __set(\$name, \$value)
$magicSet .= " \$this->\$name = \$value;";
}

$magicSet .= "\n }";

return $magicSet;
return $magicSet . "\n }";
}

/**
Expand All @@ -556,6 +592,14 @@ private function generateMagicIsset(ClassMetadata $class)
{
$lazyPublicProperties = $this->getLazyLoadedPublicPropertiesNames($class);
$hasParentIsset = $class->getReflectionClass()->hasMethod('__isset');
$parametersString = '$name';
$returnTypeHint = null;

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

if (empty($lazyPublicProperties) && ! $hasParentIsset) {
return '';
Expand All @@ -568,7 +612,7 @@ private function generateMagicIsset(ClassMetadata $class)
* @param string \$name
* @return boolean
*/
public function __isset(\$name)
public function __isset($parametersString)$returnTypeHint
{
EOT;
Expand All @@ -590,7 +634,6 @@ public function __isset(\$name)
$this->__initializer__ && $this->__initializer__->__invoke($this, '__isset', [$name]);
return parent::__isset($name);

EOT;
} else {
$magicIsset .= " return false;";
Expand Down
2 changes: 0 additions & 2 deletions lib/Doctrine/Common/Util/ClassUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
*
* @author Benjamin Eberlei <[email protected]>
* @author Johannes Schmitt <[email protected]>
*
* @deprecated The ClassUtils class is deprecated.
*/
class ClassUtils
{
Expand Down
3 changes: 3 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ parameters:
-
message: '#^Property Doctrine\\Tests\\Common\\Proxy\\ProxyLogicVoidReturnTypeTest::\$initializerCallbackMock \(callable&PHPUnit\\Framework\\MockObject\\MockObject\) does not accept PHPUnit\\Framework\\MockObject\\MockObject&stdClass\.$#'
path: '%currentWorkingDirectory%/tests/Doctrine/Tests/Common/Proxy/ProxyLogicVoidReturnTypeTest.php'
-
message: '#^Method Doctrine\\Tests\\Common\\Proxy\\MagicIssetClassWithInteger::__isset\(\) should return bool but returns int\.$#'
path: '%currentWorkingDirectory%/tests/Doctrine/Tests/Common/Proxy/MagicIssetClassWithInteger.php'

includes:
- vendor/phpstan/phpstan-phpunit/extension.neon
Expand Down
39 changes: 39 additions & 0 deletions tests/Doctrine/Tests/Common/Proxy/MagicGetClassWithScalarType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Doctrine\Tests\Common\Proxy;

/**
* Test asset class
* @author Jan Barasek <[email protected]>
*/
class MagicGetClassWithScalarType
{
/**
* @var string
*/
public $id = 'id';

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

/**
* @param string $name
*
* @return string
* @throws \BadMethodCallException
*/
public function __get(string $name): string
{
if ($name === 'test') {
return 'test';
}

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

return 'not defined';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Doctrine\Tests\Common\Proxy;

/**
* Test asset class
* @author Jan Barasek <[email protected]>
*/
class MagicGetClassWithScalarTypeAndRenamedParameter
{
/**
* @var string
*/
public $id = 'id';

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

/**
* @param string $n
*
* @return string
* @throws \BadMethodCallException
*/
public function __get(string $n): string
{
if ($n === 'test') {
return 'test';
}

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

return 'not defined';
}
}
Loading

0 comments on commit 402a424

Please sign in to comment.