Skip to content

Commit 0757e5f

Browse files
authored
Merge pull request #2 from rstgroup/handle-multiuse
Handle multiuse
2 parents 0d65a52 + 88b345f commit 0757e5f

File tree

4 files changed

+31
-12
lines changed

4 files changed

+31
-12
lines changed

src/Builder/Reflection.php

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,8 @@ private function buildParameter(ReflectionParameter $parameter, $data, Reflectio
110110

111111
$parsedFile = $parser->parse(file_get_contents($constructor->getDeclaringClass()->getFileName()));
112112
$namespace = $this->getNamespaceStmt($parsedFile);
113-
$use = $this->getUseStmt($namespace);
114-
$namespaces = $this->getUsesNamespaces($use);
113+
$uses = $this->getUseStmts($namespace);
114+
$namespaces = $this->getUsesNamespaces($uses);
115115

116116
foreach($data as $objectConstructorData) {
117117
$list[] = $this->build(
@@ -141,23 +141,28 @@ private function getNamespaceStmt(array $nodes): Stmt\Namespace_
141141
return new Stmt\Namespace_();
142142
}
143143

144-
private function getUseStmt(Stmt\Namespace_ $node): Stmt\Use_
144+
/** @return Stmt\Use_[] */
145+
private function getUseStmts(Stmt\Namespace_ $node): array
145146
{
147+
$uses = [];
146148
foreach ($node->stmts as $node) {
147149
if ($node instanceof Stmt\Use_) {
148-
return $node;
150+
$uses[]= $node;
149151
}
150152
}
151153

152-
return new Stmt\Use_([]);
154+
return $uses;
153155
}
154156

155-
/** @return string[] */
156-
private function getUsesNamespaces(Stmt\Use_ $node): array
157+
/**
158+
* @param Stmt\Use_[] $uses
159+
* @return string[]
160+
*/
161+
private function getUsesNamespaces(array $uses): array
157162
{
158163
$names = [];
159-
foreach ($node->uses as $use) {
160-
$names[] = $use->name->toString();
164+
foreach ($uses as $use) {
165+
$names[] = $use->uses[0]->name->toString();
161166
}
162167

163168
return $names;

test/ListOfObjectsWithUseStmtConstructor.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,19 @@
33
namespace RstGroup\ObjectBuilder\Test;
44

55
use RstGroup\ObjectBuilder\Test\Object\SomeObject;
6+
use RstGroup\ObjectBuilder\Test\Object\SomeSecondObject;
67

78
class ListOfObjectsWithUseStmtConstructor
89
{
910
public $list;
11+
public $object;
1012

1113
/**
12-
* @param SomeObject[] $list
14+
* @param SomeSecondObject[] $list
1315
*/
1416
public function __construct(array $list)
1517
{
1618
$this->list = $list;
19+
$this->object = new SomeObject();
1720
}
1821
}

test/Object/SomeSecondObject.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace RstGroup\ObjectBuilder\Test\Object;
4+
5+
class SomeSecondObject
6+
{
7+
public function __construct()
8+
{
9+
}
10+
}

test/unit/Builder/ReflectionTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@
66
use RstGroup\ObjectBuilder\Builder\ParameterNameStrategy\Simple;
77
use RstGroup\ObjectBuilder\Builder\Reflection;
88
use RstGroup\ObjectBuilder\Test\ListOfObjectsWithoutUseButWithFQNTypedArrayConstructor;
9+
use RstGroup\ObjectBuilder\Test\ListOfObjectsWithoutUseStmtConstructor;
910
use RstGroup\ObjectBuilder\Test\ListOfObjectsWithUseStmtConstructor;
1011
use RstGroup\ObjectBuilder\Test\Object\SomeObject;
12+
use RstGroup\ObjectBuilder\Test\Object\SomeSecondObject;
1113
use RstGroup\ObjectBuilder\Test\SimpleMixedConstructor;
1214
use RstGroup\ObjectBuilder\Test\SimpleMixedConstructorWithDefaultValue;
1315
use RstGroup\ObjectBuilder\Test\SimpleScalarConstructor;
1416
use RstGroup\ObjectBuilder\Test\SomeAggregateRoot;
15-
use RstGroup\ObjectBuilder\Test\ListOfObjectsWithoutUseStmtConstructor;
1617
use RstGroup\ObjectBuilder\Test\SomeObjectWithEmptyConstructor;
1718

1819
class ReflectionTest extends TestCase
@@ -122,7 +123,7 @@ public function iCanBuildObjectWithObjectCollectionWithUseInConstructor()
122123
$this->assertInstanceOf(ListOfObjectsWithUseStmtConstructor::class, $object);
123124
$this->assertCount(2, $object->list);
124125
foreach($object->list as $element) {
125-
$this->assertInstanceOf(SomeObject::class, $element);
126+
$this->assertInstanceOf(SomeSecondObject::class, $element);
126127
}
127128
}
128129

0 commit comments

Comments
 (0)