Skip to content

Commit e04c98d

Browse files
authored
Fix issue where 'shareInstances' resolve but fail to propagate (#201)
* #200 - Expand shareInstances test to replicate propagation issue * #200 - Fix issue where 'shareInstances' resolve but fail to propagate Because `$share` is passed by reference to `matchParam()`, and `matchParam()` removes matching objects from its `$search` array, instances may be removed from `$share` before it is passed to `expand()` or `create()`. Depending on the order of constructor parameters and the relative placement of `shareInstances` dependencies in the object tree, this may result in multiple instances of these dependencies being created. Fixed by passing a copy of the `$share` array to `matchParam()`.
1 parent 3249d52 commit e04c98d

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

Dice.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ private function getParams(\ReflectionMethod $method, array $rule) {
235235
$parameters[] = $match;
236236
}
237237
// Do the same with $share
238-
else if ($share && ($match = $this->matchParam($param, $class, $share)) !== false) {
238+
else if (($copy = $share) && ($match = $this->matchParam($param, $class, $copy)) !== false) {
239239
$parameters[] = $match;
240240
}
241241
// When nothing from $args or $share matches but a class is type hinted, create an instance to use, using a substitution if set

tests/ShareInstancesTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ public function testShareInstances() {
1818
$this->assertInstanceOf('SharedInstanceTest1', $shareTest->share1);
1919
$this->assertInstanceOf('SharedInstanceTest2', $shareTest->share2);
2020

21+
$this->assertSame($shareTest->shared, $shareTest->share1->shared);
2122
$this->assertSame($shareTest->share1->shared, $shareTest->share2->shared);
23+
$this->assertEquals($shareTest->shared->uniq, $shareTest->share1->shared->uniq);
2224
$this->assertEquals($shareTest->share1->shared->uniq, $shareTest->share2->shared->uniq);
2325

2426
}
@@ -41,7 +43,9 @@ public function testNamedShareInstances() {
4143
$this->assertInstanceOf('SharedInstanceTest1', $shareTest->share1);
4244
$this->assertInstanceOf('SharedInstanceTest2', $shareTest->share2);
4345

46+
$this->assertSame($shareTest->shared, $shareTest->share1->shared);
4447
$this->assertSame($shareTest->share1->shared, $shareTest->share2->shared);
48+
$this->assertEquals($shareTest->shared->uniq, $shareTest->share1->shared->uniq);
4549
$this->assertEquals($shareTest->share1->shared->uniq, $shareTest->share2->shared->uniq);
4650

4751

@@ -72,15 +76,21 @@ public function testShareInstancesMultiple() {
7276
$this->assertInstanceOf('SharedInstanceTest1', $shareTest->share1);
7377
$this->assertInstanceOf('SharedInstanceTest2', $shareTest->share2);
7478

79+
$this->assertSame($shareTest->shared, $shareTest->share1->shared);
7580
$this->assertSame($shareTest->share1->shared, $shareTest->share2->shared);
81+
$this->assertEquals($shareTest->shared->uniq, $shareTest->share1->shared->uniq);
7682
$this->assertEquals($shareTest->share1->shared->uniq, $shareTest->share2->shared->uniq);
7783

7884

7985
$shareTest2 = $dice->create('TestSharedInstancesTop');
86+
$this->assertSame($shareTest2->shared, $shareTest2->share1->shared);
8087
$this->assertSame($shareTest2->share1->shared, $shareTest2->share2->shared);
88+
$this->assertEquals($shareTest2->shared->uniq, $shareTest2->share1->shared->uniq);
8189
$this->assertEquals($shareTest2->share1->shared->uniq, $shareTest2->share2->shared->uniq);
8290

91+
$this->assertNotSame($shareTest->shared, $shareTest2->shared);
8392
$this->assertNotSame($shareTest->share1->shared, $shareTest2->share2->shared);
93+
$this->assertNotEquals($shareTest->shared->uniq, $shareTest2->shared->uniq);
8494
$this->assertNotEquals($shareTest->share1->shared->uniq, $shareTest2->share2->shared->uniq);
8595

8696
}

tests/TestData/ShareInstances.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
* @license http:// www.opensource.org/licenses/bsd-license.php BSD License *
66
* @version 3.0 */
77
class TestSharedInstancesTop {
8+
public $shared;
89
public $share1;
910
public $share2;
1011

11-
public function __construct(SharedInstanceTest1 $share1, SharedInstanceTest2 $share2) {
12+
public function __construct(Shared $shared, SharedInstanceTest1 $share1, SharedInstanceTest2 $share2) {
13+
$this->shared = $shared;
1214
$this->share1 = $share1;
1315
$this->share2 = $share2;
1416
}

0 commit comments

Comments
 (0)