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); } } 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 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 @@ +