From 88b345f2d3e21daa816d716be2529bb6c4b8fbd0 Mon Sep 17 00:00:00 2001 From: Krzysztof Gardziejewski Date: Wed, 18 Jul 2018 15:52:53 +0200 Subject: [PATCH] handle multiuse --- src/Builder/Reflection.php | 23 ++++++++++++-------- test/ListOfObjectsWithUseStmtConstructor.php | 5 ++++- test/Object/SomeSecondObject.php | 10 +++++++++ test/unit/Builder/ReflectionTest.php | 5 +++-- 4 files changed, 31 insertions(+), 12 deletions(-) create mode 100644 test/Object/SomeSecondObject.php diff --git a/src/Builder/Reflection.php b/src/Builder/Reflection.php index 44b60ff..8895066 100644 --- a/src/Builder/Reflection.php +++ b/src/Builder/Reflection.php @@ -110,8 +110,8 @@ private function buildParameter(ReflectionParameter $parameter, $data, Reflectio $parsedFile = $parser->parse(file_get_contents($constructor->getDeclaringClass()->getFileName())); $namespace = $this->getNamespaceStmt($parsedFile); - $use = $this->getUseStmt($namespace); - $namespaces = $this->getUsesNamespaces($use); + $uses = $this->getUseStmts($namespace); + $namespaces = $this->getUsesNamespaces($uses); foreach($data as $objectConstructorData) { $list[] = $this->build( @@ -141,23 +141,28 @@ private function getNamespaceStmt(array $nodes): Stmt\Namespace_ return new Stmt\Namespace_(); } - private function getUseStmt(Stmt\Namespace_ $node): Stmt\Use_ + /** @return Stmt\Use_[] */ + private function getUseStmts(Stmt\Namespace_ $node): array { + $uses = []; foreach ($node->stmts as $node) { if ($node instanceof Stmt\Use_) { - return $node; + $uses[]= $node; } } - return new Stmt\Use_([]); + return $uses; } - /** @return string[] */ - private function getUsesNamespaces(Stmt\Use_ $node): array + /** + * @param Stmt\Use_[] $uses + * @return string[] + */ + private function getUsesNamespaces(array $uses): array { $names = []; - foreach ($node->uses as $use) { - $names[] = $use->name->toString(); + foreach ($uses as $use) { + $names[] = $use->uses[0]->name->toString(); } return $names; diff --git a/test/ListOfObjectsWithUseStmtConstructor.php b/test/ListOfObjectsWithUseStmtConstructor.php index 946c842..7fac801 100644 --- a/test/ListOfObjectsWithUseStmtConstructor.php +++ b/test/ListOfObjectsWithUseStmtConstructor.php @@ -3,16 +3,19 @@ namespace RstGroup\ObjectBuilder\Test; use RstGroup\ObjectBuilder\Test\Object\SomeObject; +use RstGroup\ObjectBuilder\Test\Object\SomeSecondObject; class ListOfObjectsWithUseStmtConstructor { public $list; + public $object; /** - * @param SomeObject[] $list + * @param SomeSecondObject[] $list */ public function __construct(array $list) { $this->list = $list; + $this->object = new SomeObject(); } } diff --git a/test/Object/SomeSecondObject.php b/test/Object/SomeSecondObject.php new file mode 100644 index 0000000..b972c95 --- /dev/null +++ b/test/Object/SomeSecondObject.php @@ -0,0 +1,10 @@ +assertInstanceOf(ListOfObjectsWithUseStmtConstructor::class, $object); $this->assertCount(2, $object->list); foreach($object->list as $element) { - $this->assertInstanceOf(SomeObject::class, $element); + $this->assertInstanceOf(SomeSecondObject::class, $element); } }