From 1e3f5f61dae1ee7de479ea99b18ed9a53a7cc65d Mon Sep 17 00:00:00 2001 From: Daniel Tschinder Date: Wed, 21 Aug 2013 10:17:18 +0200 Subject: [PATCH 1/4] Added tests for issue #4988 --- .../Code/Generator/ClassGeneratorTest.php | 12 ++++++++++++ .../Code/Generator/ParameterGeneratorTest.php | 15 +++++++++++++++ .../ZendTest/Code/TestAsset/NonNamespaceClass.php | 8 ++++++++ 3 files changed, 35 insertions(+) create mode 100644 tests/ZendTest/Code/TestAsset/NonNamespaceClass.php diff --git a/tests/ZendTest/Code/Generator/ClassGeneratorTest.php b/tests/ZendTest/Code/Generator/ClassGeneratorTest.php index 449b7824056..a9fc1b07917 100644 --- a/tests/ZendTest/Code/Generator/ClassGeneratorTest.php +++ b/tests/ZendTest/Code/Generator/ClassGeneratorTest.php @@ -266,6 +266,18 @@ public function testClassFromReflectionDiscardParentImplementedInterfaces() $this->assertContains($expectedClassDef, $code); } + /** + * @group gh-4988 + */ + public function testNonNamespaceClassReturnsAllMethods() + { + require_once __DIR__ . '/../TestAsset/NonNamespaceClass.php'; + + $reflClass = new ClassReflection('ZendTest_Code_NsTest_BarClass'); + $classGenerator = ClassGenerator::fromReflection($reflClass); + $this->assertCount(1, $classGenerator->getMethods()); + } + /** * @group ZF-9602 */ diff --git a/tests/ZendTest/Code/Generator/ParameterGeneratorTest.php b/tests/ZendTest/Code/Generator/ParameterGeneratorTest.php index d5979271c4f..6bd7ad45ef5 100644 --- a/tests/ZendTest/Code/Generator/ParameterGeneratorTest.php +++ b/tests/ZendTest/Code/Generator/ParameterGeneratorTest.php @@ -202,4 +202,19 @@ public function testCreateFromArray() $this->assertEquals('foo', $parameterGenerator->getSourceContent()); $this->assertEquals('-', $parameterGenerator->getIndentation()); } + + /** + * @group gh-4988 + */ + public function testParameterGeneratorReturnsCorrectTypeForNonNamespaceClasses() + { + require_once __DIR__ . '/../TestAsset/NonNamespaceClass.php'; + + $reflClass = new \Zend\Code\Reflection\ClassReflection('ZendTest_Code_NsTest_BarClass'); + $params = $reflClass->getMethod('fooMethod')->getParameters(); + + $param = ParameterGenerator::fromReflection($params[0]); + + $this->assertEquals('\ZendTest_Code_NsTest_BarClass', $param->getType()); + } } diff --git a/tests/ZendTest/Code/TestAsset/NonNamespaceClass.php b/tests/ZendTest/Code/TestAsset/NonNamespaceClass.php new file mode 100644 index 00000000000..d16f02b9ff1 --- /dev/null +++ b/tests/ZendTest/Code/TestAsset/NonNamespaceClass.php @@ -0,0 +1,8 @@ + Date: Tue, 20 Aug 2013 10:47:49 +0200 Subject: [PATCH 2/4] If no namespace used do not try to concatenate the namespace If using ClassGenerator for non namespace classes no methods are found inside classes, as the classname check fails always. --- library/Zend/Code/Generator/ClassGenerator.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/library/Zend/Code/Generator/ClassGenerator.php b/library/Zend/Code/Generator/ClassGenerator.php index 80eedaa9151..0da5a1665db 100644 --- a/library/Zend/Code/Generator/ClassGenerator.php +++ b/library/Zend/Code/Generator/ClassGenerator.php @@ -118,7 +118,8 @@ public static function fromReflection(ClassReflection $classReflection) $methods = array(); foreach ($classReflection->getMethods() as $reflectionMethod) { - if ($reflectionMethod->getDeclaringClass()->getName() == $cg->getNamespaceName() . "\\" . $cg->getName()) { + $className = ($cg->getNamespaceName())? $cg->getNamespaceName() . "\\" . $cg->getName() : $cg->getName(); + if ($reflectionMethod->getDeclaringClass()->getName() == $className) { $methods[] = MethodGenerator::fromReflection($reflectionMethod); } } From 29dbe1ac4341b1c49f3b9f0c27805dcdd3ea55ab Mon Sep 17 00:00:00 2001 From: Daniel Tschinder Date: Tue, 20 Aug 2013 10:51:45 +0200 Subject: [PATCH 3/4] Only try to remove the namespace from a classname if there is a namespace and add backslash for non namespace classes For non-namespace classes this substr/strlen algorithm removed the first character of the classname. The slash is needed when generating namespace classes, which use non-namespace classes. --- library/Zend/Code/Generator/ParameterGenerator.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/library/Zend/Code/Generator/ParameterGenerator.php b/library/Zend/Code/Generator/ParameterGenerator.php index f85d7d4c944..f9269fee47c 100644 --- a/library/Zend/Code/Generator/ParameterGenerator.php +++ b/library/Zend/Code/Generator/ParameterGenerator.php @@ -64,8 +64,12 @@ public static function fromReflection(ParameterReflection $reflectionParameter) $parameterType = $typeClass->getName(); $currentNamespace = $reflectionParameter->getDeclaringClass()->getNamespaceName(); - if (substr($parameterType, 0, strlen($currentNamespace)) == $currentNamespace) { - $parameterType = substr($parameterType, strlen($currentNamespace)+1); + if (!empty($currentNamespace)) { + if (substr($parameterType, 0, strlen($currentNamespace)) == $currentNamespace) { + $parameterType = substr($parameterType, strlen($currentNamespace) + 1); + } + } else { + $parameterType = '\\' . trim($parameterType, '\\'); } $param->setType($parameterType); From 71d2c39c297fc52ee9bc610abbade72c0fa0df4e Mon Sep 17 00:00:00 2001 From: Matthew Weier O'Phinney Date: Wed, 21 Aug 2013 09:29:51 -0500 Subject: [PATCH 4/4] Remove "gh-" prefix from group annotations - Also removed `@package` annotations - And added license block to test asset class file --- tests/ZendTest/Code/Generator/ClassGeneratorTest.php | 8 +++----- tests/ZendTest/Code/Generator/ParameterGeneratorTest.php | 4 +--- tests/ZendTest/Code/TestAsset/NonNamespaceClass.php | 7 +++++++ 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/tests/ZendTest/Code/Generator/ClassGeneratorTest.php b/tests/ZendTest/Code/Generator/ClassGeneratorTest.php index a381c3ddeec..44f7d9fa87f 100644 --- a/tests/ZendTest/Code/Generator/ClassGeneratorTest.php +++ b/tests/ZendTest/Code/Generator/ClassGeneratorTest.php @@ -5,7 +5,6 @@ * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @package Zend_Code */ namespace ZendTest\Code\Generator; @@ -18,7 +17,6 @@ /** * @category Zend - * @package Zend_Code_Generator * @subpackage UnitTests * * @group Zend_Code_Generator @@ -267,7 +265,7 @@ public function testClassFromReflectionDiscardParentImplementedInterfaces() } /** - * @group gh-4988 + * @group 4988 */ public function testNonNamespaceClassReturnsAllMethods() { @@ -391,7 +389,7 @@ public function testAddUses() } /** - * @group gh-4990 + * @group 4990 */ public function testAddOneUseTwiceOnlyAddsOne() { @@ -407,7 +405,7 @@ public function testAddOneUseTwiceOnlyAddsOne() } /** - * @group gh-4990 + * @group 4990 */ public function testAddOneUseWithAliasTwiceOnlyAddsOne() { diff --git a/tests/ZendTest/Code/Generator/ParameterGeneratorTest.php b/tests/ZendTest/Code/Generator/ParameterGeneratorTest.php index 6bd7ad45ef5..8a4bdf59763 100644 --- a/tests/ZendTest/Code/Generator/ParameterGeneratorTest.php +++ b/tests/ZendTest/Code/Generator/ParameterGeneratorTest.php @@ -5,7 +5,6 @@ * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @package Zend_Code */ namespace ZendTest\Code\Generator; @@ -16,7 +15,6 @@ /** * @category Zend - * @package Zend_Code_Generator * @subpackage UnitTests * * @group Zend_Code_Generator @@ -204,7 +202,7 @@ public function testCreateFromArray() } /** - * @group gh-4988 + * @group 4988 */ public function testParameterGeneratorReturnsCorrectTypeForNonNamespaceClasses() { diff --git a/tests/ZendTest/Code/TestAsset/NonNamespaceClass.php b/tests/ZendTest/Code/TestAsset/NonNamespaceClass.php index d16f02b9ff1..d89aa19c5b3 100644 --- a/tests/ZendTest/Code/TestAsset/NonNamespaceClass.php +++ b/tests/ZendTest/Code/TestAsset/NonNamespaceClass.php @@ -1,4 +1,11 @@