Skip to content

Commit

Permalink
handle multiuse
Browse files Browse the repository at this point in the history
  • Loading branch information
Krzysztof Gardziejewski committed Jul 18, 2018
1 parent 0d65a52 commit 88b345f
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 12 deletions.
23 changes: 14 additions & 9 deletions src/Builder/Reflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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;
Expand Down
5 changes: 4 additions & 1 deletion test/ListOfObjectsWithUseStmtConstructor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
10 changes: 10 additions & 0 deletions test/Object/SomeSecondObject.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php declare(strict_types=1);

namespace RstGroup\ObjectBuilder\Test\Object;

class SomeSecondObject
{
public function __construct()
{
}
}
5 changes: 3 additions & 2 deletions test/unit/Builder/ReflectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@
use RstGroup\ObjectBuilder\Builder\ParameterNameStrategy\Simple;
use RstGroup\ObjectBuilder\Builder\Reflection;
use RstGroup\ObjectBuilder\Test\ListOfObjectsWithoutUseButWithFQNTypedArrayConstructor;
use RstGroup\ObjectBuilder\Test\ListOfObjectsWithoutUseStmtConstructor;
use RstGroup\ObjectBuilder\Test\ListOfObjectsWithUseStmtConstructor;
use RstGroup\ObjectBuilder\Test\Object\SomeObject;
use RstGroup\ObjectBuilder\Test\Object\SomeSecondObject;
use RstGroup\ObjectBuilder\Test\SimpleMixedConstructor;
use RstGroup\ObjectBuilder\Test\SimpleMixedConstructorWithDefaultValue;
use RstGroup\ObjectBuilder\Test\SimpleScalarConstructor;
use RstGroup\ObjectBuilder\Test\SomeAggregateRoot;
use RstGroup\ObjectBuilder\Test\ListOfObjectsWithoutUseStmtConstructor;
use RstGroup\ObjectBuilder\Test\SomeObjectWithEmptyConstructor;

class ReflectionTest extends TestCase
Expand Down Expand Up @@ -122,7 +123,7 @@ public function iCanBuildObjectWithObjectCollectionWithUseInConstructor()
$this->assertInstanceOf(ListOfObjectsWithUseStmtConstructor::class, $object);
$this->assertCount(2, $object->list);
foreach($object->list as $element) {
$this->assertInstanceOf(SomeObject::class, $element);
$this->assertInstanceOf(SomeSecondObject::class, $element);
}
}

Expand Down

0 comments on commit 88b345f

Please sign in to comment.