From 7f0d1fd33788d1375484152b7c7f79de8b47c05d Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Thu, 6 Feb 2014 10:22:26 +0100 Subject: [PATCH 1/3] Moving lazy loading constructor to its own class to remove duplication --- .../MethodGenerator/Constructor.php | 58 ++++++++++++++++ .../LazyLoadingGhostGenerator.php | 3 +- .../LazyLoadingValueHolderGenerator.php | 3 +- .../MethodGenerator/ConstructorTest.php | 67 +++++++++++++++++++ .../MethodGenerator/ConstructorTest.php | 10 +-- 5 files changed, 134 insertions(+), 7 deletions(-) create mode 100644 src/ProxyManager/ProxyGenerator/LazyLoading/MethodGenerator/Constructor.php create mode 100644 tests/ProxyManagerTest/ProxyGenerator/AccessInterceptorValueHolder/LazyLoading/MethodGenerator/ConstructorTest.php diff --git a/src/ProxyManager/ProxyGenerator/LazyLoading/MethodGenerator/Constructor.php b/src/ProxyManager/ProxyGenerator/LazyLoading/MethodGenerator/Constructor.php new file mode 100644 index 000000000..6f80e2f25 --- /dev/null +++ b/src/ProxyManager/ProxyGenerator/LazyLoading/MethodGenerator/Constructor.php @@ -0,0 +1,58 @@ + + * @license MIT + */ +class Constructor extends MethodGenerator +{ + /** + * Constructor + */ + public function __construct(ReflectionClass $originalClass, PropertyGenerator $initializerProperty) + { + parent::__construct('__construct'); + + $this->setParameter(new ParameterGenerator('initializer')); + + /* @var $publicProperties \ReflectionProperty[] */ + $publicProperties = $originalClass->getProperties(ReflectionProperty::IS_PUBLIC); + $unsetProperties = array(); + + foreach ($publicProperties as $publicProperty) { + $unsetProperties[] = '$this->' . $publicProperty->getName(); + } + + $this->setDocblock("@override constructor for lazy initialization\n\n@param \\Closure|null \$initializer"); + $this->setBody( + ($unsetProperties ? 'unset(' . implode(', ', $unsetProperties) . ");\n\n" : '') + . '$this->' . $initializerProperty->getName() . ' = $initializer;' + ); + } +} diff --git a/src/ProxyManager/ProxyGenerator/LazyLoadingGhostGenerator.php b/src/ProxyManager/ProxyGenerator/LazyLoadingGhostGenerator.php index 6a42fb1ed..7a2b4e10e 100644 --- a/src/ProxyManager/ProxyGenerator/LazyLoadingGhostGenerator.php +++ b/src/ProxyManager/ProxyGenerator/LazyLoadingGhostGenerator.php @@ -18,8 +18,9 @@ namespace ProxyManager\ProxyGenerator; +use ProxyManager\ProxyGenerator\LazyLoading\MethodGenerator\Constructor; + use ProxyManager\ProxyGenerator\LazyLoadingGhost\MethodGenerator\CallInitializer; -use ProxyManager\ProxyGenerator\LazyLoadingGhost\MethodGenerator\Constructor; use ProxyManager\ProxyGenerator\LazyLoadingGhost\MethodGenerator\GetProxyInitializer; use ProxyManager\ProxyGenerator\LazyLoadingGhost\MethodGenerator\InitializeProxy; use ProxyManager\ProxyGenerator\LazyLoadingGhost\MethodGenerator\IsProxyInitialized; diff --git a/src/ProxyManager/ProxyGenerator/LazyLoadingValueHolderGenerator.php b/src/ProxyManager/ProxyGenerator/LazyLoadingValueHolderGenerator.php index b7ded41a6..b4be5abd4 100644 --- a/src/ProxyManager/ProxyGenerator/LazyLoadingValueHolderGenerator.php +++ b/src/ProxyManager/ProxyGenerator/LazyLoadingValueHolderGenerator.php @@ -24,7 +24,8 @@ use ProxyManager\ProxyGenerator\AccessInterceptor\MethodGenerator\MagicWakeup; -use ProxyManager\ProxyGenerator\LazyLoadingValueHolder\MethodGenerator\Constructor; +use ProxyManager\ProxyGenerator\LazyLoading\MethodGenerator\Constructor; + use ProxyManager\ProxyGenerator\LazyLoadingValueHolder\MethodGenerator\GetProxyInitializer; use ProxyManager\ProxyGenerator\LazyLoadingValueHolder\MethodGenerator\InitializeProxy; use ProxyManager\ProxyGenerator\LazyLoadingValueHolder\MethodGenerator\IsProxyInitialized; diff --git a/tests/ProxyManagerTest/ProxyGenerator/AccessInterceptorValueHolder/LazyLoading/MethodGenerator/ConstructorTest.php b/tests/ProxyManagerTest/ProxyGenerator/AccessInterceptorValueHolder/LazyLoading/MethodGenerator/ConstructorTest.php new file mode 100644 index 000000000..83241558f --- /dev/null +++ b/tests/ProxyManagerTest/ProxyGenerator/AccessInterceptorValueHolder/LazyLoading/MethodGenerator/ConstructorTest.php @@ -0,0 +1,67 @@ + + * @license MIT + */ +class ConstructorTest extends PHPUnit_Framework_TestCase +{ + /** + * @covers \ProxyManager\ProxyGenerator\LazyLoadingGhost\MethodGenerator\Constructor::__construct + */ + public function testBodyStructure() + { + $initializer = $this->getMock('Zend\\Code\\Generator\\PropertyGenerator'); + $reflection = new ReflectionClass( + 'ProxyManagerTestAsset\\ProxyGenerator\\LazyLoading\\MethodGenerator\\ClassWithTwoPublicProperties' + ); + + $initializer->expects($this->any())->method('getName')->will($this->returnValue('foo')); + + $constructor = new Constructor($reflection, $initializer); + + $this->assertSame('__construct', $constructor->getName()); + $this->assertCount(1, $constructor->getParameters()); + $this->assertSame("unset(\$this->bar, \$this->baz);\n\n\$this->foo = \$initializer;", $constructor->getBody()); + } + + /** + * @covers \ProxyManager\ProxyGenerator\LazyLoadingGhost\MethodGenerator\Constructor::__construct + */ + public function testBodyStructureWithoutPublicProperties() + { + $initializer = $this->getMock('Zend\\Code\\Generator\\PropertyGenerator'); + + $initializer->expects($this->any())->method('getName')->will($this->returnValue('foo')); + + $constructor = new Constructor(new ReflectionClass('ProxyManagerTestAsset\\EmptyClass'), $initializer); + + $this->assertSame('__construct', $constructor->getName()); + $this->assertCount(1, $constructor->getParameters()); + $this->assertSame("\$this->foo = \$initializer;", $constructor->getBody()); + } +} diff --git a/tests/ProxyManagerTest/ProxyGenerator/LazyLoadingGhost/MethodGenerator/ConstructorTest.php b/tests/ProxyManagerTest/ProxyGenerator/LazyLoadingGhost/MethodGenerator/ConstructorTest.php index 83241558f..8f61250ae 100644 --- a/tests/ProxyManagerTest/ProxyGenerator/LazyLoadingGhost/MethodGenerator/ConstructorTest.php +++ b/tests/ProxyManagerTest/ProxyGenerator/LazyLoadingGhost/MethodGenerator/ConstructorTest.php @@ -16,14 +16,14 @@ * and is licensed under the MIT license. */ -namespace ProxyManagerTest\ProxyGenerator\LazyLoadingGhost\MethodGenerator; +namespace ProxyManagerTest\ProxyGenerator\LazyLoading\MethodGenerator; use PHPUnit_Framework_TestCase; -use ProxyManager\ProxyGenerator\LazyLoadingGhost\MethodGenerator\Constructor; +use ProxyManager\ProxyGenerator\LazyLoading\MethodGenerator\Constructor; use ReflectionClass; /** - * Tests for {@see \ProxyManager\ProxyGenerator\LazyLoadingGhost\MethodGenerator\Constructor} + * Tests for {@see \ProxyManager\ProxyGenerator\LazyLoading\MethodGenerator\Constructor} * * @author Marco Pivetta * @license MIT @@ -31,7 +31,7 @@ class ConstructorTest extends PHPUnit_Framework_TestCase { /** - * @covers \ProxyManager\ProxyGenerator\LazyLoadingGhost\MethodGenerator\Constructor::__construct + * @covers \ProxyManager\ProxyGenerator\LazyLoading\MethodGenerator\Constructor::__construct */ public function testBodyStructure() { @@ -50,7 +50,7 @@ public function testBodyStructure() } /** - * @covers \ProxyManager\ProxyGenerator\LazyLoadingGhost\MethodGenerator\Constructor::__construct + * @covers \ProxyManager\ProxyGenerator\LazyLoading\MethodGenerator\Constructor::__construct */ public function testBodyStructureWithoutPublicProperties() { From af462af4124b5d3b9ccbbe8ab956b01e4c32fbff Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Thu, 6 Feb 2014 10:32:36 +0100 Subject: [PATCH 2/3] Moving tests for the lazy loading constructor to their own namespace --- .../LazyLoading/MethodGenerator/ConstructorTest.php | 10 +++++----- .../MethodGenerator/ConstructorTest.php | 10 +++++----- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/tests/ProxyManagerTest/ProxyGenerator/AccessInterceptorValueHolder/LazyLoading/MethodGenerator/ConstructorTest.php b/tests/ProxyManagerTest/ProxyGenerator/AccessInterceptorValueHolder/LazyLoading/MethodGenerator/ConstructorTest.php index 83241558f..8f61250ae 100644 --- a/tests/ProxyManagerTest/ProxyGenerator/AccessInterceptorValueHolder/LazyLoading/MethodGenerator/ConstructorTest.php +++ b/tests/ProxyManagerTest/ProxyGenerator/AccessInterceptorValueHolder/LazyLoading/MethodGenerator/ConstructorTest.php @@ -16,14 +16,14 @@ * and is licensed under the MIT license. */ -namespace ProxyManagerTest\ProxyGenerator\LazyLoadingGhost\MethodGenerator; +namespace ProxyManagerTest\ProxyGenerator\LazyLoading\MethodGenerator; use PHPUnit_Framework_TestCase; -use ProxyManager\ProxyGenerator\LazyLoadingGhost\MethodGenerator\Constructor; +use ProxyManager\ProxyGenerator\LazyLoading\MethodGenerator\Constructor; use ReflectionClass; /** - * Tests for {@see \ProxyManager\ProxyGenerator\LazyLoadingGhost\MethodGenerator\Constructor} + * Tests for {@see \ProxyManager\ProxyGenerator\LazyLoading\MethodGenerator\Constructor} * * @author Marco Pivetta * @license MIT @@ -31,7 +31,7 @@ class ConstructorTest extends PHPUnit_Framework_TestCase { /** - * @covers \ProxyManager\ProxyGenerator\LazyLoadingGhost\MethodGenerator\Constructor::__construct + * @covers \ProxyManager\ProxyGenerator\LazyLoading\MethodGenerator\Constructor::__construct */ public function testBodyStructure() { @@ -50,7 +50,7 @@ public function testBodyStructure() } /** - * @covers \ProxyManager\ProxyGenerator\LazyLoadingGhost\MethodGenerator\Constructor::__construct + * @covers \ProxyManager\ProxyGenerator\LazyLoading\MethodGenerator\Constructor::__construct */ public function testBodyStructureWithoutPublicProperties() { diff --git a/tests/ProxyManagerTest/ProxyGenerator/LazyLoadingGhost/MethodGenerator/ConstructorTest.php b/tests/ProxyManagerTest/ProxyGenerator/LazyLoadingGhost/MethodGenerator/ConstructorTest.php index 8f61250ae..83241558f 100644 --- a/tests/ProxyManagerTest/ProxyGenerator/LazyLoadingGhost/MethodGenerator/ConstructorTest.php +++ b/tests/ProxyManagerTest/ProxyGenerator/LazyLoadingGhost/MethodGenerator/ConstructorTest.php @@ -16,14 +16,14 @@ * and is licensed under the MIT license. */ -namespace ProxyManagerTest\ProxyGenerator\LazyLoading\MethodGenerator; +namespace ProxyManagerTest\ProxyGenerator\LazyLoadingGhost\MethodGenerator; use PHPUnit_Framework_TestCase; -use ProxyManager\ProxyGenerator\LazyLoading\MethodGenerator\Constructor; +use ProxyManager\ProxyGenerator\LazyLoadingGhost\MethodGenerator\Constructor; use ReflectionClass; /** - * Tests for {@see \ProxyManager\ProxyGenerator\LazyLoading\MethodGenerator\Constructor} + * Tests for {@see \ProxyManager\ProxyGenerator\LazyLoadingGhost\MethodGenerator\Constructor} * * @author Marco Pivetta * @license MIT @@ -31,7 +31,7 @@ class ConstructorTest extends PHPUnit_Framework_TestCase { /** - * @covers \ProxyManager\ProxyGenerator\LazyLoading\MethodGenerator\Constructor::__construct + * @covers \ProxyManager\ProxyGenerator\LazyLoadingGhost\MethodGenerator\Constructor::__construct */ public function testBodyStructure() { @@ -50,7 +50,7 @@ public function testBodyStructure() } /** - * @covers \ProxyManager\ProxyGenerator\LazyLoading\MethodGenerator\Constructor::__construct + * @covers \ProxyManager\ProxyGenerator\LazyLoadingGhost\MethodGenerator\Constructor::__construct */ public function testBodyStructureWithoutPublicProperties() { From 3d90db2d9ab3e929841585afdf23d580e7a0d58a Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Thu, 6 Feb 2014 10:33:27 +0100 Subject: [PATCH 3/3] Removing code duplication --- .../MethodGenerator/Constructor.php | 58 ---------------- .../MethodGenerator/Constructor.php | 61 ----------------- .../MethodGenerator/ConstructorTest.php | 67 ------------------- .../MethodGenerator/ConstructorTest.php | 67 ------------------- 4 files changed, 253 deletions(-) delete mode 100644 src/ProxyManager/ProxyGenerator/LazyLoadingGhost/MethodGenerator/Constructor.php delete mode 100644 src/ProxyManager/ProxyGenerator/LazyLoadingValueHolder/MethodGenerator/Constructor.php delete mode 100644 tests/ProxyManagerTest/ProxyGenerator/LazyLoadingGhost/MethodGenerator/ConstructorTest.php delete mode 100644 tests/ProxyManagerTest/ProxyGenerator/LazyLoadingValueHolder/MethodGenerator/ConstructorTest.php diff --git a/src/ProxyManager/ProxyGenerator/LazyLoadingGhost/MethodGenerator/Constructor.php b/src/ProxyManager/ProxyGenerator/LazyLoadingGhost/MethodGenerator/Constructor.php deleted file mode 100644 index 95c7380a6..000000000 --- a/src/ProxyManager/ProxyGenerator/LazyLoadingGhost/MethodGenerator/Constructor.php +++ /dev/null @@ -1,58 +0,0 @@ - - * @license MIT - */ -class Constructor extends MethodGenerator -{ - /** - * Constructor - */ - public function __construct(ReflectionClass $originalClass, PropertyGenerator $initializerProperty) - { - parent::__construct('__construct'); - - $this->setParameter(new ParameterGenerator('initializer')); - - /* @var $publicProperties \ReflectionProperty[] */ - $publicProperties = $originalClass->getProperties(ReflectionProperty::IS_PUBLIC); - $unsetProperties = array(); - - foreach ($publicProperties as $publicProperty) { - $unsetProperties[] = '$this->' . $publicProperty->getName(); - } - - $this->setDocblock("@override constructor for lazy initialization\n\n@param \\Closure|null \$initializer"); - $this->setBody( - ($unsetProperties ? 'unset(' . implode(', ', $unsetProperties) . ");\n\n" : '') - . '$this->' . $initializerProperty->getName() . ' = $initializer;' - ); - } -} diff --git a/src/ProxyManager/ProxyGenerator/LazyLoadingValueHolder/MethodGenerator/Constructor.php b/src/ProxyManager/ProxyGenerator/LazyLoadingValueHolder/MethodGenerator/Constructor.php deleted file mode 100644 index 5607d15ef..000000000 --- a/src/ProxyManager/ProxyGenerator/LazyLoadingValueHolder/MethodGenerator/Constructor.php +++ /dev/null @@ -1,61 +0,0 @@ - - * @license MIT - */ -class Constructor extends MethodGenerator -{ - /** - * Constructor - * - * @param ReflectionClass $originalClass Reflection of the class to proxy - * @param PropertyGenerator $initializerProperty Initializer property - */ - public function __construct(ReflectionClass $originalClass, PropertyGenerator $initializerProperty) - { - parent::__construct('__construct'); - - $this->setParameter(new ParameterGenerator('initializer')); - - /* @var $publicProperties \ReflectionProperty[] */ - $publicProperties = $originalClass->getProperties(ReflectionProperty::IS_PUBLIC); - $unsetProperties = array(); - - foreach ($publicProperties as $publicProperty) { - $unsetProperties[] = '$this->' . $publicProperty->getName(); - } - - $this->setDocblock("@override constructor for lazy initialization\n\n@param \\Closure|null \$initializer"); - $this->setBody( - ($unsetProperties ? 'unset(' . implode(', ', $unsetProperties) . ");\n\n" : '') - . '$this->' . $initializerProperty->getName() . ' = $initializer;' - ); - } -} diff --git a/tests/ProxyManagerTest/ProxyGenerator/LazyLoadingGhost/MethodGenerator/ConstructorTest.php b/tests/ProxyManagerTest/ProxyGenerator/LazyLoadingGhost/MethodGenerator/ConstructorTest.php deleted file mode 100644 index 83241558f..000000000 --- a/tests/ProxyManagerTest/ProxyGenerator/LazyLoadingGhost/MethodGenerator/ConstructorTest.php +++ /dev/null @@ -1,67 +0,0 @@ - - * @license MIT - */ -class ConstructorTest extends PHPUnit_Framework_TestCase -{ - /** - * @covers \ProxyManager\ProxyGenerator\LazyLoadingGhost\MethodGenerator\Constructor::__construct - */ - public function testBodyStructure() - { - $initializer = $this->getMock('Zend\\Code\\Generator\\PropertyGenerator'); - $reflection = new ReflectionClass( - 'ProxyManagerTestAsset\\ProxyGenerator\\LazyLoading\\MethodGenerator\\ClassWithTwoPublicProperties' - ); - - $initializer->expects($this->any())->method('getName')->will($this->returnValue('foo')); - - $constructor = new Constructor($reflection, $initializer); - - $this->assertSame('__construct', $constructor->getName()); - $this->assertCount(1, $constructor->getParameters()); - $this->assertSame("unset(\$this->bar, \$this->baz);\n\n\$this->foo = \$initializer;", $constructor->getBody()); - } - - /** - * @covers \ProxyManager\ProxyGenerator\LazyLoadingGhost\MethodGenerator\Constructor::__construct - */ - public function testBodyStructureWithoutPublicProperties() - { - $initializer = $this->getMock('Zend\\Code\\Generator\\PropertyGenerator'); - - $initializer->expects($this->any())->method('getName')->will($this->returnValue('foo')); - - $constructor = new Constructor(new ReflectionClass('ProxyManagerTestAsset\\EmptyClass'), $initializer); - - $this->assertSame('__construct', $constructor->getName()); - $this->assertCount(1, $constructor->getParameters()); - $this->assertSame("\$this->foo = \$initializer;", $constructor->getBody()); - } -} diff --git a/tests/ProxyManagerTest/ProxyGenerator/LazyLoadingValueHolder/MethodGenerator/ConstructorTest.php b/tests/ProxyManagerTest/ProxyGenerator/LazyLoadingValueHolder/MethodGenerator/ConstructorTest.php deleted file mode 100644 index e914305a2..000000000 --- a/tests/ProxyManagerTest/ProxyGenerator/LazyLoadingValueHolder/MethodGenerator/ConstructorTest.php +++ /dev/null @@ -1,67 +0,0 @@ - - * @license MIT - */ -class ConstructorTest extends PHPUnit_Framework_TestCase -{ - /** - * @covers \ProxyManager\ProxyGenerator\LazyLoadingValueHolder\MethodGenerator\Constructor::__construct - */ - public function testBodyStructure() - { - $initializer = $this->getMock('Zend\\Code\\Generator\\PropertyGenerator'); - $reflection = new ReflectionClass( - 'ProxyManagerTestAsset\\ProxyGenerator\\LazyLoading\\MethodGenerator\\ClassWithTwoPublicProperties' - ); - - $initializer->expects($this->any())->method('getName')->will($this->returnValue('foo')); - - $constructor = new Constructor($reflection, $initializer); - - $this->assertSame('__construct', $constructor->getName()); - $this->assertCount(1, $constructor->getParameters()); - $this->assertSame("unset(\$this->bar, \$this->baz);\n\n\$this->foo = \$initializer;", $constructor->getBody()); - } - - /** - * @covers \ProxyManager\ProxyGenerator\LazyLoadingValueHolder\MethodGenerator\Constructor::__construct - */ - public function testBodyStructureWithoutPublicProperties() - { - $initializer = $this->getMock('Zend\\Code\\Generator\\PropertyGenerator'); - - $initializer->expects($this->any())->method('getName')->will($this->returnValue('foo')); - - $constructor = new Constructor(new ReflectionClass('ProxyManagerTestAsset\\EmptyClass'), $initializer); - - $this->assertSame('__construct', $constructor->getName()); - $this->assertCount(1, $constructor->getParameters()); - $this->assertSame("\$this->foo = \$initializer;", $constructor->getBody()); - } -}