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

Commit 9debbfb

Browse files
committed
Merge branch 'hotfix/69' into develop
Forward port #69
2 parents a1c8eab + 61f882d commit 9debbfb

File tree

3 files changed

+66
-5
lines changed

3 files changed

+66
-5
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ All notable changes to this project will be documented in this file, in reverse
4141
how parameter typehints were generated; previously, fully-qualified class
4242
names were not being generated with the leading backslash, causing them to
4343
attempt to resolve as if they were relative to the current namespace.
44+
- [#69](https://github.com/zendframework/zend-code/pull/69) fixes an issue with
45+
how class names under the same namespace are generated when generating
46+
typehints, extends, and implements values; they now strip the
47+
common namespace from the class name.
4448

4549
## 3.0.2 - 2016-04-20
4650

src/Generator/ClassGenerator.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -950,12 +950,13 @@ public function generate()
950950
$output .= static::OBJECT_TYPE . ' ' . $this->getName();
951951

952952
if (!empty($this->extendedClass)) {
953-
$output .= ' extends ' . $this->extendedClass;
953+
$output .= ' extends ' . $this->generateShortOrCompleteClassname($this->extendedClass);
954954
}
955955

956956
$implemented = $this->getImplementedInterfaces();
957957

958958
if (!empty($implemented)) {
959+
$implemented = array_map([$this, 'generateShortOrCompleteClassname'], $implemented);
959960
$output .= ' ' . static::IMPLEMENTS_KEYWORD . ' ' . implode(', ', $implemented);
960961
}
961962

@@ -1009,4 +1010,23 @@ private function validateConstantValue($value)
10091010
gettype($value)
10101011
));
10111012
}
1013+
1014+
/**
1015+
* @param string $fqnClassName
1016+
*
1017+
* @return string
1018+
*/
1019+
private function generateShortOrCompleteClassname($fqnClassName)
1020+
{
1021+
$parts = explode('\\', $fqnClassName);
1022+
$className = array_pop($parts);
1023+
$classNamespace = implode('\\', $parts);
1024+
$currentNamespace = (string) $this->getNamespaceName();
1025+
1026+
if ($classNamespace === $currentNamespace || in_array($fqnClassName, $this->getUses())) {
1027+
return $className;
1028+
}
1029+
1030+
return '\\' . $fqnClassName;
1031+
}
10121032
}

test/Generator/ClassGeneratorTest.php

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -242,8 +242,8 @@ public function testClassFromReflectionThatImplementsInterfaces()
242242
$code = $classGenerator->generate();
243243

244244
$expectedClassDef = 'class ClassWithInterface'
245-
. ' implements ZendTest\Code\Generator\TestAsset\OneInterface'
246-
. ', ZendTest\Code\Generator\TestAsset\TwoInterface';
245+
. ' implements OneInterface'
246+
. ', TwoInterface';
247247
$this->assertContains($expectedClassDef, $code);
248248
}
249249

@@ -260,8 +260,8 @@ public function testClassFromReflectionDiscardParentImplementedInterfaces()
260260
$code = $classGenerator->generate();
261261

262262
$expectedClassDef = 'class NewClassWithInterface'
263-
. ' extends ZendTest\Code\Generator\TestAsset\ClassWithInterface'
264-
. ' implements ZendTest\Code\Generator\TestAsset\ThreeInterface';
263+
. ' extends ClassWithInterface'
264+
. ' implements ThreeInterface';
265265
$this->assertContains($expectedClassDef, $code);
266266
}
267267

@@ -1085,4 +1085,41 @@ final class SomeClass
10851085
$output = $classGenerator->generate();
10861086
$this->assertEquals($expectedOutput, $output, $output);
10871087
}
1088+
1089+
public function testCorrectExtendNames()
1090+
{
1091+
$classGenerator = new ClassGenerator();
1092+
$classGenerator->setName('ClassName');
1093+
$classGenerator->setNamespaceName('SomeNamespace');
1094+
$classGenerator->addUse('Zend\Code\NameInformation');
1095+
$classGenerator->setExtendedClass('Zend\Code\NameInformation');
1096+
$this->assertContains('class ClassName extends NameInformation', $classGenerator->generate());
1097+
1098+
$classGenerator = new ClassGenerator();
1099+
$classGenerator->setName('ClassName');
1100+
$classGenerator->setNamespaceName('SomeNamespace');
1101+
$classGenerator->setExtendedClass('DateTime');
1102+
$this->assertContains('class ClassName extends \DateTime', $classGenerator->generate());
1103+
1104+
$classGenerator = new ClassGenerator();
1105+
$classGenerator->setName('ClassName');
1106+
$classGenerator->setExtendedClass('DateTime');
1107+
$this->assertContains('class ClassName extends DateTime', $classGenerator->generate());
1108+
}
1109+
1110+
public function testCorrectImplementNames()
1111+
{
1112+
$classGenerator = new ClassGenerator();
1113+
$classGenerator->setName('ClassName');
1114+
$classGenerator->setNamespaceName('SomeNamespace');
1115+
$classGenerator->addUse('Zend\Code\Generator\GeneratorInterface');
1116+
$classGenerator->setImplementedInterfaces([
1117+
'SomeNamespace\ClassInterface',
1118+
'Zend\Code\Generator\GeneratorInterface',
1119+
'Iteratable'
1120+
]);
1121+
1122+
$expected = 'class ClassName implements ClassInterface, GeneratorInterface, \Iteratable';
1123+
$this->assertContains($expected, $classGenerator->generate());
1124+
}
10881125
}

0 commit comments

Comments
 (0)