Skip to content

Commit 71b0c9e

Browse files
committed
fix: ensure collections are initialized before manipulation in Anomaly and IonStorm classes
1 parent 66e4235 commit 71b0c9e

File tree

6 files changed

+18
-14
lines changed

6 files changed

+18
-14
lines changed

src/Component/Anomaly/Type/IonStorm/IonStormPropagation.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,13 @@ public function __construct(
2121

2222
public function propagateStorm(Anomaly $root, LocationPool $locationPool): void
2323
{
24-
if ($root->getChildren()->isEmpty()) {
24+
$children = $root->getChildren()->toArray();
25+
if ($children === []) {
2526
$root->setRemainingTicks(0);
2627
return;
2728
}
2829

29-
foreach ($root->getChildren()->toArray() as $child) {
30+
foreach ($children as $child) {
3031
$this->transferRemainingTicks(
3132
$root,
3233
$child,

src/Component/Anomaly/Type/IonStorm/LocationPoolFactory.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ public function createLocationPool(Anomaly $anomaly, int $stretch): LocationPool
2626
private function getLocations(Anomaly $anomaly, int $stretch): array
2727
{
2828
$panelBoundaries = null;
29-
foreach ($anomaly->getChildren() as $child) {
29+
$children = $anomaly->getChildren()->toArray();
30+
foreach ($children as $child) {
3031

3132
$location = $child->getLocation();
3233
if ($location === null) {

src/Orm/Entity/Anomaly.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,12 @@ public function setLocation(?Location $location): Anomaly
117117
$key = $this->anomalyType->getId();
118118

119119
if ($old !== null) {
120+
$old->getAnomalies()->toArray(); // make sure the collection is initialized
120121
$old->getAnomalies()->remove($key);
121122
}
122123

123124
if ($location !== null && !$location->getAnomalies()->containsKey($key)) {
125+
$location->getAnomalies()->toArray(); // make sure the collection is initialized
124126
$location->getAnomalies()->set($key, $this);
125127
}
126128

@@ -132,13 +134,14 @@ public function getParent(): ?Anomaly
132134
return $this->parent;
133135
}
134136

135-
public function setParent(Anomaly $parent, Location $location): Anomaly
137+
public function setParent(Anomaly $parent, Location $location): Anomaly //check
136138
{
137139
if ($this->parent === $parent) {
138140
return $this;
139141
}
140142

141143
$this->parent = $parent;
144+
$parent->getChildren()->toArray(); // make sure the collection is initialized
142145
$parent->getChildren()->set($location->getId(), $this);
143146

144147
return $this;
@@ -170,7 +173,7 @@ public function getChildren(): Collection
170173

171174
public function hasChildren(): bool
172175
{
173-
return !$this->getChildren()->isEmpty();
176+
return $this->getChildren()->toArray() !== [];
174177
}
175178

176179
#[\Override]

src/Orm/Repository/AnomalyRepository.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,13 @@ public function delete(Anomaly $anomaly): void
3636
{
3737
$location = $anomaly->getLocation();
3838
if ($location !== null) {
39+
$location->getAnomalies()->toArray(); // make sure the collection is initialized
3940
$location->getAnomalies()->removeElement($anomaly);
4041
}
4142

4243
$parent = $anomaly->getParent();
4344
if ($parent !== null) {
45+
$parent->getChildren()->toArray(); // make sure the collection is initialized
4446
$parent->getChildren()->removeElement($anomaly);
4547
}
4648

tests/unit/Component/Anomaly/Type/IonStorm/IonStormPropagationTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,6 @@ public function testPropagateStorm(): void
9797

9898
$root->shouldReceive('getChildren')
9999
->withNoArgs()
100-
->twice()
101100
->andReturn(new ArrayCollection([
102101
$child1,
103102
$child2WithIonStormOnNeighbour,

tests/unit/Orm/Entity/AnomalyTest.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,16 @@ public function testSetLocation(): void
3838

3939
public function testSetParent(): void
4040
{
41-
$parent = $this->createMock(Anomaly::class);
42-
$location = $this->createMock(Location::class);
41+
$parent = $this->mock(Anomaly::class);
42+
$location = $this->mock(Location::class);
4343

4444
$anomaly = new Anomaly();
4545

46-
$parent->expects($this->once())
47-
->method('getChildren')
48-
->willReturn(new \Doctrine\Common\Collections\ArrayCollection());
46+
$parent->shouldReceive('getChildren')
47+
->andReturn(new \Doctrine\Common\Collections\ArrayCollection());
4948

50-
$location->expects($this->once())
51-
->method('getId')
52-
->willReturn(42);
49+
$location->shouldReceive('getId')
50+
->andReturn(42);
5351

5452
$anomaly->setParent($parent, $location);
5553

0 commit comments

Comments
 (0)