From c95975bfe8b5c868d61a9fbfd0aa38b5d6900d33 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Thu, 23 Oct 2014 07:18:32 +0200 Subject: [PATCH 01/49] First attempt to write a test that verifies if ProxyManager will crash with defined classes --- .../FatalPreventionFunctionalTest.php | 95 +++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 tests/ProxyManagerTest/Functional/FatalPreventionFunctionalTest.php diff --git a/tests/ProxyManagerTest/Functional/FatalPreventionFunctionalTest.php b/tests/ProxyManagerTest/Functional/FatalPreventionFunctionalTest.php new file mode 100644 index 000000000..377b4fca6 --- /dev/null +++ b/tests/ProxyManagerTest/Functional/FatalPreventionFunctionalTest.php @@ -0,0 +1,95 @@ + + * @license MIT + * + * @group Functional + * @coversNothing + */ +class FatalPreventionFunctionalTest extends PHPUnit_Framework_TestCase +{ + private $template = <<<'PHP' +createProxy(%s, function () {}); +} catch (\ProxyManager\Exception\ExceptionInterface $e) { +} + +echo 'SUCCESS: ' . %s; +PHP; + + /** + * Verifies that proxies generated from different factories will retain their specific implementation + * and won't conflict + * + * @dataProvider getTestedClasses + */ + public function testLazyLoadingGhost($className) + { + $runner = PHPUnit_Util_PHP::factory(); + + $code = sprintf( + $this->template, + var_export(realpath(__DIR__ . '/../../../vendor/autoload.php'), true), + 'ProxyManager\\Factory\\LazyLoadingGhostFactory', + var_export($className, true), + var_export($className, true) + ); + + $result = $runner->runJob($code); + + if (('SUCCESS: ' . $className) !== $result['stdout']) { + $this->fail(sprintf( + "Crashed with class '%s'.\n\nStdout:\n%s\nStderr:\n%s\nGenerated code:\n%s'", + $className, + $result['stdout'], + $result['stderr'], + $code + )); + } + + $this->assertSame('SUCCESS: ' . $className, $result['stdout']); + } + + /** + * @return string[][] + */ + public function getTestedClasses() + { + return array_map( + function ($className) { + return array($className); + }, + get_declared_classes() + ); + } +} From 6095e776ea579bb3ef8c9aa38ec9802fb22d3d44 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Thu, 23 Oct 2014 07:19:33 +0200 Subject: [PATCH 02/49] Minor docblock fixes/clarifications --- .../Functional/FatalPreventionFunctionalTest.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/ProxyManagerTest/Functional/FatalPreventionFunctionalTest.php b/tests/ProxyManagerTest/Functional/FatalPreventionFunctionalTest.php index 377b4fca6..5b05ab8fa 100644 --- a/tests/ProxyManagerTest/Functional/FatalPreventionFunctionalTest.php +++ b/tests/ProxyManagerTest/Functional/FatalPreventionFunctionalTest.php @@ -48,8 +48,9 @@ class FatalPreventionFunctionalTest extends PHPUnit_Framework_TestCase PHP; /** - * Verifies that proxies generated from different factories will retain their specific implementation - * and won't conflict + * Verifies that lazy loading ghost will work with all given classes + * + * @param string $className a valid (existing/autoloadable) class name * * @dataProvider getTestedClasses */ From 9785349d702c5173cc80591b8461758635fb9b72 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Thu, 23 Oct 2014 07:19:56 +0200 Subject: [PATCH 03/49] Minor docblock fixes/clarifications --- .../Functional/FatalPreventionFunctionalTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/ProxyManagerTest/Functional/FatalPreventionFunctionalTest.php b/tests/ProxyManagerTest/Functional/FatalPreventionFunctionalTest.php index 5b05ab8fa..de0358dac 100644 --- a/tests/ProxyManagerTest/Functional/FatalPreventionFunctionalTest.php +++ b/tests/ProxyManagerTest/Functional/FatalPreventionFunctionalTest.php @@ -48,7 +48,7 @@ class FatalPreventionFunctionalTest extends PHPUnit_Framework_TestCase PHP; /** - * Verifies that lazy loading ghost will work with all given classes + * Verifies that lazy loading ghost creation will work with all given classes * * @param string $className a valid (existing/autoloadable) class name * From 10973fe1e8e0cf94f3ae719497f9c2c581f0861b Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Thu, 23 Oct 2014 07:23:39 +0200 Subject: [PATCH 04/49] Adding tests for lazy loading value holder instantiation with all defined classes --- .../FatalPreventionFunctionalTest.php | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/tests/ProxyManagerTest/Functional/FatalPreventionFunctionalTest.php b/tests/ProxyManagerTest/Functional/FatalPreventionFunctionalTest.php index de0358dac..553c4c358 100644 --- a/tests/ProxyManagerTest/Functional/FatalPreventionFunctionalTest.php +++ b/tests/ProxyManagerTest/Functional/FatalPreventionFunctionalTest.php @@ -81,6 +81,40 @@ public function testLazyLoadingGhost($className) $this->assertSame('SUCCESS: ' . $className, $result['stdout']); } + /** + * Verifies that lazy loading value holder creation will work with all given classes + * + * @param string $className a valid (existing/autoloadable) class name + * + * @dataProvider getTestedClasses + */ + public function testLazyLoadingValueHolder($className) + { + $runner = PHPUnit_Util_PHP::factory(); + + $code = sprintf( + $this->template, + var_export(realpath(__DIR__ . '/../../../vendor/autoload.php'), true), + 'ProxyManager\\Factory\\LazyLoadingValueHolderFactory', + var_export($className, true), + var_export($className, true) + ); + + $result = $runner->runJob($code); + + if (('SUCCESS: ' . $className) !== $result['stdout']) { + $this->fail(sprintf( + "Crashed with class '%s'.\n\nStdout:\n%s\nStderr:\n%s\nGenerated code:\n%s'", + $className, + $result['stdout'], + $result['stderr'], + $code + )); + } + + $this->assertSame('SUCCESS: ' . $className, $result['stdout']); + } + /** * @return string[][] */ From 1d4e9146f038e39694a25059e35d660c4f1deb4c Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Thu, 23 Oct 2014 07:31:11 +0200 Subject: [PATCH 05/49] Adding tests for null object instantiation with all defined classes --- .../FatalPreventionFunctionalTest.php | 42 +++++++++++++++++-- 1 file changed, 38 insertions(+), 4 deletions(-) diff --git a/tests/ProxyManagerTest/Functional/FatalPreventionFunctionalTest.php b/tests/ProxyManagerTest/Functional/FatalPreventionFunctionalTest.php index 553c4c358..3f1e07f74 100644 --- a/tests/ProxyManagerTest/Functional/FatalPreventionFunctionalTest.php +++ b/tests/ProxyManagerTest/Functional/FatalPreventionFunctionalTest.php @@ -40,7 +40,7 @@ class FatalPreventionFunctionalTest extends PHPUnit_Framework_TestCase $factory = new %s; try { - $factory->createProxy(%s, function () {}); + $factory->createProxy(%s); } catch (\ProxyManager\Exception\ExceptionInterface $e) { } @@ -62,7 +62,7 @@ public function testLazyLoadingGhost($className) $this->template, var_export(realpath(__DIR__ . '/../../../vendor/autoload.php'), true), 'ProxyManager\\Factory\\LazyLoadingGhostFactory', - var_export($className, true), + var_export($className, true) . ', function() {}', var_export($className, true) ); @@ -96,6 +96,40 @@ public function testLazyLoadingValueHolder($className) $this->template, var_export(realpath(__DIR__ . '/../../../vendor/autoload.php'), true), 'ProxyManager\\Factory\\LazyLoadingValueHolderFactory', + var_export($className, true) . ', function() {}', + var_export($className, true) + ); + + $result = $runner->runJob($code); + + if (('SUCCESS: ' . $className) !== $result['stdout']) { + $this->fail(sprintf( + "Crashed with class '%s'.\n\nStdout:\n%s\nStderr:\n%s\nGenerated code:\n%s'", + $className, + $result['stdout'], + $result['stderr'], + $code + )); + } + + $this->assertSame('SUCCESS: ' . $className, $result['stdout']); + } + + /** + * Verifies that null object creation will work with all given classes + * + * @param string $className a valid (existing/autoloadable) class name + * + * @dataProvider getTestedClasses + */ + public function testNullObjectFactory($className) + { + $runner = PHPUnit_Util_PHP::factory(); + + $code = sprintf( + $this->template, + var_export(realpath(__DIR__ . '/../../../vendor/autoload.php'), true), + 'ProxyManager\\Factory\\NullObjectFactory', var_export($className, true), var_export($className, true) ); @@ -120,11 +154,11 @@ public function testLazyLoadingValueHolder($className) */ public function getTestedClasses() { - return array_map( + return array_slice(array_map( function ($className) { return array($className); }, get_declared_classes() - ); + ), 0, 10); } } From 52f7c51d9ef24bb092cb996b9f121c606a5ed511 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Thu, 23 Oct 2014 07:42:59 +0200 Subject: [PATCH 06/49] Using the raw code generator in tests --- .../FatalPreventionFunctionalTest.php | 22 +++++++++++++------ 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/tests/ProxyManagerTest/Functional/FatalPreventionFunctionalTest.php b/tests/ProxyManagerTest/Functional/FatalPreventionFunctionalTest.php index 3f1e07f74..2d4660d40 100644 --- a/tests/ProxyManagerTest/Functional/FatalPreventionFunctionalTest.php +++ b/tests/ProxyManagerTest/Functional/FatalPreventionFunctionalTest.php @@ -37,10 +37,18 @@ class FatalPreventionFunctionalTest extends PHPUnit_Framework_TestCase require_once %s; -$factory = new %s; +$className = %s; +$generatedClass = new ProxyManager\Generator\ClassGenerator(uniqid('generated')); +$generatorStrategy = new ProxyManager\GeneratorStrategy\EvaluatingGeneratorStrategy(); +$classGenerator = new %s; +$classSignatureGenerator = new ProxyManager\Signature\ClassSignatureGenerator( + new ProxyManager\Signature\SignatureGenerator() +); try { - $factory->createProxy(%s); + $classGenerator->generate(new ReflectionClass($className), $generatedClass); + $classSignatureGenerator->addSignature($generatedClass, array('eval tests')); + $generatorStrategy->generate($generatedClass); } catch (\ProxyManager\Exception\ExceptionInterface $e) { } @@ -61,8 +69,8 @@ public function testLazyLoadingGhost($className) $code = sprintf( $this->template, var_export(realpath(__DIR__ . '/../../../vendor/autoload.php'), true), - 'ProxyManager\\Factory\\LazyLoadingGhostFactory', - var_export($className, true) . ', function() {}', + var_export($className, true), + 'ProxyManager\\ProxyGenerator\\LazyLoadingGhostGenerator', var_export($className, true) ); @@ -95,8 +103,8 @@ public function testLazyLoadingValueHolder($className) $code = sprintf( $this->template, var_export(realpath(__DIR__ . '/../../../vendor/autoload.php'), true), - 'ProxyManager\\Factory\\LazyLoadingValueHolderFactory', - var_export($className, true) . ', function() {}', + var_export($className, true), + 'ProxyManager\\ProxyGenerator\\LazyLoadingValueHolderGenerator', var_export($className, true) ); @@ -129,8 +137,8 @@ public function testNullObjectFactory($className) $code = sprintf( $this->template, var_export(realpath(__DIR__ . '/../../../vendor/autoload.php'), true), - 'ProxyManager\\Factory\\NullObjectFactory', var_export($className, true), + 'ProxyManager\\ProxyGenerator\\NullObjectGenerator', var_export($className, true) ); From 581f704060c170d8ddf27fa828adaa150e0d3d1d Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Thu, 23 Oct 2014 07:56:54 +0200 Subject: [PATCH 07/49] Mapping over existing classes and generators instead of manually defining single tests --- .../FatalPreventionFunctionalTest.php | 108 +++++------------- 1 file changed, 31 insertions(+), 77 deletions(-) diff --git a/tests/ProxyManagerTest/Functional/FatalPreventionFunctionalTest.php b/tests/ProxyManagerTest/Functional/FatalPreventionFunctionalTest.php index 2d4660d40..87307ef7a 100644 --- a/tests/ProxyManagerTest/Functional/FatalPreventionFunctionalTest.php +++ b/tests/ProxyManagerTest/Functional/FatalPreventionFunctionalTest.php @@ -56,13 +56,15 @@ class FatalPreventionFunctionalTest extends PHPUnit_Framework_TestCase PHP; /** - * Verifies that lazy loading ghost creation will work with all given classes + * Verifies that code generation and evaluation will not cause fatals with any given class * - * @param string $className a valid (existing/autoloadable) class name + * @param string $generatorClass an instantiable class (no arguments) implementing + * the {@see \ProxyManager\ProxyGenerator\ProxyGeneratorInterface} + * @param string $className a valid (existing/autoloadable) class name * * @dataProvider getTestedClasses */ - public function testLazyLoadingGhost($className) + public function testCodeGeneration($generatorClass, $className) { $runner = PHPUnit_Util_PHP::factory(); @@ -70,7 +72,7 @@ public function testLazyLoadingGhost($className) $this->template, var_export(realpath(__DIR__ . '/../../../vendor/autoload.php'), true), var_export($className, true), - 'ProxyManager\\ProxyGenerator\\LazyLoadingGhostGenerator', + $generatorClass, var_export($className, true) ); @@ -78,7 +80,8 @@ public function testLazyLoadingGhost($className) if (('SUCCESS: ' . $className) !== $result['stdout']) { $this->fail(sprintf( - "Crashed with class '%s'.\n\nStdout:\n%s\nStderr:\n%s\nGenerated code:\n%s'", + "Crashed with class '%s' and generator '%s'.\n\nStdout:\n%s\nStderr:\n%s\nGenerated code:\n%s'", + $generatorClass, $className, $result['stdout'], $result['stderr'], @@ -90,83 +93,34 @@ public function testLazyLoadingGhost($className) } /** - * Verifies that lazy loading value holder creation will work with all given classes - * - * @param string $className a valid (existing/autoloadable) class name - * - * @dataProvider getTestedClasses + * @return string[][] */ - public function testLazyLoadingValueHolder($className) + public function getTestedClasses() { - $runner = PHPUnit_Util_PHP::factory(); - - $code = sprintf( - $this->template, - var_export(realpath(__DIR__ . '/../../../vendor/autoload.php'), true), - var_export($className, true), + $generators = array( + 'ProxyManager\\ProxyGenerator\\AccessInterceptorScopeLocalizerGenerator', + 'ProxyManager\\ProxyGenerator\\AccessInterceptorValueHolderGenerator', + 'ProxyManager\\ProxyGenerator\\LazyLoadingGhostGenerator', 'ProxyManager\\ProxyGenerator\\LazyLoadingValueHolderGenerator', - var_export($className, true) - ); - - $result = $runner->runJob($code); - - if (('SUCCESS: ' . $className) !== $result['stdout']) { - $this->fail(sprintf( - "Crashed with class '%s'.\n\nStdout:\n%s\nStderr:\n%s\nGenerated code:\n%s'", - $className, - $result['stdout'], - $result['stderr'], - $code - )); - } - - $this->assertSame('SUCCESS: ' . $className, $result['stdout']); - } - - /** - * Verifies that null object creation will work with all given classes - * - * @param string $className a valid (existing/autoloadable) class name - * - * @dataProvider getTestedClasses - */ - public function testNullObjectFactory($className) - { - $runner = PHPUnit_Util_PHP::factory(); - - $code = sprintf( - $this->template, - var_export(realpath(__DIR__ . '/../../../vendor/autoload.php'), true), - var_export($className, true), 'ProxyManager\\ProxyGenerator\\NullObjectGenerator', - var_export($className, true) + 'ProxyManager\\ProxyGenerator\\RemoteObjectGenerator', ); - $result = $runner->runJob($code); - - if (('SUCCESS: ' . $className) !== $result['stdout']) { - $this->fail(sprintf( - "Crashed with class '%s'.\n\nStdout:\n%s\nStderr:\n%s\nGenerated code:\n%s'", - $className, - $result['stdout'], - $result['stderr'], - $code - )); - } - - $this->assertSame('SUCCESS: ' . $className, $result['stdout']); - } - - /** - * @return string[][] - */ - public function getTestedClasses() - { - return array_slice(array_map( - function ($className) { - return array($className); - }, - get_declared_classes() - ), 0, 10); + $classes = array_slice(get_declared_classes(), 0, 5); + + return call_user_func_array( + 'array_merge', + array_map( + function ($generator) use ($classes) { + return array_map( + function ($class) use ($generator) { + return array($generator, $class); + }, + $classes + ); + }, + $generators + ) + ); } } From cae8c477b3e9693117e5b9f5251dfa43a0303e04 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Thu, 23 Oct 2014 07:58:43 +0200 Subject: [PATCH 08/49] Removing limits over declared classes list - now testing all declared classes --- .../FatalPreventionFunctionalTest.php | 24 ++++++++----------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/tests/ProxyManagerTest/Functional/FatalPreventionFunctionalTest.php b/tests/ProxyManagerTest/Functional/FatalPreventionFunctionalTest.php index 87307ef7a..bca4475e5 100644 --- a/tests/ProxyManagerTest/Functional/FatalPreventionFunctionalTest.php +++ b/tests/ProxyManagerTest/Functional/FatalPreventionFunctionalTest.php @@ -97,29 +97,25 @@ public function testCodeGeneration($generatorClass, $className) */ public function getTestedClasses() { - $generators = array( - 'ProxyManager\\ProxyGenerator\\AccessInterceptorScopeLocalizerGenerator', - 'ProxyManager\\ProxyGenerator\\AccessInterceptorValueHolderGenerator', - 'ProxyManager\\ProxyGenerator\\LazyLoadingGhostGenerator', - 'ProxyManager\\ProxyGenerator\\LazyLoadingValueHolderGenerator', - 'ProxyManager\\ProxyGenerator\\NullObjectGenerator', - 'ProxyManager\\ProxyGenerator\\RemoteObjectGenerator', - ); - - $classes = array_slice(get_declared_classes(), 0, 5); - return call_user_func_array( 'array_merge', array_map( - function ($generator) use ($classes) { + function ($generator) { return array_map( function ($class) use ($generator) { return array($generator, $class); }, - $classes + get_declared_classes() ); }, - $generators + array( + 'ProxyManager\\ProxyGenerator\\AccessInterceptorScopeLocalizerGenerator', + 'ProxyManager\\ProxyGenerator\\AccessInterceptorValueHolderGenerator', + 'ProxyManager\\ProxyGenerator\\LazyLoadingGhostGenerator', + 'ProxyManager\\ProxyGenerator\\LazyLoadingValueHolderGenerator', + 'ProxyManager\\ProxyGenerator\\NullObjectGenerator', + 'ProxyManager\\ProxyGenerator\\RemoteObjectGenerator', + ) ) ); } From 75379c92f90acea9ae884f30ac96401d8e723859 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Thu, 23 Oct 2014 08:05:56 +0200 Subject: [PATCH 09/49] Skipping `final` classes when generating proxies: throwing an exception instead. --- .../Exception/InvalidProxiedClassException.php | 10 ++++++++++ .../AccessInterceptorScopeLocalizerGenerator.php | 4 ++++ .../AccessInterceptorValueHolderGenerator.php | 5 +++++ .../ProxyGenerator/LazyLoadingGhostGenerator.php | 5 +++++ .../ProxyGenerator/LazyLoadingValueHolderGenerator.php | 5 +++++ .../ProxyGenerator/NullObjectGenerator.php | 5 +++++ .../ProxyGenerator/RemoteObjectGenerator.php | 5 +++++ 7 files changed, 39 insertions(+) diff --git a/src/ProxyManager/Exception/InvalidProxiedClassException.php b/src/ProxyManager/Exception/InvalidProxiedClassException.php index fff18b75b..011579112 100644 --- a/src/ProxyManager/Exception/InvalidProxiedClassException.php +++ b/src/ProxyManager/Exception/InvalidProxiedClassException.php @@ -38,4 +38,14 @@ public static function interfaceNotSupported(ReflectionClass $reflection) { return new self(sprintf('Provided interface "%s" cannot be proxied', $reflection->getName())); } + + /** + * @param ReflectionClass $reflection + * + * @return self + */ + public static function finalClassNotSupported(ReflectionClass $reflection) + { + return new self(sprintf('Provided class "%s" is final and cannot be proxied', $reflection->getName())); + } } diff --git a/src/ProxyManager/ProxyGenerator/AccessInterceptorScopeLocalizerGenerator.php b/src/ProxyManager/ProxyGenerator/AccessInterceptorScopeLocalizerGenerator.php index c9ecd127f..ad594edf6 100644 --- a/src/ProxyManager/ProxyGenerator/AccessInterceptorScopeLocalizerGenerator.php +++ b/src/ProxyManager/ProxyGenerator/AccessInterceptorScopeLocalizerGenerator.php @@ -58,6 +58,10 @@ public function generate(ReflectionClass $originalClass, ClassGenerator $classGe throw InvalidProxiedClassException::interfaceNotSupported($originalClass); } + if ($originalClass->isFinal()) { + throw InvalidProxiedClassException::finalClassNotSupported($originalClass); + } + $classGenerator->setExtendedClass($originalClass->getName()); $classGenerator->setImplementedInterfaces(array('ProxyManager\\Proxy\\AccessInterceptorInterface')); $classGenerator->addPropertyFromGenerator($prefixInterceptors = new MethodPrefixInterceptors()); diff --git a/src/ProxyManager/ProxyGenerator/AccessInterceptorValueHolderGenerator.php b/src/ProxyManager/ProxyGenerator/AccessInterceptorValueHolderGenerator.php index 8b6a3aafd..19f7c1d75 100644 --- a/src/ProxyManager/ProxyGenerator/AccessInterceptorValueHolderGenerator.php +++ b/src/ProxyManager/ProxyGenerator/AccessInterceptorValueHolderGenerator.php @@ -18,6 +18,7 @@ namespace ProxyManager\ProxyGenerator; +use ProxyManager\Exception\InvalidProxiedClassException; use ProxyManager\Generator\Util\ClassGeneratorUtils; use ProxyManager\ProxyGenerator\AccessInterceptor\MethodGenerator\MagicWakeup; use ProxyManager\ProxyGenerator\AccessInterceptor\MethodGenerator\SetMethodPrefixInterceptor; @@ -58,6 +59,10 @@ class AccessInterceptorValueHolderGenerator implements ProxyGeneratorInterface */ public function generate(ReflectionClass $originalClass, ClassGenerator $classGenerator) { + if ($originalClass->isFinal()) { + throw InvalidProxiedClassException::finalClassNotSupported($originalClass); + } + $publicProperties = new PublicPropertiesMap($originalClass); $interfaces = array( 'ProxyManager\\Proxy\\AccessInterceptorInterface', diff --git a/src/ProxyManager/ProxyGenerator/LazyLoadingGhostGenerator.php b/src/ProxyManager/ProxyGenerator/LazyLoadingGhostGenerator.php index 2a6a0c92f..db772a3b7 100644 --- a/src/ProxyManager/ProxyGenerator/LazyLoadingGhostGenerator.php +++ b/src/ProxyManager/ProxyGenerator/LazyLoadingGhostGenerator.php @@ -18,6 +18,7 @@ namespace ProxyManager\ProxyGenerator; +use ProxyManager\Exception\InvalidProxiedClassException; use ProxyManager\Generator\Util\ClassGeneratorUtils; use ProxyManager\ProxyGenerator\LazyLoading\MethodGenerator\Constructor; use ProxyManager\ProxyGenerator\LazyLoadingGhost\MethodGenerator\CallInitializer; @@ -58,6 +59,10 @@ class LazyLoadingGhostGenerator implements ProxyGeneratorInterface */ public function generate(ReflectionClass $originalClass, ClassGenerator $classGenerator) { + if ($originalClass->isFinal()) { + throw InvalidProxiedClassException::finalClassNotSupported($originalClass); + } + $interfaces = array('ProxyManager\\Proxy\\GhostObjectInterface'); $publicProperties = new PublicPropertiesMap($originalClass); $publicPropsDefaults = new PublicPropertiesDefaults($originalClass); diff --git a/src/ProxyManager/ProxyGenerator/LazyLoadingValueHolderGenerator.php b/src/ProxyManager/ProxyGenerator/LazyLoadingValueHolderGenerator.php index c80de7391..efb54f17b 100644 --- a/src/ProxyManager/ProxyGenerator/LazyLoadingValueHolderGenerator.php +++ b/src/ProxyManager/ProxyGenerator/LazyLoadingValueHolderGenerator.php @@ -18,6 +18,7 @@ namespace ProxyManager\ProxyGenerator; +use ProxyManager\Exception\InvalidProxiedClassException; use ProxyManager\ProxyGenerator\AccessInterceptor\MethodGenerator\MagicWakeup; use ProxyManager\ProxyGenerator\LazyLoading\MethodGenerator\Constructor; use ProxyManager\ProxyGenerator\LazyLoadingValueHolder\MethodGenerator\GetProxyInitializer; @@ -58,6 +59,10 @@ class LazyLoadingValueHolderGenerator implements ProxyGeneratorInterface */ public function generate(ReflectionClass $originalClass, ClassGenerator $classGenerator) { + if ($originalClass->isFinal()) { + throw InvalidProxiedClassException::finalClassNotSupported($originalClass); + } + $interfaces = array('ProxyManager\\Proxy\\VirtualProxyInterface'); $publicProperties = new PublicPropertiesMap($originalClass); diff --git a/src/ProxyManager/ProxyGenerator/NullObjectGenerator.php b/src/ProxyManager/ProxyGenerator/NullObjectGenerator.php index 20437c45d..f57d71239 100644 --- a/src/ProxyManager/ProxyGenerator/NullObjectGenerator.php +++ b/src/ProxyManager/ProxyGenerator/NullObjectGenerator.php @@ -18,6 +18,7 @@ namespace ProxyManager\ProxyGenerator; +use ProxyManager\Exception\InvalidProxiedClassException; use ProxyManager\Generator\Util\ClassGeneratorUtils; use ProxyManager\ProxyGenerator\NullObject\MethodGenerator\Constructor; use ProxyManager\ProxyGenerator\NullObject\MethodGenerator\NullObjectMethodInterceptor; @@ -41,6 +42,10 @@ class NullObjectGenerator implements ProxyGeneratorInterface */ public function generate(ReflectionClass $originalClass, ClassGenerator $classGenerator) { + if ($originalClass->isFinal()) { + throw InvalidProxiedClassException::finalClassNotSupported($originalClass); + } + $interfaces = array('ProxyManager\\Proxy\\NullObjectInterface'); if ($originalClass->isInterface()) { diff --git a/src/ProxyManager/ProxyGenerator/RemoteObjectGenerator.php b/src/ProxyManager/ProxyGenerator/RemoteObjectGenerator.php index 19352e73e..4e390f3f7 100644 --- a/src/ProxyManager/ProxyGenerator/RemoteObjectGenerator.php +++ b/src/ProxyManager/ProxyGenerator/RemoteObjectGenerator.php @@ -18,6 +18,7 @@ namespace ProxyManager\ProxyGenerator; +use ProxyManager\Exception\InvalidProxiedClassException; use ProxyManager\Generator\Util\ClassGeneratorUtils; use ProxyManager\ProxyGenerator\RemoteObject\MethodGenerator\Constructor; use ProxyManager\ProxyGenerator\RemoteObject\MethodGenerator\MagicGet; @@ -48,6 +49,10 @@ class RemoteObjectGenerator implements ProxyGeneratorInterface */ public function generate(ReflectionClass $originalClass, ClassGenerator $classGenerator) { + if ($originalClass->isFinal()) { + throw InvalidProxiedClassException::finalClassNotSupported($originalClass); + } + $interfaces = array('ProxyManager\\Proxy\\RemoteObjectInterface'); if ($originalClass->isInterface()) { From cddb91bc7f1765dc1a35491a223567aa8a74d4a2 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Sat, 25 Oct 2014 04:58:57 +0200 Subject: [PATCH 10/49] Skipping over non-test-asset classes that are found in the project, allowing not found classes --- .../FatalPreventionFunctionalTest.php | 49 +++++++++++++++++-- 1 file changed, 45 insertions(+), 4 deletions(-) diff --git a/tests/ProxyManagerTest/Functional/FatalPreventionFunctionalTest.php b/tests/ProxyManagerTest/Functional/FatalPreventionFunctionalTest.php index bca4475e5..d5cee6b8a 100644 --- a/tests/ProxyManagerTest/Functional/FatalPreventionFunctionalTest.php +++ b/tests/ProxyManagerTest/Functional/FatalPreventionFunctionalTest.php @@ -20,6 +20,7 @@ use PHPUnit_Framework_TestCase; use PHPUnit_Util_PHP; +use ReflectionClass; /** * Verifies that proxy-manager will not attempt to `eval()` code that will cause fatal errors @@ -49,7 +50,8 @@ class FatalPreventionFunctionalTest extends PHPUnit_Framework_TestCase $classGenerator->generate(new ReflectionClass($className), $generatedClass); $classSignatureGenerator->addSignature($generatedClass, array('eval tests')); $generatorStrategy->generate($generatedClass); -} catch (\ProxyManager\Exception\ExceptionInterface $e) { +} catch (ProxyManager\Exception\ExceptionInterface $e) { +} catch (ReflectionException $e) { } echo 'SUCCESS: ' . %s; @@ -76,7 +78,7 @@ public function testCodeGeneration($generatorClass, $className) var_export($className, true) ); - $result = $runner->runJob($code); + $result = $runner->runJob($code, array('n')); if (('SUCCESS: ' . $className) !== $result['stdout']) { $this->fail(sprintf( @@ -97,15 +99,17 @@ public function testCodeGeneration($generatorClass, $className) */ public function getTestedClasses() { + $that = $this; + return call_user_func_array( 'array_merge', array_map( - function ($generator) { + function ($generator) use ($that) { return array_map( function ($class) use ($generator) { return array($generator, $class); }, - get_declared_classes() + $that->getProxyTestedClasses() ); }, array( @@ -119,4 +123,41 @@ function ($class) use ($generator) { ) ); } + + /** + * @private (public only for PHP 5.3 compatibility) + * + * @return string[] + */ + public function getProxyTestedClasses() + { + $skippedPaths = array( + realpath(__DIR__ . '/../../src'), + realpath(__DIR__ . '/../../vendor'), + realpath(__DIR__ . '/../../tests/ProxyManagerTest'), + ); + + return array_filter( + get_declared_classes(), + function ($className) use ($skippedPaths) { + $reflectionClass = new ReflectionClass($className); + $fileName = $reflectionClass->getFileName(); + + if (! $fileName) { + return false; + } + + $realPath = realpath($fileName); + + foreach ($skippedPaths as $skippedPath) { + if (0 === strpos($realPath, $skippedPath)) { + // skip classes defined within ProxyManager, vendor or the test suite + return false; + } + } + + return true; + } + ); + } } From 1f78150a8f2e7db48c3e0abb03434182733970bd Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Sat, 25 Oct 2014 05:00:21 +0200 Subject: [PATCH 11/49] Passing `-n` as parameter (should run without extensions) --- .../Functional/FatalPreventionFunctionalTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/ProxyManagerTest/Functional/FatalPreventionFunctionalTest.php b/tests/ProxyManagerTest/Functional/FatalPreventionFunctionalTest.php index d5cee6b8a..276fd3ccd 100644 --- a/tests/ProxyManagerTest/Functional/FatalPreventionFunctionalTest.php +++ b/tests/ProxyManagerTest/Functional/FatalPreventionFunctionalTest.php @@ -78,7 +78,7 @@ public function testCodeGeneration($generatorClass, $className) var_export($className, true) ); - $result = $runner->runJob($code, array('n')); + $result = $runner->runJob($code, array('-n')); if (('SUCCESS: ' . $className) !== $result['stdout']) { $this->fail(sprintf( From c0064af42c55d858367bead08c1d3fc9845e788a Mon Sep 17 00:00:00 2001 From: malukenho Date: Mon, 27 Oct 2014 09:30:52 -0300 Subject: [PATCH 12/49] Remove duplication of interfaces --- src/ProxyManager/Generator/ClassGenerator.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/ProxyManager/Generator/ClassGenerator.php b/src/ProxyManager/Generator/ClassGenerator.php index 0304c42f2..3726570cb 100644 --- a/src/ProxyManager/Generator/ClassGenerator.php +++ b/src/ProxyManager/Generator/ClassGenerator.php @@ -45,6 +45,8 @@ public function setExtendedClass($extendedClass) */ public function setImplementedInterfaces(array $interfaces) { + $interfaces = array_diff($interfaces, $this->getImplementedInterfaces()); + foreach ($interfaces as & $interface) { $interface = '\\' . trim($interface, '\\'); } From 3ed8eba9622cc3ac4949193b159b2596318e699a Mon Sep 17 00:00:00 2001 From: malukenho Date: Mon, 27 Oct 2014 11:55:11 -0300 Subject: [PATCH 13/49] Turn interfaces unique on collection --- src/ProxyManager/Generator/ClassGenerator.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ProxyManager/Generator/ClassGenerator.php b/src/ProxyManager/Generator/ClassGenerator.php index 3726570cb..51aa51aa1 100644 --- a/src/ProxyManager/Generator/ClassGenerator.php +++ b/src/ProxyManager/Generator/ClassGenerator.php @@ -45,7 +45,7 @@ public function setExtendedClass($extendedClass) */ public function setImplementedInterfaces(array $interfaces) { - $interfaces = array_diff($interfaces, $this->getImplementedInterfaces()); + $interfaces = array_diff(array_unique($interfaces), $this->getImplementedInterfaces()); foreach ($interfaces as & $interface) { $interface = '\\' . trim($interface, '\\'); From c5a0a7f5c1058d43572b6f80d49e99b869d1cef4 Mon Sep 17 00:00:00 2001 From: malukenho Date: Mon, 27 Oct 2014 12:04:30 -0300 Subject: [PATCH 14/49] Generate abstract methods to be compliance with method signatures --- .../MethodGenerator/AbstractMethod.php | 53 +++++++++++++++++++ ...cessInterceptorScopeLocalizerGenerator.php | 2 + 2 files changed, 55 insertions(+) create mode 100644 src/ProxyManager/ProxyGenerator/AccessInterceptorScopeLocalizer/MethodGenerator/AbstractMethod.php diff --git a/src/ProxyManager/ProxyGenerator/AccessInterceptorScopeLocalizer/MethodGenerator/AbstractMethod.php b/src/ProxyManager/ProxyGenerator/AccessInterceptorScopeLocalizer/MethodGenerator/AbstractMethod.php new file mode 100644 index 000000000..77066a454 --- /dev/null +++ b/src/ProxyManager/ProxyGenerator/AccessInterceptorScopeLocalizer/MethodGenerator/AbstractMethod.php @@ -0,0 +1,53 @@ + + * @license MIT + */ +class AbstractMethod extends MethodGenerator +{ + public function __construct( + ReflectionClass $originalClass, + PropertyGenerator $prefixInterceptors, + PropertyGenerator $suffixInterceptors + ) { + $abstractMethods = $originalClass->getMethods(ReflectionMethod::IS_ABSTRACT); + + foreach($abstractMethods as $method) + { + parent::__construct($method->getName()); + + foreach ($method->getParameters() as $parameter) { + $this->setParameter($parameter->getName()); + } + } + } +} diff --git a/src/ProxyManager/ProxyGenerator/AccessInterceptorScopeLocalizerGenerator.php b/src/ProxyManager/ProxyGenerator/AccessInterceptorScopeLocalizerGenerator.php index ad594edf6..9babc0755 100644 --- a/src/ProxyManager/ProxyGenerator/AccessInterceptorScopeLocalizerGenerator.php +++ b/src/ProxyManager/ProxyGenerator/AccessInterceptorScopeLocalizerGenerator.php @@ -23,6 +23,7 @@ use ProxyManager\ProxyGenerator\AccessInterceptor\MethodGenerator\SetMethodPrefixInterceptor; use ProxyManager\ProxyGenerator\AccessInterceptor\MethodGenerator\SetMethodSuffixInterceptor; use ProxyManager\ProxyGenerator\AccessInterceptor\PropertyGenerator\MethodPrefixInterceptors; +use ProxyManager\ProxyGenerator\AccessInterceptorScopeLocalizer\MethodGenerator\AbstractMethod; use ProxyManager\ProxyGenerator\AccessInterceptorScopeLocalizer\MethodGenerator\Constructor; use ProxyManager\ProxyGenerator\AccessInterceptorScopeLocalizer\MethodGenerator\InterceptedMethod; use ProxyManager\ProxyGenerator\AccessInterceptorScopeLocalizer\MethodGenerator\MagicClone; @@ -87,6 +88,7 @@ function (ReflectionMethod $method) use ($prefixInterceptors, $suffixInterceptor ), array( new Constructor($originalClass, $prefixInterceptors, $suffixInterceptors), + new AbstractMethod($originalClass, $prefixInterceptors, $suffixInterceptors), new SetMethodPrefixInterceptor($prefixInterceptors), new SetMethodSuffixInterceptor($suffixInterceptors), new MagicGet($originalClass, $prefixInterceptors, $suffixInterceptors), From 2051528a5a8d889793c541fb3c3cfc8aa6da9453 Mon Sep 17 00:00:00 2001 From: malukenho Date: Mon, 27 Oct 2014 15:39:51 -0300 Subject: [PATCH 15/49] Fix abstract methods inheritance --- .../Generator/Util/ClassGeneratorUtils.php | 15 ++++++++- .../MethodGenerator/AbstractMethod.php | 31 ++++++++++++------- ...cessInterceptorScopeLocalizerGenerator.php | 5 ++- .../AccessInterceptorValueHolderGenerator.php | 5 +++ .../LazyLoadingGhostGenerator.php | 5 +++ .../LazyLoadingValueHolderGenerator.php | 5 +++ .../ProxyGenerator/RemoteObjectGenerator.php | 5 +++ 7 files changed, 57 insertions(+), 14 deletions(-) diff --git a/src/ProxyManager/Generator/Util/ClassGeneratorUtils.php b/src/ProxyManager/Generator/Util/ClassGeneratorUtils.php index 827da2e14..f612a82c3 100644 --- a/src/ProxyManager/Generator/Util/ClassGeneratorUtils.php +++ b/src/ProxyManager/Generator/Util/ClassGeneratorUtils.php @@ -19,6 +19,7 @@ namespace ProxyManager\Generator\Util; use ReflectionClass; +use ReflectionMethod; use Zend\Code\Generator\MethodGenerator; use Zend\Code\Generator\ClassGenerator as GeneratorClass; @@ -44,10 +45,22 @@ public static function addMethodIfNotFinal( ) { $methodName = $generatedMethod->getName(); - if ($originalClass->hasMethod($methodName) && $originalClass->getMethod($methodName)->isFinal()) { + if ($classGenerator->hasMethod($methodName) + || ($originalClass->hasMethod($methodName) && $originalClass->getMethod($methodName)->isFinal())) { return false; } $classGenerator->addMethodFromGenerator($generatedMethod); } + + public static function getAbstractMethods($originalClass) + { + $methodList = array(); + $abstractMethods = $originalClass->getMethods(ReflectionMethod::IS_ABSTRACT); + foreach ($abstractMethods as $method) { + $methodList[] = $method->getName(); + } + + return $methodList; + } } diff --git a/src/ProxyManager/ProxyGenerator/AccessInterceptorScopeLocalizer/MethodGenerator/AbstractMethod.php b/src/ProxyManager/ProxyGenerator/AccessInterceptorScopeLocalizer/MethodGenerator/AbstractMethod.php index 77066a454..49ee3f55b 100644 --- a/src/ProxyManager/ProxyGenerator/AccessInterceptorScopeLocalizer/MethodGenerator/AbstractMethod.php +++ b/src/ProxyManager/ProxyGenerator/AccessInterceptorScopeLocalizer/MethodGenerator/AbstractMethod.php @@ -34,20 +34,27 @@ */ class AbstractMethod extends MethodGenerator { - public function __construct( - ReflectionClass $originalClass, - PropertyGenerator $prefixInterceptors, - PropertyGenerator $suffixInterceptors - ) { - $abstractMethods = $originalClass->getMethods(ReflectionMethod::IS_ABSTRACT); + public function __construct(ReflectionClass $originalClass, $name) + { + parent::__construct($name); + $method = $originalClass->getMethod($name); - foreach($abstractMethods as $method) - { - parent::__construct($method->getName()); + foreach ($method->getParameters() as $param) { + $this->setParameter($param->getName()); + } + + $this->setBody(''); + } - foreach ($method->getParameters() as $parameter) { - $this->setParameter($parameter->getName()); - } + public static function createCollection(ReflectionClass $originalClass, array $methods) + { + $methodCollection = array(); + foreach ($methods as $methodName) { + $methodCollection[] = new self($originalClass, $methodName); + } + if ('PHPUnit_Runner_BaseTestRunner' == $originalClass->getName()) { + //print_r($methodCollection); } + return $methodCollection; } } diff --git a/src/ProxyManager/ProxyGenerator/AccessInterceptorScopeLocalizerGenerator.php b/src/ProxyManager/ProxyGenerator/AccessInterceptorScopeLocalizerGenerator.php index 9babc0755..86db00634 100644 --- a/src/ProxyManager/ProxyGenerator/AccessInterceptorScopeLocalizerGenerator.php +++ b/src/ProxyManager/ProxyGenerator/AccessInterceptorScopeLocalizerGenerator.php @@ -88,7 +88,6 @@ function (ReflectionMethod $method) use ($prefixInterceptors, $suffixInterceptor ), array( new Constructor($originalClass, $prefixInterceptors, $suffixInterceptors), - new AbstractMethod($originalClass, $prefixInterceptors, $suffixInterceptors), new SetMethodPrefixInterceptor($prefixInterceptors), new SetMethodSuffixInterceptor($suffixInterceptors), new MagicGet($originalClass, $prefixInterceptors, $suffixInterceptors), @@ -97,6 +96,10 @@ function (ReflectionMethod $method) use ($prefixInterceptors, $suffixInterceptor new MagicUnset($originalClass, $prefixInterceptors, $suffixInterceptors), new MagicSleep($originalClass, $prefixInterceptors, $suffixInterceptors), new MagicClone($originalClass, $prefixInterceptors, $suffixInterceptors), + ), + AbstractMethod::createCollection( + $originalClass, + ClassGeneratorUtils::getAbstractMethods($originalClass) ) ) ); diff --git a/src/ProxyManager/ProxyGenerator/AccessInterceptorValueHolderGenerator.php b/src/ProxyManager/ProxyGenerator/AccessInterceptorValueHolderGenerator.php index 19f7c1d75..4e7e3fd50 100644 --- a/src/ProxyManager/ProxyGenerator/AccessInterceptorValueHolderGenerator.php +++ b/src/ProxyManager/ProxyGenerator/AccessInterceptorValueHolderGenerator.php @@ -25,6 +25,7 @@ use ProxyManager\ProxyGenerator\AccessInterceptor\MethodGenerator\SetMethodSuffixInterceptor; use ProxyManager\ProxyGenerator\AccessInterceptor\PropertyGenerator\MethodPrefixInterceptors; use ProxyManager\ProxyGenerator\AccessInterceptor\PropertyGenerator\MethodSuffixInterceptors; +use ProxyManager\ProxyGenerator\AccessInterceptorScopeLocalizer\MethodGenerator\AbstractMethod; use ProxyManager\ProxyGenerator\AccessInterceptorValueHolder\MethodGenerator\Constructor; use ProxyManager\ProxyGenerator\AccessInterceptorValueHolder\MethodGenerator\InterceptedMethod; use ProxyManager\ProxyGenerator\AccessInterceptorValueHolder\MethodGenerator\MagicClone; @@ -133,6 +134,10 @@ function (ReflectionMethod $method) use ($prefixInterceptors, $suffixInterceptor new MagicClone($originalClass, $valueHolder, $prefixInterceptors, $suffixInterceptors), new MagicSleep($originalClass, $valueHolder), new MagicWakeup($originalClass, $valueHolder), + ), + AbstractMethod::createCollection( + $originalClass, + ClassGeneratorUtils::getAbstractMethods($originalClass) ) ) ); diff --git a/src/ProxyManager/ProxyGenerator/LazyLoadingGhostGenerator.php b/src/ProxyManager/ProxyGenerator/LazyLoadingGhostGenerator.php index db772a3b7..f9dc1934d 100644 --- a/src/ProxyManager/ProxyGenerator/LazyLoadingGhostGenerator.php +++ b/src/ProxyManager/ProxyGenerator/LazyLoadingGhostGenerator.php @@ -20,6 +20,7 @@ use ProxyManager\Exception\InvalidProxiedClassException; use ProxyManager\Generator\Util\ClassGeneratorUtils; +use ProxyManager\ProxyGenerator\AccessInterceptorScopeLocalizer\MethodGenerator\AbstractMethod; use ProxyManager\ProxyGenerator\LazyLoading\MethodGenerator\Constructor; use ProxyManager\ProxyGenerator\LazyLoadingGhost\MethodGenerator\CallInitializer; use ProxyManager\ProxyGenerator\LazyLoadingGhost\MethodGenerator\GetProxyInitializer; @@ -109,6 +110,10 @@ function (ReflectionMethod $method) use ($initializer, $init) { new GetProxyInitializer($initializer), new InitializeProxy($initializer, $init), new IsProxyInitialized($initializer), + ), + AbstractMethod::createCollection( + $originalClass, + ClassGeneratorUtils::getAbstractMethods($originalClass) ) ) ); diff --git a/src/ProxyManager/ProxyGenerator/LazyLoadingValueHolderGenerator.php b/src/ProxyManager/ProxyGenerator/LazyLoadingValueHolderGenerator.php index efb54f17b..55d8cec12 100644 --- a/src/ProxyManager/ProxyGenerator/LazyLoadingValueHolderGenerator.php +++ b/src/ProxyManager/ProxyGenerator/LazyLoadingValueHolderGenerator.php @@ -20,6 +20,7 @@ use ProxyManager\Exception\InvalidProxiedClassException; use ProxyManager\ProxyGenerator\AccessInterceptor\MethodGenerator\MagicWakeup; +use ProxyManager\ProxyGenerator\AccessInterceptorScopeLocalizer\MethodGenerator\AbstractMethod; use ProxyManager\ProxyGenerator\LazyLoading\MethodGenerator\Constructor; use ProxyManager\ProxyGenerator\LazyLoadingValueHolder\MethodGenerator\GetProxyInitializer; use ProxyManager\ProxyGenerator\LazyLoadingValueHolder\MethodGenerator\InitializeProxy; @@ -106,6 +107,10 @@ function (ReflectionMethod $method) use ($initializer, $valueHolder) { new InitializeProxy($initializer, $valueHolder), new IsProxyInitialized($valueHolder), new GetWrappedValueHolderValue($valueHolder), + ), + AbstractMethod::createCollection( + $originalClass, + ClassGeneratorUtils::getAbstractMethods($originalClass) ) ) ); diff --git a/src/ProxyManager/ProxyGenerator/RemoteObjectGenerator.php b/src/ProxyManager/ProxyGenerator/RemoteObjectGenerator.php index 4e390f3f7..e5b12e81a 100644 --- a/src/ProxyManager/ProxyGenerator/RemoteObjectGenerator.php +++ b/src/ProxyManager/ProxyGenerator/RemoteObjectGenerator.php @@ -20,6 +20,7 @@ use ProxyManager\Exception\InvalidProxiedClassException; use ProxyManager\Generator\Util\ClassGeneratorUtils; +use ProxyManager\ProxyGenerator\AccessInterceptorScopeLocalizer\MethodGenerator\AbstractMethod; use ProxyManager\ProxyGenerator\RemoteObject\MethodGenerator\Constructor; use ProxyManager\ProxyGenerator\RemoteObject\MethodGenerator\MagicGet; use ProxyManager\ProxyGenerator\RemoteObject\MethodGenerator\MagicIsset; @@ -88,6 +89,10 @@ function (ReflectionMethod $method) use ($adapter, $originalClass) { new MagicSet($originalClass, $adapter), new MagicIsset($originalClass, $adapter), new MagicUnset($originalClass, $adapter), + ), + AbstractMethod::createCollection( + $originalClass, + ClassGeneratorUtils::getAbstractMethods($originalClass) ) ) ); From bdfaba9c9f05c2caefd0c51f78e9d82b7de87c48 Mon Sep 17 00:00:00 2001 From: malukenho Date: Mon, 27 Oct 2014 16:15:09 -0300 Subject: [PATCH 16/49] Fix interfaces implements - Remove uncessary namespaces from AbstractMethod - Put `NullObjectGenerator` to be generated with abstract methods implementation --- .../MethodGenerator/AbstractMethod.php | 4 ---- .../ProxyGenerator/NullObjectGenerator.php | 16 ++++++++++++---- .../ProxyGenerator/NullObjectGeneratorTest.php | 4 ---- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/ProxyManager/ProxyGenerator/AccessInterceptorScopeLocalizer/MethodGenerator/AbstractMethod.php b/src/ProxyManager/ProxyGenerator/AccessInterceptorScopeLocalizer/MethodGenerator/AbstractMethod.php index 49ee3f55b..bc7ed05fa 100644 --- a/src/ProxyManager/ProxyGenerator/AccessInterceptorScopeLocalizer/MethodGenerator/AbstractMethod.php +++ b/src/ProxyManager/ProxyGenerator/AccessInterceptorScopeLocalizer/MethodGenerator/AbstractMethod.php @@ -18,12 +18,8 @@ namespace ProxyManager\ProxyGenerator\AccessInterceptorScopeLocalizer\MethodGenerator; -use ProxyManager\Generator\ParameterGenerator; use ReflectionClass; -use ReflectionMethod; use Zend\Code\Generator\MethodGenerator; -use Zend\Code\Generator\PropertyGenerator; -use Zend\Code\Reflection\MethodReflection; /** * Create methods to be compliance with abstracts methods on diff --git a/src/ProxyManager/ProxyGenerator/NullObjectGenerator.php b/src/ProxyManager/ProxyGenerator/NullObjectGenerator.php index f57d71239..6e88555ce 100644 --- a/src/ProxyManager/ProxyGenerator/NullObjectGenerator.php +++ b/src/ProxyManager/ProxyGenerator/NullObjectGenerator.php @@ -20,11 +20,13 @@ use ProxyManager\Exception\InvalidProxiedClassException; use ProxyManager\Generator\Util\ClassGeneratorUtils; +use ProxyManager\ProxyGenerator\AccessInterceptorScopeLocalizer\MethodGenerator\AbstractMethod; use ProxyManager\ProxyGenerator\NullObject\MethodGenerator\Constructor; use ProxyManager\ProxyGenerator\NullObject\MethodGenerator\NullObjectMethodInterceptor; use ProxyManager\ProxyGenerator\Util\ProxiedMethodsFilter; use ReflectionClass; use Zend\Code\Generator\ClassGenerator; +use Zend\Code\Generator\MethodGenerator; use Zend\Code\Reflection\MethodReflection; /** @@ -50,10 +52,6 @@ public function generate(ReflectionClass $originalClass, ClassGenerator $classGe if ($originalClass->isInterface()) { $interfaces[] = $originalClass->getName(); - } else { - foreach ($originalClass->getInterfaceNames() as $name) { - $interfaces[] = $name; - } } $classGenerator->setImplementedInterfaces($interfaces); @@ -66,6 +64,16 @@ public function generate(ReflectionClass $originalClass, ClassGenerator $classGe ); } + array_map( + function (MethodGenerator $generatedMethod) use ($originalClass, $classGenerator) { + ClassGeneratorUtils::addMethodIfNotFinal($originalClass, $classGenerator, $generatedMethod); + }, + AbstractMethod::createCollection( + $originalClass, + ClassGeneratorUtils::getAbstractMethods($originalClass) + ) + ); + ClassGeneratorUtils::addMethodIfNotFinal($originalClass, $classGenerator, new Constructor($originalClass)); } } diff --git a/tests/ProxyManagerTest/ProxyGenerator/NullObjectGeneratorTest.php b/tests/ProxyManagerTest/ProxyGenerator/NullObjectGeneratorTest.php index ee0df058b..11a6cbdd5 100644 --- a/tests/ProxyManagerTest/ProxyGenerator/NullObjectGeneratorTest.php +++ b/tests/ProxyManagerTest/ProxyGenerator/NullObjectGeneratorTest.php @@ -57,10 +57,6 @@ public function testGeneratesValidCode($className) if ($originalClass->isInterface()) { $this->assertTrue($generatedReflection->implementsInterface($className)); - } else { - $this->assertEmpty( - array_diff($originalClass->getInterfaceNames(), $generatedReflection->getInterfaceNames()) - ); } $this->assertSame($generatedClassName, $generatedReflection->getName()); From 71bba0e19200abe62a777b236ee727c47be120bd Mon Sep 17 00:00:00 2001 From: malukenho Date: Mon, 27 Oct 2014 17:17:33 -0300 Subject: [PATCH 17/49] Remove debug line of code --- .../MethodGenerator/AbstractMethod.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/ProxyManager/ProxyGenerator/AccessInterceptorScopeLocalizer/MethodGenerator/AbstractMethod.php b/src/ProxyManager/ProxyGenerator/AccessInterceptorScopeLocalizer/MethodGenerator/AbstractMethod.php index bc7ed05fa..9796fd1e2 100644 --- a/src/ProxyManager/ProxyGenerator/AccessInterceptorScopeLocalizer/MethodGenerator/AbstractMethod.php +++ b/src/ProxyManager/ProxyGenerator/AccessInterceptorScopeLocalizer/MethodGenerator/AbstractMethod.php @@ -48,9 +48,7 @@ public static function createCollection(ReflectionClass $originalClass, array $m foreach ($methods as $methodName) { $methodCollection[] = new self($originalClass, $methodName); } - if ('PHPUnit_Runner_BaseTestRunner' == $originalClass->getName()) { - //print_r($methodCollection); - } + return $methodCollection; } } From f9744d7ef682ce3a2b1dfad866fc98343c6086e0 Mon Sep 17 00:00:00 2001 From: malukenho Date: Sun, 2 Oct 2011 14:00:00 +0100 Subject: [PATCH 18/49] Remove call to `setBody()` with a empty string - Put some annotations on methods of ClassGeneratorUtils --- .../Generator/Util/ClassGeneratorUtils.php | 10 +++++++++- .../MethodGenerator/AbstractMethod.php | 18 +++++++++++++++--- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/src/ProxyManager/Generator/Util/ClassGeneratorUtils.php b/src/ProxyManager/Generator/Util/ClassGeneratorUtils.php index f612a82c3..f778af91c 100644 --- a/src/ProxyManager/Generator/Util/ClassGeneratorUtils.php +++ b/src/ProxyManager/Generator/Util/ClassGeneratorUtils.php @@ -53,10 +53,18 @@ public static function addMethodIfNotFinal( $classGenerator->addMethodFromGenerator($generatedMethod); } - public static function getAbstractMethods($originalClass) + /** + * Return the abstract methods name from a given `ReflectionClass` instance. + * + * @param $originalClass + * + * @return array + */ + public static function getAbstractMethods(ReflectionClass $originalClass) { $methodList = array(); $abstractMethods = $originalClass->getMethods(ReflectionMethod::IS_ABSTRACT); + foreach ($abstractMethods as $method) { $methodList[] = $method->getName(); } diff --git a/src/ProxyManager/ProxyGenerator/AccessInterceptorScopeLocalizer/MethodGenerator/AbstractMethod.php b/src/ProxyManager/ProxyGenerator/AccessInterceptorScopeLocalizer/MethodGenerator/AbstractMethod.php index 9796fd1e2..d21096091 100644 --- a/src/ProxyManager/ProxyGenerator/AccessInterceptorScopeLocalizer/MethodGenerator/AbstractMethod.php +++ b/src/ProxyManager/ProxyGenerator/AccessInterceptorScopeLocalizer/MethodGenerator/AbstractMethod.php @@ -30,6 +30,12 @@ */ class AbstractMethod extends MethodGenerator { + /** + * Constructor. + * + * @param ReflectionClass $originalClass + * @param string $name + */ public function __construct(ReflectionClass $originalClass, $name) { parent::__construct($name); @@ -38,17 +44,23 @@ public function __construct(ReflectionClass $originalClass, $name) foreach ($method->getParameters() as $param) { $this->setParameter($param->getName()); } - - $this->setBody(''); } + /** + * Return a collection of abstractMethods objects. + * + * @param ReflectionClass $originalClass + * @param array $methods + * + * @return AbstractMethod[] + */ public static function createCollection(ReflectionClass $originalClass, array $methods) { $methodCollection = array(); foreach ($methods as $methodName) { $methodCollection[] = new self($originalClass, $methodName); } - + return $methodCollection; } } From 388c3b3f0af1899c0d615515b7413bb664eb184e Mon Sep 17 00:00:00 2001 From: malukenho Date: Wed, 12 Oct 2011 14:00:00 +0100 Subject: [PATCH 19/49] Update namespaces, remove alias for ClassGenerator --- src/ProxyManager/Generator/Util/ClassGeneratorUtils.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/ProxyManager/Generator/Util/ClassGeneratorUtils.php b/src/ProxyManager/Generator/Util/ClassGeneratorUtils.php index f778af91c..395645cb5 100644 --- a/src/ProxyManager/Generator/Util/ClassGeneratorUtils.php +++ b/src/ProxyManager/Generator/Util/ClassGeneratorUtils.php @@ -20,8 +20,8 @@ use ReflectionClass; use ReflectionMethod; +use Zend\Code\Generator\ClassGenerator; use Zend\Code\Generator\MethodGenerator; -use Zend\Code\Generator\ClassGenerator as GeneratorClass; /** * Util class to help to generate code @@ -33,14 +33,14 @@ final class ClassGeneratorUtils { /** * @param ReflectionClass $originalClass - * @param GeneratorClass $classGenerator + * @param ClassGenerator $classGenerator * @param MethodGenerator $generatedMethod * * @return void|false */ public static function addMethodIfNotFinal( ReflectionClass $originalClass, - GeneratorClass $classGenerator, + ClassGenerator $classGenerator, MethodGenerator $generatedMethod ) { $methodName = $generatedMethod->getName(); From c7c3ca279d703b22312acfe27f0222ec32834cf6 Mon Sep 17 00:00:00 2001 From: malukenho Date: Thu, 6 Nov 2014 08:27:29 -0300 Subject: [PATCH 20/49] Modify name of "createCollection" to "buildConcreteMethodsFromOriginalClass" --- .../MethodGenerator/AbstractMethod.php | 2 +- .../ProxyGenerator/AccessInterceptorScopeLocalizerGenerator.php | 2 +- .../ProxyGenerator/AccessInterceptorValueHolderGenerator.php | 2 +- src/ProxyManager/ProxyGenerator/LazyLoadingGhostGenerator.php | 2 +- .../ProxyGenerator/LazyLoadingValueHolderGenerator.php | 2 +- src/ProxyManager/ProxyGenerator/NullObjectGenerator.php | 2 +- src/ProxyManager/ProxyGenerator/RemoteObjectGenerator.php | 2 +- 7 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/ProxyManager/ProxyGenerator/AccessInterceptorScopeLocalizer/MethodGenerator/AbstractMethod.php b/src/ProxyManager/ProxyGenerator/AccessInterceptorScopeLocalizer/MethodGenerator/AbstractMethod.php index d21096091..983c568e8 100644 --- a/src/ProxyManager/ProxyGenerator/AccessInterceptorScopeLocalizer/MethodGenerator/AbstractMethod.php +++ b/src/ProxyManager/ProxyGenerator/AccessInterceptorScopeLocalizer/MethodGenerator/AbstractMethod.php @@ -54,7 +54,7 @@ public function __construct(ReflectionClass $originalClass, $name) * * @return AbstractMethod[] */ - public static function createCollection(ReflectionClass $originalClass, array $methods) + public static function buildConcreteMethodsFromOriginalClass(ReflectionClass $originalClass, array $methods) { $methodCollection = array(); foreach ($methods as $methodName) { diff --git a/src/ProxyManager/ProxyGenerator/AccessInterceptorScopeLocalizerGenerator.php b/src/ProxyManager/ProxyGenerator/AccessInterceptorScopeLocalizerGenerator.php index 86db00634..b2916bd4a 100644 --- a/src/ProxyManager/ProxyGenerator/AccessInterceptorScopeLocalizerGenerator.php +++ b/src/ProxyManager/ProxyGenerator/AccessInterceptorScopeLocalizerGenerator.php @@ -97,7 +97,7 @@ function (ReflectionMethod $method) use ($prefixInterceptors, $suffixInterceptor new MagicSleep($originalClass, $prefixInterceptors, $suffixInterceptors), new MagicClone($originalClass, $prefixInterceptors, $suffixInterceptors), ), - AbstractMethod::createCollection( + AbstractMethod::buildConcreteMethodsFromOriginalClass( $originalClass, ClassGeneratorUtils::getAbstractMethods($originalClass) ) diff --git a/src/ProxyManager/ProxyGenerator/AccessInterceptorValueHolderGenerator.php b/src/ProxyManager/ProxyGenerator/AccessInterceptorValueHolderGenerator.php index 4e7e3fd50..836dedab4 100644 --- a/src/ProxyManager/ProxyGenerator/AccessInterceptorValueHolderGenerator.php +++ b/src/ProxyManager/ProxyGenerator/AccessInterceptorValueHolderGenerator.php @@ -135,7 +135,7 @@ function (ReflectionMethod $method) use ($prefixInterceptors, $suffixInterceptor new MagicSleep($originalClass, $valueHolder), new MagicWakeup($originalClass, $valueHolder), ), - AbstractMethod::createCollection( + AbstractMethod::buildConcreteMethodsFromOriginalClass( $originalClass, ClassGeneratorUtils::getAbstractMethods($originalClass) ) diff --git a/src/ProxyManager/ProxyGenerator/LazyLoadingGhostGenerator.php b/src/ProxyManager/ProxyGenerator/LazyLoadingGhostGenerator.php index f9dc1934d..812066f9b 100644 --- a/src/ProxyManager/ProxyGenerator/LazyLoadingGhostGenerator.php +++ b/src/ProxyManager/ProxyGenerator/LazyLoadingGhostGenerator.php @@ -111,7 +111,7 @@ function (ReflectionMethod $method) use ($initializer, $init) { new InitializeProxy($initializer, $init), new IsProxyInitialized($initializer), ), - AbstractMethod::createCollection( + AbstractMethod::buildConcreteMethodsFromOriginalClass( $originalClass, ClassGeneratorUtils::getAbstractMethods($originalClass) ) diff --git a/src/ProxyManager/ProxyGenerator/LazyLoadingValueHolderGenerator.php b/src/ProxyManager/ProxyGenerator/LazyLoadingValueHolderGenerator.php index 55d8cec12..e4faaffa1 100644 --- a/src/ProxyManager/ProxyGenerator/LazyLoadingValueHolderGenerator.php +++ b/src/ProxyManager/ProxyGenerator/LazyLoadingValueHolderGenerator.php @@ -108,7 +108,7 @@ function (ReflectionMethod $method) use ($initializer, $valueHolder) { new IsProxyInitialized($valueHolder), new GetWrappedValueHolderValue($valueHolder), ), - AbstractMethod::createCollection( + AbstractMethod::buildConcreteMethodsFromOriginalClass( $originalClass, ClassGeneratorUtils::getAbstractMethods($originalClass) ) diff --git a/src/ProxyManager/ProxyGenerator/NullObjectGenerator.php b/src/ProxyManager/ProxyGenerator/NullObjectGenerator.php index 6e88555ce..3d6e7b721 100644 --- a/src/ProxyManager/ProxyGenerator/NullObjectGenerator.php +++ b/src/ProxyManager/ProxyGenerator/NullObjectGenerator.php @@ -68,7 +68,7 @@ public function generate(ReflectionClass $originalClass, ClassGenerator $classGe function (MethodGenerator $generatedMethod) use ($originalClass, $classGenerator) { ClassGeneratorUtils::addMethodIfNotFinal($originalClass, $classGenerator, $generatedMethod); }, - AbstractMethod::createCollection( + AbstractMethod::buildConcreteMethodsFromOriginalClass( $originalClass, ClassGeneratorUtils::getAbstractMethods($originalClass) ) diff --git a/src/ProxyManager/ProxyGenerator/RemoteObjectGenerator.php b/src/ProxyManager/ProxyGenerator/RemoteObjectGenerator.php index e5b12e81a..4e85d10dc 100644 --- a/src/ProxyManager/ProxyGenerator/RemoteObjectGenerator.php +++ b/src/ProxyManager/ProxyGenerator/RemoteObjectGenerator.php @@ -90,7 +90,7 @@ function (ReflectionMethod $method) use ($adapter, $originalClass) { new MagicIsset($originalClass, $adapter), new MagicUnset($originalClass, $adapter), ), - AbstractMethod::createCollection( + AbstractMethod::buildConcreteMethodsFromOriginalClass( $originalClass, ClassGeneratorUtils::getAbstractMethods($originalClass) ) From 700dd8b5f6ae5a7a135ace39c9ad302ccfb0594f Mon Sep 17 00:00:00 2001 From: malukenho Date: Thu, 6 Nov 2014 08:32:27 -0300 Subject: [PATCH 21/49] Use array_map instead of foreach --- src/ProxyManager/Generator/Util/ClassGeneratorUtils.php | 4 ++-- .../MethodGenerator/AbstractMethod.php | 5 +++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/ProxyManager/Generator/Util/ClassGeneratorUtils.php b/src/ProxyManager/Generator/Util/ClassGeneratorUtils.php index 395645cb5..e00960e7b 100644 --- a/src/ProxyManager/Generator/Util/ClassGeneratorUtils.php +++ b/src/ProxyManager/Generator/Util/ClassGeneratorUtils.php @@ -65,9 +65,9 @@ public static function getAbstractMethods(ReflectionClass $originalClass) $methodList = array(); $abstractMethods = $originalClass->getMethods(ReflectionMethod::IS_ABSTRACT); - foreach ($abstractMethods as $method) { + array_map(function($method) use(& $methodList) { $methodList[] = $method->getName(); - } + }, $abstractMethods); return $methodList; } diff --git a/src/ProxyManager/ProxyGenerator/AccessInterceptorScopeLocalizer/MethodGenerator/AbstractMethod.php b/src/ProxyManager/ProxyGenerator/AccessInterceptorScopeLocalizer/MethodGenerator/AbstractMethod.php index 983c568e8..157768ffa 100644 --- a/src/ProxyManager/ProxyGenerator/AccessInterceptorScopeLocalizer/MethodGenerator/AbstractMethod.php +++ b/src/ProxyManager/ProxyGenerator/AccessInterceptorScopeLocalizer/MethodGenerator/AbstractMethod.php @@ -57,9 +57,10 @@ public function __construct(ReflectionClass $originalClass, $name) public static function buildConcreteMethodsFromOriginalClass(ReflectionClass $originalClass, array $methods) { $methodCollection = array(); - foreach ($methods as $methodName) { + + array_map(function($methodName) use (& $methodCollection, $originalClass) { $methodCollection[] = new self($originalClass, $methodName); - } + }, $methods); return $methodCollection; } From a80bae9dbd94f66587b61f949895a1be7d41992a Mon Sep 17 00:00:00 2001 From: malukenho Date: Thu, 6 Nov 2014 09:21:04 -0300 Subject: [PATCH 22/49] Simplify `ClassGeneratorUtils` removing the method `getAbstractMethods()` and using the self method of `ReflectionClass` to create a list of abstract methods and pass itself to the `AbstractMethod` to creates instances. --- .../Generator/Util/ClassGeneratorUtils.php | 19 ------------------- .../MethodGenerator/AbstractMethod.php | 18 ++++++++---------- 2 files changed, 8 insertions(+), 29 deletions(-) diff --git a/src/ProxyManager/Generator/Util/ClassGeneratorUtils.php b/src/ProxyManager/Generator/Util/ClassGeneratorUtils.php index e00960e7b..5fb81a68a 100644 --- a/src/ProxyManager/Generator/Util/ClassGeneratorUtils.php +++ b/src/ProxyManager/Generator/Util/ClassGeneratorUtils.php @@ -52,23 +52,4 @@ public static function addMethodIfNotFinal( $classGenerator->addMethodFromGenerator($generatedMethod); } - - /** - * Return the abstract methods name from a given `ReflectionClass` instance. - * - * @param $originalClass - * - * @return array - */ - public static function getAbstractMethods(ReflectionClass $originalClass) - { - $methodList = array(); - $abstractMethods = $originalClass->getMethods(ReflectionMethod::IS_ABSTRACT); - - array_map(function($method) use(& $methodList) { - $methodList[] = $method->getName(); - }, $abstractMethods); - - return $methodList; - } } diff --git a/src/ProxyManager/ProxyGenerator/AccessInterceptorScopeLocalizer/MethodGenerator/AbstractMethod.php b/src/ProxyManager/ProxyGenerator/AccessInterceptorScopeLocalizer/MethodGenerator/AbstractMethod.php index 157768ffa..4064137b2 100644 --- a/src/ProxyManager/ProxyGenerator/AccessInterceptorScopeLocalizer/MethodGenerator/AbstractMethod.php +++ b/src/ProxyManager/ProxyGenerator/AccessInterceptorScopeLocalizer/MethodGenerator/AbstractMethod.php @@ -19,6 +19,7 @@ namespace ProxyManager\ProxyGenerator\AccessInterceptorScopeLocalizer\MethodGenerator; use ReflectionClass; +use ReflectionMethod; use Zend\Code\Generator\MethodGenerator; /** @@ -33,13 +34,11 @@ class AbstractMethod extends MethodGenerator /** * Constructor. * - * @param ReflectionClass $originalClass - * @param string $name + * @param ReflectionMethod $method */ - public function __construct(ReflectionClass $originalClass, $name) + public function __construct(ReflectionMethod $method) { - parent::__construct($name); - $method = $originalClass->getMethod($name); + parent::__construct($method->getName()); foreach ($method->getParameters() as $param) { $this->setParameter($param->getName()); @@ -50,17 +49,16 @@ public function __construct(ReflectionClass $originalClass, $name) * Return a collection of abstractMethods objects. * * @param ReflectionClass $originalClass - * @param array $methods * * @return AbstractMethod[] */ - public static function buildConcreteMethodsFromOriginalClass(ReflectionClass $originalClass, array $methods) + public static function buildConcreteMethodsFromOriginalClass(ReflectionClass $originalClass) { $methodCollection = array(); - array_map(function($methodName) use (& $methodCollection, $originalClass) { - $methodCollection[] = new self($originalClass, $methodName); - }, $methods); + array_map(function(ReflectionMethod $methodName) use (& $methodCollection, $originalClass) { + $methodCollection[] = new self($methodName); + }, $originalClass->getMethods(ReflectionMethod::IS_ABSTRACT)); return $methodCollection; } From 9f65e13393f849db52fe62da7cb9ce35d8abb0f9 Mon Sep 17 00:00:00 2001 From: malukenho Date: Thu, 6 Nov 2014 09:21:54 -0300 Subject: [PATCH 23/49] Refactor call of method and arguments to create AbstractMethod Collections --- .../AccessInterceptorScopeLocalizerGenerator.php | 5 +---- .../ProxyGenerator/AccessInterceptorValueHolderGenerator.php | 5 +---- .../ProxyGenerator/LazyLoadingGhostGenerator.php | 5 +---- .../ProxyGenerator/LazyLoadingValueHolderGenerator.php | 5 +---- src/ProxyManager/ProxyGenerator/NullObjectGenerator.php | 5 +---- src/ProxyManager/ProxyGenerator/RemoteObjectGenerator.php | 5 +---- 6 files changed, 6 insertions(+), 24 deletions(-) diff --git a/src/ProxyManager/ProxyGenerator/AccessInterceptorScopeLocalizerGenerator.php b/src/ProxyManager/ProxyGenerator/AccessInterceptorScopeLocalizerGenerator.php index b2916bd4a..c9ec1af90 100644 --- a/src/ProxyManager/ProxyGenerator/AccessInterceptorScopeLocalizerGenerator.php +++ b/src/ProxyManager/ProxyGenerator/AccessInterceptorScopeLocalizerGenerator.php @@ -97,10 +97,7 @@ function (ReflectionMethod $method) use ($prefixInterceptors, $suffixInterceptor new MagicSleep($originalClass, $prefixInterceptors, $suffixInterceptors), new MagicClone($originalClass, $prefixInterceptors, $suffixInterceptors), ), - AbstractMethod::buildConcreteMethodsFromOriginalClass( - $originalClass, - ClassGeneratorUtils::getAbstractMethods($originalClass) - ) + AbstractMethod::buildConcreteMethodsFromOriginalClass($originalClass) ) ); } diff --git a/src/ProxyManager/ProxyGenerator/AccessInterceptorValueHolderGenerator.php b/src/ProxyManager/ProxyGenerator/AccessInterceptorValueHolderGenerator.php index 836dedab4..ed927c50e 100644 --- a/src/ProxyManager/ProxyGenerator/AccessInterceptorValueHolderGenerator.php +++ b/src/ProxyManager/ProxyGenerator/AccessInterceptorValueHolderGenerator.php @@ -135,10 +135,7 @@ function (ReflectionMethod $method) use ($prefixInterceptors, $suffixInterceptor new MagicSleep($originalClass, $valueHolder), new MagicWakeup($originalClass, $valueHolder), ), - AbstractMethod::buildConcreteMethodsFromOriginalClass( - $originalClass, - ClassGeneratorUtils::getAbstractMethods($originalClass) - ) + AbstractMethod::buildConcreteMethodsFromOriginalClass($originalClass) ) ); } diff --git a/src/ProxyManager/ProxyGenerator/LazyLoadingGhostGenerator.php b/src/ProxyManager/ProxyGenerator/LazyLoadingGhostGenerator.php index 812066f9b..296629f40 100644 --- a/src/ProxyManager/ProxyGenerator/LazyLoadingGhostGenerator.php +++ b/src/ProxyManager/ProxyGenerator/LazyLoadingGhostGenerator.php @@ -111,10 +111,7 @@ function (ReflectionMethod $method) use ($initializer, $init) { new InitializeProxy($initializer, $init), new IsProxyInitialized($initializer), ), - AbstractMethod::buildConcreteMethodsFromOriginalClass( - $originalClass, - ClassGeneratorUtils::getAbstractMethods($originalClass) - ) + AbstractMethod::buildConcreteMethodsFromOriginalClass($originalClass) ) ); } diff --git a/src/ProxyManager/ProxyGenerator/LazyLoadingValueHolderGenerator.php b/src/ProxyManager/ProxyGenerator/LazyLoadingValueHolderGenerator.php index e4faaffa1..f5b46b6fc 100644 --- a/src/ProxyManager/ProxyGenerator/LazyLoadingValueHolderGenerator.php +++ b/src/ProxyManager/ProxyGenerator/LazyLoadingValueHolderGenerator.php @@ -108,10 +108,7 @@ function (ReflectionMethod $method) use ($initializer, $valueHolder) { new IsProxyInitialized($valueHolder), new GetWrappedValueHolderValue($valueHolder), ), - AbstractMethod::buildConcreteMethodsFromOriginalClass( - $originalClass, - ClassGeneratorUtils::getAbstractMethods($originalClass) - ) + AbstractMethod::buildConcreteMethodsFromOriginalClass($originalClass) ) ); } diff --git a/src/ProxyManager/ProxyGenerator/NullObjectGenerator.php b/src/ProxyManager/ProxyGenerator/NullObjectGenerator.php index 3d6e7b721..58c4d73f8 100644 --- a/src/ProxyManager/ProxyGenerator/NullObjectGenerator.php +++ b/src/ProxyManager/ProxyGenerator/NullObjectGenerator.php @@ -68,10 +68,7 @@ public function generate(ReflectionClass $originalClass, ClassGenerator $classGe function (MethodGenerator $generatedMethod) use ($originalClass, $classGenerator) { ClassGeneratorUtils::addMethodIfNotFinal($originalClass, $classGenerator, $generatedMethod); }, - AbstractMethod::buildConcreteMethodsFromOriginalClass( - $originalClass, - ClassGeneratorUtils::getAbstractMethods($originalClass) - ) + AbstractMethod::buildConcreteMethodsFromOriginalClass($originalClass) ); ClassGeneratorUtils::addMethodIfNotFinal($originalClass, $classGenerator, new Constructor($originalClass)); diff --git a/src/ProxyManager/ProxyGenerator/RemoteObjectGenerator.php b/src/ProxyManager/ProxyGenerator/RemoteObjectGenerator.php index 4e85d10dc..9bf31c41b 100644 --- a/src/ProxyManager/ProxyGenerator/RemoteObjectGenerator.php +++ b/src/ProxyManager/ProxyGenerator/RemoteObjectGenerator.php @@ -90,10 +90,7 @@ function (ReflectionMethod $method) use ($adapter, $originalClass) { new MagicIsset($originalClass, $adapter), new MagicUnset($originalClass, $adapter), ), - AbstractMethod::buildConcreteMethodsFromOriginalClass( - $originalClass, - ClassGeneratorUtils::getAbstractMethods($originalClass) - ) + AbstractMethod::buildConcreteMethodsFromOriginalClass($originalClass) ) ); } From e429068781f06862ef8f306a37c1a30644aab784 Mon Sep 17 00:00:00 2001 From: malukenho Date: Thu, 6 Nov 2014 09:43:36 -0300 Subject: [PATCH 24/49] Simplify `setImplementedInterfaces` --- src/ProxyManager/Generator/ClassGenerator.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/ProxyManager/Generator/ClassGenerator.php b/src/ProxyManager/Generator/ClassGenerator.php index 51aa51aa1..0304c42f2 100644 --- a/src/ProxyManager/Generator/ClassGenerator.php +++ b/src/ProxyManager/Generator/ClassGenerator.php @@ -45,8 +45,6 @@ public function setExtendedClass($extendedClass) */ public function setImplementedInterfaces(array $interfaces) { - $interfaces = array_diff(array_unique($interfaces), $this->getImplementedInterfaces()); - foreach ($interfaces as & $interface) { $interface = '\\' . trim($interface, '\\'); } From 86a69aae23bb0ed91d4d09fbbe85010d0f3a30f3 Mon Sep 17 00:00:00 2001 From: malukenho Date: Thu, 6 Nov 2014 09:45:31 -0300 Subject: [PATCH 25/49] Fix scope to create a class inside a Closure --- .../MethodGenerator/AbstractMethod.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/ProxyManager/ProxyGenerator/AccessInterceptorScopeLocalizer/MethodGenerator/AbstractMethod.php b/src/ProxyManager/ProxyGenerator/AccessInterceptorScopeLocalizer/MethodGenerator/AbstractMethod.php index 4064137b2..da3b946e9 100644 --- a/src/ProxyManager/ProxyGenerator/AccessInterceptorScopeLocalizer/MethodGenerator/AbstractMethod.php +++ b/src/ProxyManager/ProxyGenerator/AccessInterceptorScopeLocalizer/MethodGenerator/AbstractMethod.php @@ -54,10 +54,11 @@ public function __construct(ReflectionMethod $method) */ public static function buildConcreteMethodsFromOriginalClass(ReflectionClass $originalClass) { + $class = __CLASS__; $methodCollection = array(); - array_map(function(ReflectionMethod $methodName) use (& $methodCollection, $originalClass) { - $methodCollection[] = new self($methodName); + array_map(function(ReflectionMethod $methodName) use (& $methodCollection, $originalClass, $class) { + $methodCollection[] = new $class($methodName); }, $originalClass->getMethods(ReflectionMethod::IS_ABSTRACT)); return $methodCollection; From d04340e5be621ae07f514bc23f83b5c1a37631e8 Mon Sep 17 00:00:00 2001 From: malukenho Date: Thu, 6 Nov 2014 10:18:57 -0300 Subject: [PATCH 26/49] Hidden the E_STRICT message --- .../Functional/FatalPreventionFunctionalTest.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/ProxyManagerTest/Functional/FatalPreventionFunctionalTest.php b/tests/ProxyManagerTest/Functional/FatalPreventionFunctionalTest.php index 276fd3ccd..b690bede9 100644 --- a/tests/ProxyManagerTest/Functional/FatalPreventionFunctionalTest.php +++ b/tests/ProxyManagerTest/Functional/FatalPreventionFunctionalTest.php @@ -18,6 +18,8 @@ namespace ProxyManagerTest\Functional; +error_reporting(E_ALL & ~E_STRICT); + use PHPUnit_Framework_TestCase; use PHPUnit_Util_PHP; use ReflectionClass; From 341256a1d27fbc3fd118f7a47e20dfe9cf5ec1d7 Mon Sep 17 00:00:00 2001 From: malukenho Date: Thu, 6 Nov 2014 10:27:43 -0300 Subject: [PATCH 27/49] Move errors settings to the constructor --- .../Functional/FatalPreventionFunctionalTest.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/tests/ProxyManagerTest/Functional/FatalPreventionFunctionalTest.php b/tests/ProxyManagerTest/Functional/FatalPreventionFunctionalTest.php index b690bede9..8844db365 100644 --- a/tests/ProxyManagerTest/Functional/FatalPreventionFunctionalTest.php +++ b/tests/ProxyManagerTest/Functional/FatalPreventionFunctionalTest.php @@ -18,8 +18,6 @@ namespace ProxyManagerTest\Functional; -error_reporting(E_ALL & ~E_STRICT); - use PHPUnit_Framework_TestCase; use PHPUnit_Util_PHP; use ReflectionClass; @@ -59,6 +57,11 @@ class FatalPreventionFunctionalTest extends PHPUnit_Framework_TestCase echo 'SUCCESS: ' . %s; PHP; + public function setUp() + { + error_reporting(E_ALL & ~E_STRICT); + } + /** * Verifies that code generation and evaluation will not cause fatals with any given class * From 293f4b66f17237f97284cb447a688fc1d90e0ed0 Mon Sep 17 00:00:00 2001 From: malukenho Date: Thu, 6 Nov 2014 10:27:43 -0300 Subject: [PATCH 28/49] Realocate ERRORS SETTINGS --- .../Functional/FatalPreventionFunctionalTest.php | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/tests/ProxyManagerTest/Functional/FatalPreventionFunctionalTest.php b/tests/ProxyManagerTest/Functional/FatalPreventionFunctionalTest.php index 8844db365..3e1300532 100644 --- a/tests/ProxyManagerTest/Functional/FatalPreventionFunctionalTest.php +++ b/tests/ProxyManagerTest/Functional/FatalPreventionFunctionalTest.php @@ -57,11 +57,6 @@ class FatalPreventionFunctionalTest extends PHPUnit_Framework_TestCase echo 'SUCCESS: ' . %s; PHP; - public function setUp() - { - error_reporting(E_ALL & ~E_STRICT); - } - /** * Verifies that code generation and evaluation will not cause fatals with any given class * @@ -73,6 +68,7 @@ public function setUp() */ public function testCodeGeneration($generatorClass, $className) { + error_reporting(E_ALL & ~E_STRICT); $runner = PHPUnit_Util_PHP::factory(); $code = sprintf( From 3f8c80e2eb928db8739a64252e729a9de6c562fc Mon Sep 17 00:00:00 2001 From: malukenho Date: Thu, 6 Nov 2014 11:34:25 -0300 Subject: [PATCH 29/49] Fix CS --- .../MethodGenerator/AbstractMethod.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ProxyManager/ProxyGenerator/AccessInterceptorScopeLocalizer/MethodGenerator/AbstractMethod.php b/src/ProxyManager/ProxyGenerator/AccessInterceptorScopeLocalizer/MethodGenerator/AbstractMethod.php index da3b946e9..43680c4da 100644 --- a/src/ProxyManager/ProxyGenerator/AccessInterceptorScopeLocalizer/MethodGenerator/AbstractMethod.php +++ b/src/ProxyManager/ProxyGenerator/AccessInterceptorScopeLocalizer/MethodGenerator/AbstractMethod.php @@ -57,7 +57,7 @@ public static function buildConcreteMethodsFromOriginalClass(ReflectionClass $or $class = __CLASS__; $methodCollection = array(); - array_map(function(ReflectionMethod $methodName) use (& $methodCollection, $originalClass, $class) { + array_map(function (ReflectionMethod $methodName) use (& $methodCollection, $originalClass, $class) { $methodCollection[] = new $class($methodName); }, $originalClass->getMethods(ReflectionMethod::IS_ABSTRACT)); From 63e289165f321daf94af1e57137d2be69b976069 Mon Sep 17 00:00:00 2001 From: malukenho Date: Fri, 7 Nov 2014 06:53:35 -0300 Subject: [PATCH 30/49] Remove error_reporting --- .../Functional/FatalPreventionFunctionalTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/ProxyManagerTest/Functional/FatalPreventionFunctionalTest.php b/tests/ProxyManagerTest/Functional/FatalPreventionFunctionalTest.php index 3e1300532..276fd3ccd 100644 --- a/tests/ProxyManagerTest/Functional/FatalPreventionFunctionalTest.php +++ b/tests/ProxyManagerTest/Functional/FatalPreventionFunctionalTest.php @@ -68,7 +68,6 @@ class FatalPreventionFunctionalTest extends PHPUnit_Framework_TestCase */ public function testCodeGeneration($generatorClass, $className) { - error_reporting(E_ALL & ~E_STRICT); $runner = PHPUnit_Util_PHP::factory(); $code = sprintf( From 4cc42639a3f8bbc3ab6d5cb72acfdd2c362c5fec Mon Sep 17 00:00:00 2001 From: malukenho Date: Fri, 7 Nov 2014 06:57:54 -0300 Subject: [PATCH 31/49] Fix docblock --- .../MethodGenerator/AbstractMethod.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ProxyManager/ProxyGenerator/AccessInterceptorScopeLocalizer/MethodGenerator/AbstractMethod.php b/src/ProxyManager/ProxyGenerator/AccessInterceptorScopeLocalizer/MethodGenerator/AbstractMethod.php index 43680c4da..5da99f00b 100644 --- a/src/ProxyManager/ProxyGenerator/AccessInterceptorScopeLocalizer/MethodGenerator/AbstractMethod.php +++ b/src/ProxyManager/ProxyGenerator/AccessInterceptorScopeLocalizer/MethodGenerator/AbstractMethod.php @@ -50,7 +50,7 @@ public function __construct(ReflectionMethod $method) * * @param ReflectionClass $originalClass * - * @return AbstractMethod[] + * @return self[] */ public static function buildConcreteMethodsFromOriginalClass(ReflectionClass $originalClass) { From 62f7df9acf22aa492f2989f9a52441b41f51b940 Mon Sep 17 00:00:00 2001 From: malukenho Date: Fri, 7 Nov 2014 07:14:48 -0300 Subject: [PATCH 32/49] Instantiate class by name directly and Fix Cd --- .../MethodGenerator/AbstractMethod.php | 8 +++++--- .../Functional/FatalPreventionFunctionalTest.php | 4 ++++ 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/ProxyManager/ProxyGenerator/AccessInterceptorScopeLocalizer/MethodGenerator/AbstractMethod.php b/src/ProxyManager/ProxyGenerator/AccessInterceptorScopeLocalizer/MethodGenerator/AbstractMethod.php index 5da99f00b..f43333647 100644 --- a/src/ProxyManager/ProxyGenerator/AccessInterceptorScopeLocalizer/MethodGenerator/AbstractMethod.php +++ b/src/ProxyManager/ProxyGenerator/AccessInterceptorScopeLocalizer/MethodGenerator/AbstractMethod.php @@ -54,11 +54,13 @@ public function __construct(ReflectionMethod $method) */ public static function buildConcreteMethodsFromOriginalClass(ReflectionClass $originalClass) { - $class = __CLASS__; $methodCollection = array(); - array_map(function (ReflectionMethod $methodName) use (& $methodCollection, $originalClass, $class) { - $methodCollection[] = new $class($methodName); + array_map(function (ReflectionMethod $methodName) use ( + & $methodCollection, + $originalClass + ) { + $methodCollection[] = new AbstractMethod($methodName); }, $originalClass->getMethods(ReflectionMethod::IS_ABSTRACT)); return $methodCollection; diff --git a/tests/ProxyManagerTest/Functional/FatalPreventionFunctionalTest.php b/tests/ProxyManagerTest/Functional/FatalPreventionFunctionalTest.php index 276fd3ccd..c929c0b8f 100644 --- a/tests/ProxyManagerTest/Functional/FatalPreventionFunctionalTest.php +++ b/tests/ProxyManagerTest/Functional/FatalPreventionFunctionalTest.php @@ -131,6 +131,10 @@ function ($class) use ($generator) { */ public function getProxyTestedClasses() { + if (PHP_VERSION_ID < 50401) { + $this->markTestSkipped('Can\'t '); + } + $skippedPaths = array( realpath(__DIR__ . '/../../src'), realpath(__DIR__ . '/../../vendor'), From 1154d4208cf7d0011d9954aff0c985c13533a43b Mon Sep 17 00:00:00 2001 From: malukenho Date: Fri, 7 Nov 2014 07:16:03 -0300 Subject: [PATCH 33/49] Skip test prevention --- .../Functional/FatalPreventionFunctionalTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/ProxyManagerTest/Functional/FatalPreventionFunctionalTest.php b/tests/ProxyManagerTest/Functional/FatalPreventionFunctionalTest.php index c929c0b8f..e1c8dbbc7 100644 --- a/tests/ProxyManagerTest/Functional/FatalPreventionFunctionalTest.php +++ b/tests/ProxyManagerTest/Functional/FatalPreventionFunctionalTest.php @@ -132,7 +132,7 @@ function ($class) use ($generator) { public function getProxyTestedClasses() { if (PHP_VERSION_ID < 50401) { - $this->markTestSkipped('Can\'t '); + $this->markTestSkipped('Can\'t run this Test Suit on php 5.3 above'); } $skippedPaths = array( From 27f290e22e6a63a4ace8e66fd4bb9d35b48159c5 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Sat, 8 Nov 2014 07:54:51 +0100 Subject: [PATCH 34/49] #193 - reverting changes to `ClassGeneratorUtils`, as duplicate methods should raise an exception in any case --- src/ProxyManager/Generator/Util/ClassGeneratorUtils.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/ProxyManager/Generator/Util/ClassGeneratorUtils.php b/src/ProxyManager/Generator/Util/ClassGeneratorUtils.php index 5fb81a68a..790683d84 100644 --- a/src/ProxyManager/Generator/Util/ClassGeneratorUtils.php +++ b/src/ProxyManager/Generator/Util/ClassGeneratorUtils.php @@ -45,8 +45,7 @@ public static function addMethodIfNotFinal( ) { $methodName = $generatedMethod->getName(); - if ($classGenerator->hasMethod($methodName) - || ($originalClass->hasMethod($methodName) && $originalClass->getMethod($methodName)->isFinal())) { + if ($originalClass->hasMethod($methodName) && $originalClass->getMethod($methodName)->isFinal()) { return false; } From bb83f3df9ae3cfa43b16165081e3c61860c57793 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Sat, 8 Nov 2014 08:58:55 +0100 Subject: [PATCH 35/49] #193 - specification for a `CanProxyAssertion` implementation --- .../Assertion/CanProxyAssertionTest.php | 103 ++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 tests/ProxyManagerTest/ProxyGenerator/Assertion/CanProxyAssertionTest.php diff --git a/tests/ProxyManagerTest/ProxyGenerator/Assertion/CanProxyAssertionTest.php b/tests/ProxyManagerTest/ProxyGenerator/Assertion/CanProxyAssertionTest.php new file mode 100644 index 000000000..96819a637 --- /dev/null +++ b/tests/ProxyManagerTest/ProxyGenerator/Assertion/CanProxyAssertionTest.php @@ -0,0 +1,103 @@ + + * @license MIT + * + * @covers \ProxyManager\ProxyGenerator\Assertion\CanProxyAssertion + * @group Coverage + */ +class CanProxyAssertionTest extends PHPUnit_Framework_TestCase +{ + public function testDeniesFinalClasses() + { + $this->setExpectedException('ProxyManager\Exception\InvalidProxiedClassException'); + + CanProxyAssertion::assertClassCanBeProxied(new ReflectionClass('ProxyManagerTestAsset\\FinalClass')); + } + + public function testDeniesClassesWithAbstractProtectedMethods() + { + $this->setExpectedException('ProxyManager\Exception\InvalidProxiedClassException'); + + CanProxyAssertion::assertClassCanBeProxied(new ReflectionClass( + 'ProxyManagerTestAsset\\ClassWithAbstractProtectedMethod' + )); + } + + public function testAllowsInterfaceByDefault() + { + CanProxyAssertion::assertClassCanBeProxied(new ReflectionClass( + 'ProxyManagerTestAsset\\BaseInterface' + )); + + $this->assertTrue(true); // not nice, but assertions are just fail-checks, no real code executed + } + + public function testDeniesInterfaceIfSpecified() + { + $this->setExpectedException('ProxyManager\Exception\InvalidProxiedClassException'); + + CanProxyAssertion::assertClassCanBeProxied(new ReflectionClass('ProxyManagerTestAsset\\BaseInterface'), false); + } + + /** + * @param string $className + * + * @dataProvider validClasses + */ + public function testAllowedClass($className) + { + CanProxyAssertion::assertClassCanBeProxied(new ReflectionClass($className)); + + $this->assertTrue(true); // not nice, but assertions are just fail-checks, no real code executed + } + + public function validClasses() + { + return array( + array('ProxyManagerTestAsset\AccessInterceptorValueHolderMock'), + array('ProxyManagerTestAsset\BaseClass'), + array('ProxyManagerTestAsset\BaseInterface'), + array('ProxyManagerTestAsset\CallableTypeHintClass'), + array('ProxyManagerTestAsset\ClassWithByRefMagicMethods'), + array('ProxyManagerTestAsset\ClassWithFinalMagicMethods'), + array('ProxyManagerTestAsset\ClassWithFinalMethods'), + array('ProxyManagerTestAsset\ClassWithMethodWithDefaultParameters'), + array('ProxyManagerTestAsset\ClassWithMixedProperties'), + array('ProxyManagerTestAsset\ClassWithPrivateProperties'), + array('ProxyManagerTestAsset\ClassWithProtectedProperties'), + array('ProxyManagerTestAsset\ClassWithPublicProperties'), + array('ProxyManagerTestAsset\ClassWithPublicArrayProperty'), + array('ProxyManagerTestAsset\ClassWithSelfHint'), + array('ProxyManagerTestAsset\EmptyClass'), + array('ProxyManagerTestAsset\HydratedObject'), + array('ProxyManagerTestAsset\LazyLoadingMock'), + array('ProxyManagerTestAsset\NullObjectMock'), + ); + } +} From 4b48b1286b08ba52258be16fd47942876ad2899a Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Sat, 8 Nov 2014 08:59:23 +0100 Subject: [PATCH 36/49] #193 - test assets required for the `CanProxyAssertion` tests --- .../ClassWithAbstractProtectedMethod.php | 33 +++++++++++++++++++ tests/ProxyManagerTestAsset/FinalClass.php | 29 ++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 tests/ProxyManagerTestAsset/ClassWithAbstractProtectedMethod.php create mode 100644 tests/ProxyManagerTestAsset/FinalClass.php diff --git a/tests/ProxyManagerTestAsset/ClassWithAbstractProtectedMethod.php b/tests/ProxyManagerTestAsset/ClassWithAbstractProtectedMethod.php new file mode 100644 index 000000000..1ba3a5cb9 --- /dev/null +++ b/tests/ProxyManagerTestAsset/ClassWithAbstractProtectedMethod.php @@ -0,0 +1,33 @@ + + * @license MIT + */ +abstract class ClassWithAbstractProtectedMethod +{ + /** + * @return void + */ + abstract protected function protectedAbstractMethod(); +} diff --git a/tests/ProxyManagerTestAsset/FinalClass.php b/tests/ProxyManagerTestAsset/FinalClass.php new file mode 100644 index 000000000..4b818d673 --- /dev/null +++ b/tests/ProxyManagerTestAsset/FinalClass.php @@ -0,0 +1,29 @@ + + * @license MIT + */ +final class FinalClass +{ +} From eac062994aca78c6ab811fe954a81ca181aabff2 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Sat, 8 Nov 2014 09:01:57 +0100 Subject: [PATCH 37/49] #193 - test coverage for `ProxyManager\Exception\InvalidProxiedClassException::finalClassNotSupported()` --- .../Exception/InvalidProxiedClassExceptionTest.php | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/tests/ProxyManagerTest/Exception/InvalidProxiedClassExceptionTest.php b/tests/ProxyManagerTest/Exception/InvalidProxiedClassExceptionTest.php index d0f5b24a0..5f9c7d07d 100644 --- a/tests/ProxyManagerTest/Exception/InvalidProxiedClassExceptionTest.php +++ b/tests/ProxyManagerTest/Exception/InvalidProxiedClassExceptionTest.php @@ -33,9 +33,6 @@ */ class InvalidProxiedClassExceptionTest extends PHPUnit_Framework_TestCase { - /** - * @covers \ProxyManager\Exception\InvalidProxiedClassException::interfaceNotSupported - */ public function testInterfaceNotSupported() { $this->assertSame( @@ -45,4 +42,14 @@ public function testInterfaceNotSupported() )->getMessage() ); } + + public function testFinalClassNotSupported() + { + $this->assertSame( + 'Provided class "ProxyManagerTestAsset\FinalClass" is final and cannot be proxied', + InvalidProxiedClassException::finalClassNotSupported( + new ReflectionClass('ProxyManagerTestAsset\FinalClass') + )->getMessage() + ); + } } From 096dcd94b1020002a0224f2777690172463bf055 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Sat, 8 Nov 2014 09:03:55 +0100 Subject: [PATCH 38/49] #193 - specification for for `ProxyManager\Exception\InvalidProxiedClassException::abstractProtectedMethodsNotSupported()` --- .../Exception/InvalidProxiedClassExceptionTest.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tests/ProxyManagerTest/Exception/InvalidProxiedClassExceptionTest.php b/tests/ProxyManagerTest/Exception/InvalidProxiedClassExceptionTest.php index 5f9c7d07d..0656dad74 100644 --- a/tests/ProxyManagerTest/Exception/InvalidProxiedClassExceptionTest.php +++ b/tests/ProxyManagerTest/Exception/InvalidProxiedClassExceptionTest.php @@ -52,4 +52,16 @@ public function testFinalClassNotSupported() )->getMessage() ); } + + public function testAbstractProtectedMethodsNotSupported() + { + $this->assertSame( + 'Provided class "ProxyManagerTestAsset\ClassWithAbstractProtectedMethod" has following protected abstract' + . ' methods, and therefore cannot be proxied:' . "\n" + . 'ProxyManagerTestAsset\ClassWithAbstractProtectedMethod::protectedAbstractMethod', + InvalidProxiedClassException::abstractProtectedMethodsNotSupported( + new ReflectionClass('ProxyManagerTestAsset\ClassWithAbstractProtectedMethod') + )->getMessage() + ); + } } From ecab662e97d02f751a55298829ea63728a798a6d Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Sat, 8 Nov 2014 09:04:52 +0100 Subject: [PATCH 39/49] #193 - implementing `ProxyManager\Exception\InvalidProxiedClassException::abstractProtectedMethodsNotSupported()` --- .../InvalidProxiedClassException.php | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/ProxyManager/Exception/InvalidProxiedClassException.php b/src/ProxyManager/Exception/InvalidProxiedClassException.php index 011579112..5dc08d14d 100644 --- a/src/ProxyManager/Exception/InvalidProxiedClassException.php +++ b/src/ProxyManager/Exception/InvalidProxiedClassException.php @@ -20,6 +20,7 @@ use InvalidArgumentException; use ReflectionClass; +use ReflectionMethod; /** * Exception for invalid proxied classes @@ -48,4 +49,31 @@ public static function finalClassNotSupported(ReflectionClass $reflection) { return new self(sprintf('Provided class "%s" is final and cannot be proxied', $reflection->getName())); } + + /** + * @param ReflectionClass $reflection + * + * @return self + */ + public static function abstractProtectedMethodsNotSupported(ReflectionClass $reflection) + { + return new self(sprintf( + 'Provided class "%s" has following protected abstract methods, and therefore cannot be proxied:' . "\n%s", + $reflection->getName(), + implode( + "\n", + array_map( + function (ReflectionMethod $reflectionMethod) { + return $reflectionMethod->getDeclaringClass()->getName() . '::' . $reflectionMethod->getName(); + }, + array_filter( + $reflection->getMethods(), + function (ReflectionMethod $method) { + return $method->isAbstract() && $method->isProtected(); + } + ) + ) + ) + )); + } } From db34bbdd06c07844229467f2ce2fb1e1101f156a Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Sat, 8 Nov 2014 09:05:15 +0100 Subject: [PATCH 40/49] #193 - implementing `ProxyManager\Exception\CanProxyAssertion` logic --- .../Assertion/CanProxyAssertion.php | 96 +++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 src/ProxyManager/ProxyGenerator/Assertion/CanProxyAssertion.php diff --git a/src/ProxyManager/ProxyGenerator/Assertion/CanProxyAssertion.php b/src/ProxyManager/ProxyGenerator/Assertion/CanProxyAssertion.php new file mode 100644 index 000000000..fb3abd696 --- /dev/null +++ b/src/ProxyManager/ProxyGenerator/Assertion/CanProxyAssertion.php @@ -0,0 +1,96 @@ + + * @license MIT + */ +final class CanProxyAssertion +{ + /** + * Private constructor: not meant to be instantiated + */ + private function __construct() + { + } + + /** + * @param ReflectionClass $originalClass + * @param bool $allowInterfaces + * + * @throws InvalidProxiedClassException + */ + public static function assertClassCanBeProxied(ReflectionClass $originalClass, $allowInterfaces = true) + { + self::isNotFinal($originalClass); + self::hasNoAbstractProtectedMethods($originalClass); + + if (! $allowInterfaces) { + self::isNotInterface($originalClass); + } + } + + /** + * @param ReflectionClass $originalClass + * + * @throws InvalidProxiedClassException + */ + private static function isNotFinal(ReflectionClass $originalClass) + { + if ($originalClass->isFinal()) { + throw InvalidProxiedClassException::finalClassNotSupported($originalClass); + } + } + + /** + * @param ReflectionClass $originalClass + * + * @throws InvalidProxiedClassException + */ + private static function hasNoAbstractProtectedMethods(ReflectionClass $originalClass) + { + if (array_filter( + $originalClass->getMethods(), + function (ReflectionMethod $method) { + return $method->isAbstract() && $method->isProtected(); + } + )) { + throw InvalidProxiedClassException::abstractProtectedMethodsNotSupported($originalClass); + } + } + + /** + * @param ReflectionClass $originalClass + * + * @throws InvalidProxiedClassException + */ + private static function isNotInterface(ReflectionClass $originalClass) + { + if ($originalClass->isInterface()) { + throw InvalidProxiedClassException::interfaceNotSupported($originalClass); + } + } +} From ab0ca0787cbd58bf9bbb1b75fe2299ef57fa8d22 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Sat, 8 Nov 2014 09:07:35 +0100 Subject: [PATCH 41/49] #193 - verifying that `ProxyManager\Exception\CanProxyAssertion` cannot be instantiated --- .../ProxyGenerator/Assertion/CanProxyAssertionTest.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tests/ProxyManagerTest/ProxyGenerator/Assertion/CanProxyAssertionTest.php b/tests/ProxyManagerTest/ProxyGenerator/Assertion/CanProxyAssertionTest.php index 96819a637..1c9003f7d 100644 --- a/tests/ProxyManagerTest/ProxyGenerator/Assertion/CanProxyAssertionTest.php +++ b/tests/ProxyManagerTest/ProxyGenerator/Assertion/CanProxyAssertionTest.php @@ -77,6 +77,16 @@ public function testAllowedClass($className) $this->assertTrue(true); // not nice, but assertions are just fail-checks, no real code executed } + public function testDisallowsConstructor() + { + $this->setExpectedException('BadMethodCallException'); + + new CanProxyAssertion(); + } + + /** + * @return string[][] + */ public function validClasses() { return array( From 7cc76d87657ef98f3863a13828d6f635d9c52fad Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Sat, 8 Nov 2014 09:08:28 +0100 Subject: [PATCH 42/49] #193 - `ProxyManager\Exception\CanProxyAssertion` cannot be instantiated --- .../ProxyGenerator/Assertion/CanProxyAssertion.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/ProxyManager/ProxyGenerator/Assertion/CanProxyAssertion.php b/src/ProxyManager/ProxyGenerator/Assertion/CanProxyAssertion.php index fb3abd696..0492e3f68 100644 --- a/src/ProxyManager/ProxyGenerator/Assertion/CanProxyAssertion.php +++ b/src/ProxyManager/ProxyGenerator/Assertion/CanProxyAssertion.php @@ -18,6 +18,7 @@ namespace ProxyManager\ProxyGenerator\Assertion; +use BadMethodCallException; use ProxyManager\Exception\InvalidProxiedClassException; use ReflectionClass; use ReflectionMethod; @@ -31,10 +32,13 @@ final class CanProxyAssertion { /** - * Private constructor: not meant to be instantiated + * Disabled constructor: not meant to be instantiated + * + * @throws BadMethodCallException */ - private function __construct() + public function __construct() { + throw new BadMethodCallException('Unsupported constructor.'); } /** From 560a12909aab67dcb7c4722505779c234f69c4e9 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Sat, 8 Nov 2014 09:12:07 +0100 Subject: [PATCH 43/49] #193 - using `ProxyManager\Exception\CanProxyAssertion` in the generator logic --- .../AccessInterceptorScopeLocalizerGenerator.php | 12 +++--------- .../AccessInterceptorValueHolderGenerator.php | 8 +++----- .../ProxyGenerator/LazyLoadingGhostGenerator.php | 8 +++----- .../LazyLoadingValueHolderGenerator.php | 8 +++----- .../ProxyGenerator/NullObjectGenerator.php | 12 ++---------- .../ProxyGenerator/RemoteObjectGenerator.php | 8 +++----- 6 files changed, 17 insertions(+), 39 deletions(-) diff --git a/src/ProxyManager/ProxyGenerator/AccessInterceptorScopeLocalizerGenerator.php b/src/ProxyManager/ProxyGenerator/AccessInterceptorScopeLocalizerGenerator.php index c9ec1af90..f58590ed0 100644 --- a/src/ProxyManager/ProxyGenerator/AccessInterceptorScopeLocalizerGenerator.php +++ b/src/ProxyManager/ProxyGenerator/AccessInterceptorScopeLocalizerGenerator.php @@ -32,6 +32,7 @@ use ProxyManager\ProxyGenerator\AccessInterceptorScopeLocalizer\MethodGenerator\MagicSet; use ProxyManager\ProxyGenerator\AccessInterceptorScopeLocalizer\MethodGenerator\MagicSleep; use ProxyManager\ProxyGenerator\AccessInterceptorScopeLocalizer\MethodGenerator\MagicUnset; +use ProxyManager\ProxyGenerator\Assertion\CanProxyAssertion; use ProxyManager\ProxyGenerator\Util\ProxiedMethodsFilter; use ReflectionClass; use ReflectionMethod; @@ -55,13 +56,7 @@ class AccessInterceptorScopeLocalizerGenerator implements ProxyGeneratorInterfac */ public function generate(ReflectionClass $originalClass, ClassGenerator $classGenerator) { - if ($originalClass->isInterface()) { - throw InvalidProxiedClassException::interfaceNotSupported($originalClass); - } - - if ($originalClass->isFinal()) { - throw InvalidProxiedClassException::finalClassNotSupported($originalClass); - } + CanProxyAssertion::assertClassCanBeProxied($originalClass, false); $classGenerator->setExtendedClass($originalClass->getName()); $classGenerator->setImplementedInterfaces(array('ProxyManager\\Proxy\\AccessInterceptorInterface')); @@ -96,8 +91,7 @@ function (ReflectionMethod $method) use ($prefixInterceptors, $suffixInterceptor new MagicUnset($originalClass, $prefixInterceptors, $suffixInterceptors), new MagicSleep($originalClass, $prefixInterceptors, $suffixInterceptors), new MagicClone($originalClass, $prefixInterceptors, $suffixInterceptors), - ), - AbstractMethod::buildConcreteMethodsFromOriginalClass($originalClass) + ) ) ); } diff --git a/src/ProxyManager/ProxyGenerator/AccessInterceptorValueHolderGenerator.php b/src/ProxyManager/ProxyGenerator/AccessInterceptorValueHolderGenerator.php index ed927c50e..8074e826e 100644 --- a/src/ProxyManager/ProxyGenerator/AccessInterceptorValueHolderGenerator.php +++ b/src/ProxyManager/ProxyGenerator/AccessInterceptorValueHolderGenerator.php @@ -33,6 +33,7 @@ use ProxyManager\ProxyGenerator\AccessInterceptorValueHolder\MethodGenerator\MagicIsset; use ProxyManager\ProxyGenerator\AccessInterceptorValueHolder\MethodGenerator\MagicSet; use ProxyManager\ProxyGenerator\AccessInterceptorValueHolder\MethodGenerator\MagicUnset; +use ProxyManager\ProxyGenerator\Assertion\CanProxyAssertion; use ProxyManager\ProxyGenerator\LazyLoadingValueHolder\PropertyGenerator\ValueHolderProperty; use ProxyManager\ProxyGenerator\PropertyGenerator\PublicPropertiesMap; use ProxyManager\ProxyGenerator\Util\ProxiedMethodsFilter; @@ -60,9 +61,7 @@ class AccessInterceptorValueHolderGenerator implements ProxyGeneratorInterface */ public function generate(ReflectionClass $originalClass, ClassGenerator $classGenerator) { - if ($originalClass->isFinal()) { - throw InvalidProxiedClassException::finalClassNotSupported($originalClass); - } + CanProxyAssertion::assertClassCanBeProxied($originalClass); $publicProperties = new PublicPropertiesMap($originalClass); $interfaces = array( @@ -134,8 +133,7 @@ function (ReflectionMethod $method) use ($prefixInterceptors, $suffixInterceptor new MagicClone($originalClass, $valueHolder, $prefixInterceptors, $suffixInterceptors), new MagicSleep($originalClass, $valueHolder), new MagicWakeup($originalClass, $valueHolder), - ), - AbstractMethod::buildConcreteMethodsFromOriginalClass($originalClass) + ) ) ); } diff --git a/src/ProxyManager/ProxyGenerator/LazyLoadingGhostGenerator.php b/src/ProxyManager/ProxyGenerator/LazyLoadingGhostGenerator.php index 296629f40..155015499 100644 --- a/src/ProxyManager/ProxyGenerator/LazyLoadingGhostGenerator.php +++ b/src/ProxyManager/ProxyGenerator/LazyLoadingGhostGenerator.php @@ -21,6 +21,7 @@ use ProxyManager\Exception\InvalidProxiedClassException; use ProxyManager\Generator\Util\ClassGeneratorUtils; use ProxyManager\ProxyGenerator\AccessInterceptorScopeLocalizer\MethodGenerator\AbstractMethod; +use ProxyManager\ProxyGenerator\Assertion\CanProxyAssertion; use ProxyManager\ProxyGenerator\LazyLoading\MethodGenerator\Constructor; use ProxyManager\ProxyGenerator\LazyLoadingGhost\MethodGenerator\CallInitializer; use ProxyManager\ProxyGenerator\LazyLoadingGhost\MethodGenerator\GetProxyInitializer; @@ -60,9 +61,7 @@ class LazyLoadingGhostGenerator implements ProxyGeneratorInterface */ public function generate(ReflectionClass $originalClass, ClassGenerator $classGenerator) { - if ($originalClass->isFinal()) { - throw InvalidProxiedClassException::finalClassNotSupported($originalClass); - } + CanProxyAssertion::assertClassCanBeProxied($originalClass); $interfaces = array('ProxyManager\\Proxy\\GhostObjectInterface'); $publicProperties = new PublicPropertiesMap($originalClass); @@ -110,8 +109,7 @@ function (ReflectionMethod $method) use ($initializer, $init) { new GetProxyInitializer($initializer), new InitializeProxy($initializer, $init), new IsProxyInitialized($initializer), - ), - AbstractMethod::buildConcreteMethodsFromOriginalClass($originalClass) + ) ) ); } diff --git a/src/ProxyManager/ProxyGenerator/LazyLoadingValueHolderGenerator.php b/src/ProxyManager/ProxyGenerator/LazyLoadingValueHolderGenerator.php index f5b46b6fc..fbb3281df 100644 --- a/src/ProxyManager/ProxyGenerator/LazyLoadingValueHolderGenerator.php +++ b/src/ProxyManager/ProxyGenerator/LazyLoadingValueHolderGenerator.php @@ -21,6 +21,7 @@ use ProxyManager\Exception\InvalidProxiedClassException; use ProxyManager\ProxyGenerator\AccessInterceptor\MethodGenerator\MagicWakeup; use ProxyManager\ProxyGenerator\AccessInterceptorScopeLocalizer\MethodGenerator\AbstractMethod; +use ProxyManager\ProxyGenerator\Assertion\CanProxyAssertion; use ProxyManager\ProxyGenerator\LazyLoading\MethodGenerator\Constructor; use ProxyManager\ProxyGenerator\LazyLoadingValueHolder\MethodGenerator\GetProxyInitializer; use ProxyManager\ProxyGenerator\LazyLoadingValueHolder\MethodGenerator\InitializeProxy; @@ -60,9 +61,7 @@ class LazyLoadingValueHolderGenerator implements ProxyGeneratorInterface */ public function generate(ReflectionClass $originalClass, ClassGenerator $classGenerator) { - if ($originalClass->isFinal()) { - throw InvalidProxiedClassException::finalClassNotSupported($originalClass); - } + CanProxyAssertion::assertClassCanBeProxied($originalClass); $interfaces = array('ProxyManager\\Proxy\\VirtualProxyInterface'); $publicProperties = new PublicPropertiesMap($originalClass); @@ -107,8 +106,7 @@ function (ReflectionMethod $method) use ($initializer, $valueHolder) { new InitializeProxy($initializer, $valueHolder), new IsProxyInitialized($valueHolder), new GetWrappedValueHolderValue($valueHolder), - ), - AbstractMethod::buildConcreteMethodsFromOriginalClass($originalClass) + ) ) ); } diff --git a/src/ProxyManager/ProxyGenerator/NullObjectGenerator.php b/src/ProxyManager/ProxyGenerator/NullObjectGenerator.php index 58c4d73f8..ad4c28009 100644 --- a/src/ProxyManager/ProxyGenerator/NullObjectGenerator.php +++ b/src/ProxyManager/ProxyGenerator/NullObjectGenerator.php @@ -21,6 +21,7 @@ use ProxyManager\Exception\InvalidProxiedClassException; use ProxyManager\Generator\Util\ClassGeneratorUtils; use ProxyManager\ProxyGenerator\AccessInterceptorScopeLocalizer\MethodGenerator\AbstractMethod; +use ProxyManager\ProxyGenerator\Assertion\CanProxyAssertion; use ProxyManager\ProxyGenerator\NullObject\MethodGenerator\Constructor; use ProxyManager\ProxyGenerator\NullObject\MethodGenerator\NullObjectMethodInterceptor; use ProxyManager\ProxyGenerator\Util\ProxiedMethodsFilter; @@ -44,9 +45,7 @@ class NullObjectGenerator implements ProxyGeneratorInterface */ public function generate(ReflectionClass $originalClass, ClassGenerator $classGenerator) { - if ($originalClass->isFinal()) { - throw InvalidProxiedClassException::finalClassNotSupported($originalClass); - } + CanProxyAssertion::assertClassCanBeProxied($originalClass); $interfaces = array('ProxyManager\\Proxy\\NullObjectInterface'); @@ -64,13 +63,6 @@ public function generate(ReflectionClass $originalClass, ClassGenerator $classGe ); } - array_map( - function (MethodGenerator $generatedMethod) use ($originalClass, $classGenerator) { - ClassGeneratorUtils::addMethodIfNotFinal($originalClass, $classGenerator, $generatedMethod); - }, - AbstractMethod::buildConcreteMethodsFromOriginalClass($originalClass) - ); - ClassGeneratorUtils::addMethodIfNotFinal($originalClass, $classGenerator, new Constructor($originalClass)); } } diff --git a/src/ProxyManager/ProxyGenerator/RemoteObjectGenerator.php b/src/ProxyManager/ProxyGenerator/RemoteObjectGenerator.php index 9bf31c41b..87b2ea527 100644 --- a/src/ProxyManager/ProxyGenerator/RemoteObjectGenerator.php +++ b/src/ProxyManager/ProxyGenerator/RemoteObjectGenerator.php @@ -21,6 +21,7 @@ use ProxyManager\Exception\InvalidProxiedClassException; use ProxyManager\Generator\Util\ClassGeneratorUtils; use ProxyManager\ProxyGenerator\AccessInterceptorScopeLocalizer\MethodGenerator\AbstractMethod; +use ProxyManager\ProxyGenerator\Assertion\CanProxyAssertion; use ProxyManager\ProxyGenerator\RemoteObject\MethodGenerator\Constructor; use ProxyManager\ProxyGenerator\RemoteObject\MethodGenerator\MagicGet; use ProxyManager\ProxyGenerator\RemoteObject\MethodGenerator\MagicIsset; @@ -50,9 +51,7 @@ class RemoteObjectGenerator implements ProxyGeneratorInterface */ public function generate(ReflectionClass $originalClass, ClassGenerator $classGenerator) { - if ($originalClass->isFinal()) { - throw InvalidProxiedClassException::finalClassNotSupported($originalClass); - } + CanProxyAssertion::assertClassCanBeProxied($originalClass); $interfaces = array('ProxyManager\\Proxy\\RemoteObjectInterface'); @@ -89,8 +88,7 @@ function (ReflectionMethod $method) use ($adapter, $originalClass) { new MagicSet($originalClass, $adapter), new MagicIsset($originalClass, $adapter), new MagicUnset($originalClass, $adapter), - ), - AbstractMethod::buildConcreteMethodsFromOriginalClass($originalClass) + ) ) ); } From 06171ed2bdd428381117c4581e378cc03e2733b3 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Sat, 8 Nov 2014 09:13:23 +0100 Subject: [PATCH 44/49] #193 - removing `ProxyManager\ProxyGenerator\AccessInterceptorScopeLocalizer\MethodGenerator\AbstractMethod`, as we decided to NOT proxy abstract protected methods --- .../MethodGenerator/AbstractMethod.php | 68 ------------------- 1 file changed, 68 deletions(-) delete mode 100644 src/ProxyManager/ProxyGenerator/AccessInterceptorScopeLocalizer/MethodGenerator/AbstractMethod.php diff --git a/src/ProxyManager/ProxyGenerator/AccessInterceptorScopeLocalizer/MethodGenerator/AbstractMethod.php b/src/ProxyManager/ProxyGenerator/AccessInterceptorScopeLocalizer/MethodGenerator/AbstractMethod.php deleted file mode 100644 index f43333647..000000000 --- a/src/ProxyManager/ProxyGenerator/AccessInterceptorScopeLocalizer/MethodGenerator/AbstractMethod.php +++ /dev/null @@ -1,68 +0,0 @@ - - * @license MIT - */ -class AbstractMethod extends MethodGenerator -{ - /** - * Constructor. - * - * @param ReflectionMethod $method - */ - public function __construct(ReflectionMethod $method) - { - parent::__construct($method->getName()); - - foreach ($method->getParameters() as $param) { - $this->setParameter($param->getName()); - } - } - - /** - * Return a collection of abstractMethods objects. - * - * @param ReflectionClass $originalClass - * - * @return self[] - */ - public static function buildConcreteMethodsFromOriginalClass(ReflectionClass $originalClass) - { - $methodCollection = array(); - - array_map(function (ReflectionMethod $methodName) use ( - & $methodCollection, - $originalClass - ) { - $methodCollection[] = new AbstractMethod($methodName); - }, $originalClass->getMethods(ReflectionMethod::IS_ABSTRACT)); - - return $methodCollection; - } -} From 30bb59b0e9ae5fd06457ac5d65c26d319371ce6b Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Sat, 8 Nov 2014 09:14:17 +0100 Subject: [PATCH 45/49] #193 - optimized imports --- .../AccessInterceptorScopeLocalizerGenerator.php | 1 - .../ProxyGenerator/AccessInterceptorValueHolderGenerator.php | 1 - src/ProxyManager/ProxyGenerator/LazyLoadingGhostGenerator.php | 1 - .../ProxyGenerator/LazyLoadingValueHolderGenerator.php | 3 +-- src/ProxyManager/ProxyGenerator/NullObjectGenerator.php | 2 -- src/ProxyManager/ProxyGenerator/RemoteObjectGenerator.php | 1 - 6 files changed, 1 insertion(+), 8 deletions(-) diff --git a/src/ProxyManager/ProxyGenerator/AccessInterceptorScopeLocalizerGenerator.php b/src/ProxyManager/ProxyGenerator/AccessInterceptorScopeLocalizerGenerator.php index f58590ed0..d6fd77e56 100644 --- a/src/ProxyManager/ProxyGenerator/AccessInterceptorScopeLocalizerGenerator.php +++ b/src/ProxyManager/ProxyGenerator/AccessInterceptorScopeLocalizerGenerator.php @@ -18,7 +18,6 @@ namespace ProxyManager\ProxyGenerator; -use ProxyManager\Exception\InvalidProxiedClassException; use ProxyManager\Generator\Util\ClassGeneratorUtils; use ProxyManager\ProxyGenerator\AccessInterceptor\MethodGenerator\SetMethodPrefixInterceptor; use ProxyManager\ProxyGenerator\AccessInterceptor\MethodGenerator\SetMethodSuffixInterceptor; diff --git a/src/ProxyManager/ProxyGenerator/AccessInterceptorValueHolderGenerator.php b/src/ProxyManager/ProxyGenerator/AccessInterceptorValueHolderGenerator.php index 8074e826e..1627f8ab1 100644 --- a/src/ProxyManager/ProxyGenerator/AccessInterceptorValueHolderGenerator.php +++ b/src/ProxyManager/ProxyGenerator/AccessInterceptorValueHolderGenerator.php @@ -18,7 +18,6 @@ namespace ProxyManager\ProxyGenerator; -use ProxyManager\Exception\InvalidProxiedClassException; use ProxyManager\Generator\Util\ClassGeneratorUtils; use ProxyManager\ProxyGenerator\AccessInterceptor\MethodGenerator\MagicWakeup; use ProxyManager\ProxyGenerator\AccessInterceptor\MethodGenerator\SetMethodPrefixInterceptor; diff --git a/src/ProxyManager/ProxyGenerator/LazyLoadingGhostGenerator.php b/src/ProxyManager/ProxyGenerator/LazyLoadingGhostGenerator.php index 155015499..427583de0 100644 --- a/src/ProxyManager/ProxyGenerator/LazyLoadingGhostGenerator.php +++ b/src/ProxyManager/ProxyGenerator/LazyLoadingGhostGenerator.php @@ -18,7 +18,6 @@ namespace ProxyManager\ProxyGenerator; -use ProxyManager\Exception\InvalidProxiedClassException; use ProxyManager\Generator\Util\ClassGeneratorUtils; use ProxyManager\ProxyGenerator\AccessInterceptorScopeLocalizer\MethodGenerator\AbstractMethod; use ProxyManager\ProxyGenerator\Assertion\CanProxyAssertion; diff --git a/src/ProxyManager/ProxyGenerator/LazyLoadingValueHolderGenerator.php b/src/ProxyManager/ProxyGenerator/LazyLoadingValueHolderGenerator.php index fbb3281df..18eaf359c 100644 --- a/src/ProxyManager/ProxyGenerator/LazyLoadingValueHolderGenerator.php +++ b/src/ProxyManager/ProxyGenerator/LazyLoadingValueHolderGenerator.php @@ -18,7 +18,7 @@ namespace ProxyManager\ProxyGenerator; -use ProxyManager\Exception\InvalidProxiedClassException; +use ProxyManager\Generator\Util\ClassGeneratorUtils; use ProxyManager\ProxyGenerator\AccessInterceptor\MethodGenerator\MagicWakeup; use ProxyManager\ProxyGenerator\AccessInterceptorScopeLocalizer\MethodGenerator\AbstractMethod; use ProxyManager\ProxyGenerator\Assertion\CanProxyAssertion; @@ -39,7 +39,6 @@ use ProxyManager\ProxyGenerator\PropertyGenerator\PublicPropertiesMap; use ProxyManager\ProxyGenerator\Util\ProxiedMethodsFilter; use ProxyManager\ProxyGenerator\ValueHolder\MethodGenerator\GetWrappedValueHolderValue; -use ProxyManager\Generator\Util\ClassGeneratorUtils; use ReflectionClass; use ReflectionMethod; use Zend\Code\Generator\ClassGenerator; diff --git a/src/ProxyManager/ProxyGenerator/NullObjectGenerator.php b/src/ProxyManager/ProxyGenerator/NullObjectGenerator.php index ad4c28009..1345f157d 100644 --- a/src/ProxyManager/ProxyGenerator/NullObjectGenerator.php +++ b/src/ProxyManager/ProxyGenerator/NullObjectGenerator.php @@ -18,7 +18,6 @@ namespace ProxyManager\ProxyGenerator; -use ProxyManager\Exception\InvalidProxiedClassException; use ProxyManager\Generator\Util\ClassGeneratorUtils; use ProxyManager\ProxyGenerator\AccessInterceptorScopeLocalizer\MethodGenerator\AbstractMethod; use ProxyManager\ProxyGenerator\Assertion\CanProxyAssertion; @@ -27,7 +26,6 @@ use ProxyManager\ProxyGenerator\Util\ProxiedMethodsFilter; use ReflectionClass; use Zend\Code\Generator\ClassGenerator; -use Zend\Code\Generator\MethodGenerator; use Zend\Code\Reflection\MethodReflection; /** diff --git a/src/ProxyManager/ProxyGenerator/RemoteObjectGenerator.php b/src/ProxyManager/ProxyGenerator/RemoteObjectGenerator.php index 87b2ea527..d395f6c69 100644 --- a/src/ProxyManager/ProxyGenerator/RemoteObjectGenerator.php +++ b/src/ProxyManager/ProxyGenerator/RemoteObjectGenerator.php @@ -18,7 +18,6 @@ namespace ProxyManager\ProxyGenerator; -use ProxyManager\Exception\InvalidProxiedClassException; use ProxyManager\Generator\Util\ClassGeneratorUtils; use ProxyManager\ProxyGenerator\AccessInterceptorScopeLocalizer\MethodGenerator\AbstractMethod; use ProxyManager\ProxyGenerator\Assertion\CanProxyAssertion; From 680783fcb848baf838ac0364b04edfda062b231f Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Sat, 8 Nov 2014 09:18:42 +0100 Subject: [PATCH 46/49] #193 - skip heavy integration tests on HHVM (too slow for now) --- .../Functional/FatalPreventionFunctionalTest.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/ProxyManagerTest/Functional/FatalPreventionFunctionalTest.php b/tests/ProxyManagerTest/Functional/FatalPreventionFunctionalTest.php index e1c8dbbc7..2b335bbb7 100644 --- a/tests/ProxyManagerTest/Functional/FatalPreventionFunctionalTest.php +++ b/tests/ProxyManagerTest/Functional/FatalPreventionFunctionalTest.php @@ -68,6 +68,10 @@ class FatalPreventionFunctionalTest extends PHPUnit_Framework_TestCase */ public function testCodeGeneration($generatorClass, $className) { + if (defined('HHVM_VERSION')) { + $this->markTestSkipped('HHVM is just too slow for this kind of test right now.'); + } + $runner = PHPUnit_Util_PHP::factory(); $code = sprintf( From 892c15ae2eaa542b64edada14c04ecd93602f202 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Sat, 8 Nov 2014 09:27:48 +0100 Subject: [PATCH 47/49] #193 - minor CS fix as per PHP_CodeSniffer --- .../ProxyGenerator/Assertion/CanProxyAssertion.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/ProxyManager/ProxyGenerator/Assertion/CanProxyAssertion.php b/src/ProxyManager/ProxyGenerator/Assertion/CanProxyAssertion.php index 0492e3f68..213e15d9d 100644 --- a/src/ProxyManager/ProxyGenerator/Assertion/CanProxyAssertion.php +++ b/src/ProxyManager/ProxyGenerator/Assertion/CanProxyAssertion.php @@ -76,12 +76,14 @@ private static function isNotFinal(ReflectionClass $originalClass) */ private static function hasNoAbstractProtectedMethods(ReflectionClass $originalClass) { - if (array_filter( + $protectedAbstract = array_filter( $originalClass->getMethods(), function (ReflectionMethod $method) { return $method->isAbstract() && $method->isProtected(); } - )) { + ); + + if ($protectedAbstract) { throw InvalidProxiedClassException::abstractProtectedMethodsNotSupported($originalClass); } } From 3147025c5e5fc259812668bf25a979b27df4a704 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Sat, 8 Nov 2014 09:34:53 +0100 Subject: [PATCH 48/49] #193 - test skipping should not be defined in the data provider, but in the actual test --- .../Functional/FatalPreventionFunctionalTest.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/ProxyManagerTest/Functional/FatalPreventionFunctionalTest.php b/tests/ProxyManagerTest/Functional/FatalPreventionFunctionalTest.php index 2b335bbb7..4c13d99b3 100644 --- a/tests/ProxyManagerTest/Functional/FatalPreventionFunctionalTest.php +++ b/tests/ProxyManagerTest/Functional/FatalPreventionFunctionalTest.php @@ -72,6 +72,10 @@ public function testCodeGeneration($generatorClass, $className) $this->markTestSkipped('HHVM is just too slow for this kind of test right now.'); } + if (PHP_VERSION_ID < 50401) { + $this->markTestSkipped('Can\'t run this Test Suit on php 5.3 above'); + } + $runner = PHPUnit_Util_PHP::factory(); $code = sprintf( @@ -135,10 +139,6 @@ function ($class) use ($generator) { */ public function getProxyTestedClasses() { - if (PHP_VERSION_ID < 50401) { - $this->markTestSkipped('Can\'t run this Test Suit on php 5.3 above'); - } - $skippedPaths = array( realpath(__DIR__ . '/../../src'), realpath(__DIR__ . '/../../vendor'), From a5626df4246afcef8f0b3e686a0bb116c8758dd8 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Sat, 8 Nov 2014 09:35:41 +0100 Subject: [PATCH 49/49] #193 - correct error message when skipping the fatal prevention functional tests --- .../Functional/FatalPreventionFunctionalTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/ProxyManagerTest/Functional/FatalPreventionFunctionalTest.php b/tests/ProxyManagerTest/Functional/FatalPreventionFunctionalTest.php index 4c13d99b3..290d52bba 100644 --- a/tests/ProxyManagerTest/Functional/FatalPreventionFunctionalTest.php +++ b/tests/ProxyManagerTest/Functional/FatalPreventionFunctionalTest.php @@ -73,7 +73,7 @@ public function testCodeGeneration($generatorClass, $className) } if (PHP_VERSION_ID < 50401) { - $this->markTestSkipped('Can\'t run this Test Suit on php 5.3 above'); + $this->markTestSkipped('Can\'t run this test suite on php < 5.4.1'); } $runner = PHPUnit_Util_PHP::factory();