Skip to content

Commit

Permalink
Add type to collection's execute() method
Browse files Browse the repository at this point in the history
  • Loading branch information
ilijastuden committed Mar 8, 2024
1 parent 2ee0333 commit 8a94aa1
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 20 deletions.
39 changes: 20 additions & 19 deletions src/Collection/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,39 +150,42 @@ public function getTimestampHash(string $timestamp_field): string
*
* @return ResultInterface|EntityInterface[]
*/
public function execute()
public function execute(): ?iterable
{
if (!$this->isReady()) {
throw new LogicException('Collection is not ready');
}

if (is_callable($this->pre_execute_callback)) {
$ids = $this->executeIds();
$ids_count = count($ids);

if ($ids_count = count($ids)) {
call_user_func($this->pre_execute_callback, $ids);
if ($ids_count === 0) {
return null;
}

if ($ids_count > 1000) {
$sql = $this->getSelectSql(); // Don't escape more than 1000 ID-s using DB::escape(), let MySQL do the dirty work instead of PHP
} else {
$escaped_ids = $this->connection->escapeValue($ids);
call_user_func($this->pre_execute_callback, $ids);

$sql = "SELECT * FROM {$this->getTableName()} WHERE id IN ($escaped_ids) ORDER BY FIELD (id, $escaped_ids)";
}
if ($ids_count > 1000) {
$sql = $this->getSelectSql(); // Don't escape more than 1000 ID-s using DB::escape(), let MySQL do the dirty work instead of PHP
} else {
$escaped_ids = $this->connection->escapeValue($ids);

return $this->pool->findBySql($this->getType(), $sql);
$sql = sprintf(
"SELECT * FROM %s WHERE `id` IN (%s) ORDER BY FIELD (`id`, %s)",
$this->getTableName(),
$escaped_ids,
$escaped_ids,
);
}

return null;
return $this->pool->findBySql($this->getType(), $sql);
}

return $this->pool->findBySql($this->getType(), $this->getSelectSql());
}

/**
* @var int[]
*/
private $ids = false;
private ?array $ids = null;

/**
* Return ID-s of matching records.
Expand All @@ -193,7 +196,7 @@ public function executeIds(): array
throw new LogicException('Collection is not ready');
}

if ($this->ids === false) {
if ($this->ids === null) {
$this->ids = $this->connection->executeFirstColumn($this->getSelectSql(false));

if (empty($this->ids)) {
Expand All @@ -206,10 +209,8 @@ public function executeIds(): array

/**
* Return number of items that will be displayed on the current page of paginated collection (or total, if collection is not paginated).
*
* @return int
*/
public function countIds()
public function countIds(): int
{
return count($this->executeIds());
}
Expand Down
2 changes: 1 addition & 1 deletion src/CollectionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function canBeTagged(): bool;
*
* @return ResultInterface|EntityInterface[]
*/
public function execute();
public function execute(): ?iterable;

/**
* Return ID-s of matching records.
Expand Down

0 comments on commit 8a94aa1

Please sign in to comment.