From 8a94aa16b6967b88b06c7d287b1e78767d99686f Mon Sep 17 00:00:00 2001 From: Ilija Studen Date: Fri, 8 Mar 2024 10:10:36 +0100 Subject: [PATCH] Add type to collection's execute() method --- src/Collection/Type.php | 39 +++++++++++++++++++------------------ src/CollectionInterface.php | 2 +- 2 files changed, 21 insertions(+), 20 deletions(-) diff --git a/src/Collection/Type.php b/src/Collection/Type.php index 392db73..edd4550 100644 --- a/src/Collection/Type.php +++ b/src/Collection/Type.php @@ -150,7 +150,7 @@ 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'); @@ -158,31 +158,34 @@ public function execute() 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. @@ -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)) { @@ -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()); } diff --git a/src/CollectionInterface.php b/src/CollectionInterface.php index 938ad59..126cc83 100644 --- a/src/CollectionInterface.php +++ b/src/CollectionInterface.php @@ -26,7 +26,7 @@ public function canBeTagged(): bool; * * @return ResultInterface|EntityInterface[] */ - public function execute(); + public function execute(): ?iterable; /** * Return ID-s of matching records.