Skip to content

Commit

Permalink
Check object type for getAllEntries repositories
Browse files Browse the repository at this point in the history
To achieve maximum type safety we now check each entry of the array
for the correct entity type. We also type hint more explicitely
for psalm fully typed level.
  • Loading branch information
iquito committed Dec 5, 2021
1 parent 97000c3 commit dd4a36e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
6 changes: 1 addition & 5 deletions src/Builder/SelectEntries.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,7 @@ public function blocking(bool $active = true): self
/**
* Execute SELECT query and return a list of objects that matched it
*
* Returns object[] (from the entity class), we avoid the return type hint
* here so code analyzers don't get confused by generated repositories
* and their different type hint
* @return object[]
*/
public function getAllEntries(): array
{
Expand All @@ -128,8 +126,6 @@ public function getAllEntries(): array

/**
* Execute SELECT query and return exactly one entry, if one was found at all
*
* Returns object (from the entity class) or null if no entry was found
*/
public function getOneEntry(): ?object
{
Expand Down
11 changes: 10 additions & 1 deletion src/Generate/RepositoriesGenerateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,16 @@ public function blocking(bool $active = true): self
*/
public function getAllEntries(): array
{
return $this->selectImplementation->getAllEntries();
/** @var \{namespaceOfEntity}\{classOfEntity}[] $entries */
$entries = $this->selectImplementation->getAllEntries();
foreach ($entries as $entry) {
if (!($entry instanceof \{namespaceOfEntity}\{classOfEntity})) {
throw new \LogicException('Unexpected type encountered - wrong repository might be configured: ' . \get_class($entry));
}
}
return $entries;
}
public function getOneEntry(): ?\{namespaceOfEntity}\{classOfEntity}
Expand Down

0 comments on commit dd4a36e

Please sign in to comment.