diff --git a/src/model/parts/InterfacesPart.php b/src/model/parts/InterfacesPart.php index 4f1de80..b5fe09a 100644 --- a/src/model/parts/InterfacesPart.php +++ b/src/model/parts/InterfacesPart.php @@ -15,7 +15,7 @@ trait InterfacesPart { /** @var Set */ private $interfaces; - + private function initInterfaces() { $this->interfaces = new Set(); } @@ -97,11 +97,11 @@ public function hasInterfaces() { */ public function hasInterface($interface) { if ($interface instanceof PhpInterface) { - return $this->interfaces->contains($interface->getName()) + return $this->interfaces->contains($interface->getName()) || $this->interfaces->contains($interface->getQualifiedName()); } - return $this->hasInterface(new PhpInterface($interface)); + return $this->interfaces->contains($interface) || $this->hasInterface(new PhpInterface($interface)); } /** diff --git a/src/parser/visitor/ClassParserVisitor.php b/src/parser/visitor/ClassParserVisitor.php index b108a44..a2b2f24 100644 --- a/src/parser/visitor/ClassParserVisitor.php +++ b/src/parser/visitor/ClassParserVisitor.php @@ -5,12 +5,12 @@ use PhpParser\Node\Stmt\Class_; class ClassParserVisitor extends StructParserVisitor { - + use StructParserPart; public function visitClass(Class_ $node) { $struct = $this->getStruct(); - + if ($node->extends !== null) { if ($node->extends->getType() === 'Name_FullyQualified') { $struct->setParentClassName('\\' . implode('\\', $node->extends->parts)); @@ -19,8 +19,12 @@ public function visitClass(Class_ $node) { } } - foreach ($node->implements as $name) { - $struct->addInterface(implode('\\', $name->parts)); + foreach ($node->implements as $interface) { + if ($interface->getType() === 'Name_FullyQualified') { + $struct->addInterface('\\' . implode('\\', $interface->parts)); + } else { + $struct->addInterface(implode('\\', $interface->parts)); + } } $struct->setAbstract($node->isAbstract()); diff --git a/tests/fixtures/MyCollection.php b/tests/fixtures/MyCollection.php index 520bb2c..c648ddd 100644 --- a/tests/fixtures/MyCollection.php +++ b/tests/fixtures/MyCollection.php @@ -1,4 +1,3 @@ assertEquals($expected, $code); } - + public function testAbstract() { $expected = 'abstract class MyClass {' . "\n" . '}'; - + $class = PhpClass::create('MyClass')->setAbstract(true); $generator = new ModelGenerator(); $code = $generator->generate($class); - + $this->assertEquals($expected, $code); } - + public function testFinal() { $expected = 'final class MyClass {' . "\n" . '}'; - + $class = PhpClass::create('MyClass')->setFinal(true); $generator = new ModelGenerator(); $code = $generator->generate($class); - + $this->assertEquals($expected, $code); } - + public function testInterfaces() { $generator = new ModelGenerator(); - + $expected = 'class MyClass implements \Iterator {' . "\n" . '}'; $class = PhpClass::create('MyClass')->addInterface('\Iterator'); $this->assertEquals($expected, $generator->generate($class)); - + $expected = 'class MyClass implements \Iterator, \ArrayAccess {' . "\n" . '}'; $class = PhpClass::create('MyClass')->addInterface('\Iterator')->addInterface('\ArrayAccess'); $this->assertEquals($expected, $generator->generate($class)); @@ -59,51 +59,51 @@ public function testInterfaces() { public function testParent() { $expected = 'class MyClass extends MyParent {' . "\n" . '}'; - + $class = PhpClass::create('MyClass')->setParentClassName('MyParent'); $generator = new ModelGenerator(); $code = $generator->generate($class); - + $this->assertEquals($expected, $code); } public function testUseStatements() { $class = new PhpClass('Foo\\Bar'); $class->addUseStatement('Bam\\Baz'); - + $codegen = new CodeFileGenerator(['generateDocblock' => false, 'generateEmptyDocblock' => false]); $code = $codegen->generate($class); - + $this->assertEquals($this->getGeneratedContent('FooBar.php'), $code); - + $class = new PhpClass('Foo\\Bar'); $class->addUseStatement('Bam\\Baz', 'BamBaz'); - + $codegen = new CodeFileGenerator(['generateDocblock' => false, 'generateEmptyDocblock' => false]); $code = $codegen->generate($class); - + $this->assertEquals($this->getGeneratedContent('FooBarWithAlias.php'), $code); - + $class = new PhpClass('Foo'); $class->addUseStatement('Bar'); - + $generator = new ModelGenerator(); $code = $generator->generate($class); $expected = 'class Foo {' . "\n" . '}'; - + $this->assertEquals($expected, $code); } public function testABClass() { $class = Fixtures::createABClass(); - + $modelGenerator = new ModelGenerator(); $modelCode = $modelGenerator->generate($class); $this->assertEquals($this->getGeneratedContent('ABClass.php'), $modelCode); $generator = new CodeGenerator(['generateDocblock' => false]); $code = $generator->generate($class); $this->assertEquals($modelCode, $code); - + $modelGenerator = new ModelGenerator(['generateDocblock' => true]); $modelCode = $modelGenerator->generate($class); $this->assertEquals($this->getGeneratedContent('ABClassWithComments.php'), $modelCode); @@ -111,16 +111,34 @@ public function testABClass() { $code = $generator->generate($class); $this->assertEquals($modelCode, $code); } - + public function testRequireTraitsClass() { $class = PhpClass::create('RequireTraitsClass') ->addRequiredFile('FooBar.php') ->addRequiredFile('ABClass.php') ->addTrait('Iterator'); - + $generator = new ModelGenerator(); $code = $generator->generate($class); $this->assertEquals($this->getGeneratedContent('RequireTraitsClass.php'), $code); } + public function testMyCollection() { + $class = PhpClass::fromFile(__DIR__ . '/../fixtures/MyCollection.php'); + + $generator = new CodeFileGenerator(['generateDocblock' => false]); + $code = $generator->generate($class); + + $this->assertEquals($this->getFixtureContent('MyCollection.php'), $code); + } + + public function testMyCollection2() { + $class = PhpClass::fromFile(__DIR__ . '/../fixtures/MyCollection2.php'); + + $generator = new CodeFileGenerator(['generateDocblock' => false]); + $code = $generator->generate($class); + + $this->assertEquals($this->getFixtureContent('MyCollection2.php'), $code); + } + } diff --git a/tests/model/ClassTest.php b/tests/model/ClassTest.php index f7b3456..1af2443 100644 --- a/tests/model/ClassTest.php +++ b/tests/model/ClassTest.php @@ -133,6 +133,10 @@ public function testInterfaces() { $this->assertTrue($class->hasUseStatement('other\name\space\Interface')); $this->assertSame($class, $class->removeInterface('other\name\space\Interface')); $this->assertTrue($class->hasUseStatement('other\name\space\Interface')); + + $class->addInterface('\my\Interface'); + $this->assertTrue($class->hasInterface('\my\Interface')); + $this->assertFalse($class->hasInterface('my\Interface')); } public function testTraits() { diff --git a/tests/parser/ClassParserTest.php b/tests/parser/ClassParserTest.php index 92c097c..acbc168 100644 --- a/tests/parser/ClassParserTest.php +++ b/tests/parser/ClassParserTest.php @@ -10,7 +10,7 @@ * @group parser */ class ClassParserTest extends \PHPUnit_Framework_TestCase { - + use ModelAssertions; use ValueTests; @@ -47,7 +47,7 @@ public function testClassWithConstants() { $this->assertTrue($class->hasConstant('NMBR')); $this->assertEquals(300, $class->getConstant('NMBR')->getValue()); - + $this->assertTrue($class->hasConstant('BAR')); $this->assertEquals('self::FOO', $class->getConstant('BAR')->getExpression()); } @@ -67,10 +67,18 @@ public function testClassWithValues() { $class = PhpClass::fromFile(__DIR__ . '/../fixtures/ClassWithValues.php'); $this->assertClassWithValues($class); } - + + public function testTypeClass() { + $class = PhpClass::fromFile(__DIR__ . '/../fixtures/TypeClass.php'); + + $doSomething = $class->getMethod('doSomething'); + $options = $doSomething->getParameter('options'); + $this->assertEquals('Symfony\Component\OptionsResolver\OptionsResolver', $options->getType()); + } + public function testMyCollection() { $class = PhpClass::fromFile(__DIR__ . '/../fixtures/MyCollection.php'); - + $this->assertEquals('phootwork\collection\AbstractCollection', $class->getParentClassName()); $this->assertTrue($class->hasInterface('phootwork\collection\Collection')); } @@ -81,13 +89,5 @@ public function testMyCollection2() { $this->assertEquals('\phootwork\collection\AbstractCollection', $class->getParentClassName()); $this->assertTrue($class->hasInterface('\phootwork\collection\Collection')); } - - public function testTypeClass() { - $class = PhpClass::fromFile(__DIR__ . '/../fixtures/TypeClass.php'); - - $doSomething = $class->getMethod('doSomething'); - $options = $doSomething->getParameter('options'); - $this->assertEquals('Symfony\Component\OptionsResolver\OptionsResolver', $options->getType()); - } - + }