diff --git a/library/Zend/Code/Generator/ClassGenerator.php b/library/Zend/Code/Generator/ClassGenerator.php index 29ae09ea899..1d3dc6232de 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); } } 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); diff --git a/tests/ZendTest/Code/Generator/ClassGeneratorTest.php b/tests/ZendTest/Code/Generator/ClassGeneratorTest.php index a11e16adc21..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 @@ -266,6 +264,18 @@ public function testClassFromReflectionDiscardParentImplementedInterfaces() $this->assertContains($expectedClassDef, $code); } + /** + * @group 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 */ @@ -379,7 +389,7 @@ public function testAddUses() } /** - * @group gh-4990 + * @group 4990 */ public function testAddOneUseTwiceOnlyAddsOne() { @@ -395,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 d5979271c4f..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 @@ -202,4 +200,19 @@ public function testCreateFromArray() $this->assertEquals('foo', $parameterGenerator->getSourceContent()); $this->assertEquals('-', $parameterGenerator->getIndentation()); } + + /** + * @group 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..d89aa19c5b3 --- /dev/null +++ b/tests/ZendTest/Code/TestAsset/NonNamespaceClass.php @@ -0,0 +1,15 @@ +