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

Commit

Permalink
Make sure that valid classnames are being used in the implements and …
Browse files Browse the repository at this point in the history
…extends section of a class.
  • Loading branch information
veewee committed Jun 25, 2016
1 parent ca15548 commit 4a6f4ab
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 5 deletions.
22 changes: 21 additions & 1 deletion src/Generator/ClassGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -950,12 +950,13 @@ public function generate()
$output .= static::OBJECT_TYPE . ' ' . $this->getName();

if (!empty($this->extendedClass)) {
$output .= ' extends ' . $this->extendedClass;
$output .= ' extends ' . $this->generateShortOrCompleteClassname($this->extendedClass);
}

$implemented = $this->getImplementedInterfaces();

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

Expand Down Expand Up @@ -1009,4 +1010,23 @@ private function validateConstantValue($value)
gettype($value)
));
}

/**
* @param string $fqnClassName
*
* @return string
*/
private function generateShortOrCompleteClassname($fqnClassName)
{
$parts = explode('\\', $fqnClassName);
$className = array_pop($parts);
$classNamespace = implode('\\', $parts);
$currentNamespace = (string) $this->getNamespaceName();

if ($classNamespace === $currentNamespace || in_array($fqnClassName, $this->getUses())) {
return $className;
}

return '\\' . $fqnClassName;
}
}
45 changes: 41 additions & 4 deletions test/Generator/ClassGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,8 +242,8 @@ public function testClassFromReflectionThatImplementsInterfaces()
$code = $classGenerator->generate();

$expectedClassDef = 'class ClassWithInterface'
. ' implements ZendTest\Code\Generator\TestAsset\OneInterface'
. ', ZendTest\Code\Generator\TestAsset\TwoInterface';
. ' implements OneInterface'
. ', TwoInterface';
$this->assertContains($expectedClassDef, $code);
}

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

$expectedClassDef = 'class NewClassWithInterface'
. ' extends ZendTest\Code\Generator\TestAsset\ClassWithInterface'
. ' implements ZendTest\Code\Generator\TestAsset\ThreeInterface';
. ' extends ClassWithInterface'
. ' implements ThreeInterface';
$this->assertContains($expectedClassDef, $code);
}

Expand Down Expand Up @@ -1085,4 +1085,41 @@ final class SomeClass
$output = $classGenerator->generate();
$this->assertEquals($expectedOutput, $output, $output);
}

public function testCorrectExtendNames()
{
$classGenerator = new ClassGenerator();
$classGenerator->setName('ClassName');
$classGenerator->setNamespaceName('SomeNamespace');
$classGenerator->addUse('Zend\Code\NameInformation');
$classGenerator->setExtendedClass('Zend\Code\NameInformation');
$this->assertContains('class ClassName extends NameInformation', $classGenerator->generate());

$classGenerator = new ClassGenerator();
$classGenerator->setName('ClassName');
$classGenerator->setNamespaceName('SomeNamespace');
$classGenerator->setExtendedClass('DateTime');
$this->assertContains('class ClassName extends \DateTime', $classGenerator->generate());

$classGenerator = new ClassGenerator();
$classGenerator->setName('ClassName');
$classGenerator->setExtendedClass('DateTime');
$this->assertContains('class ClassName extends DateTime', $classGenerator->generate());
}

public function testCorrectImplementNames()
{
$classGenerator = new ClassGenerator();
$classGenerator->setName('ClassName');
$classGenerator->setNamespaceName('SomeNamespace');
$classGenerator->addUse('Zend\Code\Generator\GeneratorInterface');
$classGenerator->setImplementedInterfaces([
'SomeNamespace\ClassInterface',
'Zend\Code\Generator\GeneratorInterface',
'Iteratable'
]);

$expected = 'class ClassName implements ClassInterface, GeneratorInterface, \Iteratable';
$this->assertContains($expected, $classGenerator->generate());
}
}

0 comments on commit 4a6f4ab

Please sign in to comment.