Skip to content
This repository has been archived by the owner on Jan 8, 2020. It is now read-only.

Commit

Permalink
Merge branch 'hotfix/5193' into develop
Browse files Browse the repository at this point in the history
Forward port #5193
  • Loading branch information
weierophinney committed Oct 23, 2013
2 parents 8ef07dd + 5eb2499 commit ba18b6b
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 7 deletions.
6 changes: 2 additions & 4 deletions library/Zend/Code/Generator/ParameterGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,8 @@ public static function fromReflection(ParameterReflection $reflectionParameter)
$parameterType = $typeClass->getName();
$currentNamespace = $reflectionParameter->getDeclaringClass()->getNamespaceName();

if (!empty($currentNamespace)) {
if (substr($parameterType, 0, strlen($currentNamespace)) == $currentNamespace) {
$parameterType = substr($parameterType, strlen($currentNamespace) + 1);
}
if (!empty($currentNamespace) && substr($parameterType, 0, strlen($currentNamespace)) == $currentNamespace) {
$parameterType = substr($parameterType, strlen($currentNamespace) + 1);
} else {
$parameterType = '\\' . trim($parameterType, '\\');
}
Expand Down
21 changes: 18 additions & 3 deletions tests/ZendTest/Code/Generator/ParameterGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public function testFromReflectionGetParameterType()
$reflectionParameter = $this->getFirstReflectionParameter('type');
$codeGenParam = ParameterGenerator::fromReflection($reflectionParameter);

$this->assertEquals('stdClass', $codeGenParam->getType());
$this->assertEquals('\\stdClass', $codeGenParam->getType());
}

public function testFromReflectionGetReference()
Expand Down Expand Up @@ -142,7 +142,7 @@ public function dataFromReflectionGenerate()
{
return array(
array('name', '$param'),
array('type', 'stdClass $bar'),
array('type', '\\stdClass $bar'),
array('reference', '&$baz'),
array('defaultValue', '$value = \'foo\''),
array('defaultNull', '$value = null'),
Expand All @@ -161,7 +161,7 @@ public function dataFromReflectionGenerate()

/**
* @param string $method
* @return \Zend\Reflection\ReflectionParameter
* @return \Zend\Code\Reflection\ParameterReflection
*/
protected function getFirstReflectionParameter($method)
{
Expand Down Expand Up @@ -212,4 +212,19 @@ public function testParameterGeneratorReturnsCorrectTypeForNonNamespaceClasses()

$this->assertEquals('\ZendTest_Code_NsTest_BarClass', $param->getType());
}

/**
* @group 5193
*/
public function testTypehintsWithNamespaceInNamepsacedClassReturnTypewithBackslash()
{
require_once __DIR__ . '/TestAsset/NamespaceTypeHintClass.php';

$reflClass = new \Zend\Code\Reflection\ClassReflection('Namespaced\TypeHint\Bar');
$params = $reflClass->getMethod('method')->getParameters();

$param = ParameterGenerator::fromReflection($params[0]);

$this->assertEquals('\OtherNamespace\ParameterClass', $param->getType());
}
}
29 changes: 29 additions & 0 deletions tests/ZendTest/Code/Generator/TestAsset/NamespaceTypeHintClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @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
*/

namespace Namespaced\TypeHint {

use OtherNamespace\ParameterClass;

class Bar
{

public function method(ParameterClass $object)
{
}
}
}

namespace OtherNamespace {

class ParameterClass
{

}
}

0 comments on commit ba18b6b

Please sign in to comment.