Skip to content

Commit

Permalink
Avoid nullable Return for Id in Document
Browse files Browse the repository at this point in the history
Signed-off-by: Arne Blankerts <Arne@Blankerts.de>
  • Loading branch information
theseer committed Sep 18, 2023
1 parent ebd7c59 commit 77495cf
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions src/document/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,15 @@ private function __construct(
) {
}

public function id(): ?Id {
/** @psalm-assert-if-true Id $this->id */
public function hasId(): bool {
return $this->id !== null;
}
public function id(): Id {
if (!$this->hasId()) {
throw new DocumentException('No ID set');
}

return $this->id;
}

Expand Down Expand Up @@ -102,28 +110,28 @@ public function merge(self|DocumentCollection ...$toMerge): self {

foreach ($toMerge as $item) {
if ($item instanceof self) {
$id = $item->id();

if ($id === null) {
if (!$item->hasId()) {
throw new DocumentException('Document must have an ID to be merged.');
}

$mergeList->add(
$id,
$item->id,
$item->dom
);

continue;
}

foreach ($item as $single) {
$id = $single->id();
assert($single instanceof self);

if ($id === null) {
if (!$single->hasId()) {
throw new DocumentException('All documents in collection must have an ID to be merged.');
}

$mergeList->add(
$id,
$single->id,
$single->dom
);
}
Expand Down

0 comments on commit 77495cf

Please sign in to comment.