Skip to content

Commit 422b030

Browse files
angyvolinpetr-buchin
authored andcommitted
refactor not looping for loop
1 parent 943a069 commit 422b030

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

src/Write/Result/InsertOneResult.php

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,17 @@ class InsertOneResult
1515
*/
1616
public function getInsertedId()
1717
{
18-
foreach ($this->writeResult->getInsertedIds() as $id) {
19-
return $id;
18+
$insertedIds = $this->writeResult->getInsertedIds();
19+
20+
if (0 === count($insertedIds)) {
21+
throw new UnexpectedResultException(
22+
sprintf(
23+
'%s::getInsertedIds() returned empty array, though there was an insert operation.',
24+
WriteResult::class
25+
)
26+
);
2027
}
2128

22-
throw new UnexpectedResultException(
23-
sprintf(
24-
'%s::getInsertedIds() returned empty array, though there was an insert operation.',
25-
WriteResult::class
26-
)
27-
);
29+
return reset($insertedIds);
2830
}
29-
}
31+
}

0 commit comments

Comments
 (0)