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

Commit 2de9009

Browse files
committed
Merge branch 'hotfix/144-generator-aliases'
Close #144
2 parents 04a2881 + 9642410 commit 2de9009

File tree

4 files changed

+95
-0
lines changed

4 files changed

+95
-0
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ All notable changes to this project will be documented in this file, in reverse
2222

2323
### Fixed
2424

25+
- [#144](https://github.com/zendframework/zend-code/pull/144) fixes the class generator such that it now resolves
26+
`setExtendedClass()` arguments to aliases provided to the generator.
27+
2528
- [#140](https://github.com/zendframework/zend-code/pull/140) fixes `MethodScanner::setVisibility()` such that it no longer
2629
casts the provided visibility token to lower case; this fix is necessary, as
2730
the method is supposed to expect only the appropriate

src/Generator/ClassGenerator.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1145,6 +1145,20 @@ private function generateShortOrCompleteClassname($fqnClassName)
11451145
$classNamespace = implode('\\', $parts);
11461146
$currentNamespace = (string) $this->getNamespaceName();
11471147

1148+
if ($this->hasUseAlias($fqnClassName)) {
1149+
return $this->traitUsageGenerator->getUseAlias($fqnClassName);
1150+
}
1151+
if ($this->hasUseAlias($classNamespace)) {
1152+
$namespaceAlias = $this->traitUsageGenerator->getUseAlias($classNamespace);
1153+
1154+
return $namespaceAlias . '\\' . $className;
1155+
}
1156+
if ($this->traitUsageGenerator->isUseAlias($fqnClassName)) {
1157+
return $fqnClassName;
1158+
}
1159+
if ($this->traitUsageGenerator->isUseAlias($classNamespace)) {
1160+
return $fqnClassName;
1161+
}
11481162
if ($classNamespace === $currentNamespace || in_array($fqnClassName, $this->getUses())) {
11491163
return $className;
11501164
}

src/Generator/TraitUsageGenerator.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,40 @@ public function hasUseAlias($use)
112112
return false;
113113
}
114114

115+
/**
116+
* Returns the alias of the provided FQCN
117+
*
118+
* @param string $use
119+
* @return string|null
120+
*/
121+
public function getUseAlias(string $use): ?string
122+
{
123+
foreach ($this->uses as $key => $value) {
124+
$parts = explode(' as ', $key);
125+
if ($parts[0] === $use && count($parts) == 2) {
126+
return $parts[1];
127+
}
128+
}
129+
return null;
130+
}
131+
132+
/**
133+
* Returns true if the alias is defined in the use list
134+
*
135+
* @param string $alias
136+
* @return bool
137+
*/
138+
public function isUseAlias(string $alias): bool
139+
{
140+
foreach ($this->uses as $key => $value) {
141+
$parts = explode(' as ', $key);
142+
if (count($parts) === 2 && $parts[1] === $alias) {
143+
return true;
144+
}
145+
}
146+
return false;
147+
}
148+
115149
/**
116150
* @param string $use
117151
* @return TraitUsageGenerator

test/Generator/ClassGeneratorTest.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1228,6 +1228,50 @@ public function testCorrectExtendNamesFromGlobalNamespace()
12281228
self::assertContains('class ClassName extends DateTime', $classGenerator->generate());
12291229
}
12301230

1231+
public function testCorrectlyExtendsProvidedAliasIfUseAliasExists()
1232+
{
1233+
$classGenerator = new ClassGenerator();
1234+
$classGenerator->setName('ClassName');
1235+
$classGenerator->setNamespaceName('SomeNamespace');
1236+
$classGenerator->addUse('Foo\\Bar', 'BarAlias');
1237+
$classGenerator->setExtendedClass('BarAlias');
1238+
$generated = $classGenerator->generate();
1239+
self::assertContains('class ClassName extends BarAlias', $generated);
1240+
}
1241+
1242+
public function testCorrectlyExtendsProvidedNamespaceAliasIfUseAliasExistsForNamespace()
1243+
{
1244+
$classGenerator = new ClassGenerator();
1245+
$classGenerator->setName('ClassName');
1246+
$classGenerator->setNamespaceName('SomeNamespace');
1247+
$classGenerator->addUse('Foo\\Bar', 'BarAlias');
1248+
$classGenerator->setExtendedClass('BarAlias\\FooBar');
1249+
$generated = $classGenerator->generate();
1250+
self::assertContains('class ClassName extends BarAlias\\FooBar', $generated);
1251+
}
1252+
1253+
public function testCorrectlyExtendsAliasOfProvidedFQCNIfUseAliasExists()
1254+
{
1255+
$classGenerator = new ClassGenerator();
1256+
$classGenerator->setName('ClassName');
1257+
$classGenerator->setNamespaceName('SomeNamespace');
1258+
$classGenerator->addUse('Foo\\Bar', 'BarAlias');
1259+
$classGenerator->setExtendedClass('Foo\\Bar');
1260+
$generated = $classGenerator->generate();
1261+
self::assertContains('class ClassName extends BarAlias', $generated);
1262+
}
1263+
1264+
public function testCorrectlyExtendsWithNamespaceAliasOfProvidedFQCNIfUseAliasExistsForNamespace()
1265+
{
1266+
$classGenerator = new ClassGenerator();
1267+
$classGenerator->setName('ClassName');
1268+
$classGenerator->setNamespaceName('SomeNamespace');
1269+
$classGenerator->addUse('Foo\\Bar', 'BarAlias');
1270+
$classGenerator->setExtendedClass('Foo\\Bar\\FooBar');
1271+
$generated = $classGenerator->generate();
1272+
self::assertContains('class ClassName extends BarAlias\\FooBar', $generated);
1273+
}
1274+
12311275
public function testCorrectImplementNames()
12321276
{
12331277
$classGenerator = new ClassGenerator();

0 commit comments

Comments
 (0)