From 1540bb64be404c5b38f427ad2706f74d0f3e3534 Mon Sep 17 00:00:00 2001 From: Tom Kay Date: Fri, 6 Nov 2026 11:22:44 +0000 Subject: [PATCH] Add generic docblocks so IDEs resolve DAO types on collections Template IDaoCollection/DaoCollection/QlDaoCollection/CqlDaoCollection on the DAO type, and return QlDaoCollection from QlDao's collection factories, so User::collection()->where()->first(), foreach and array access resolve to User. Fluent query-builder methods are declared as returning $this to keep the type through chains. Docblock-only. Co-Authored-By: Claude Opus 5.5 --- src/Collections/IAggregateDaoCollection.php | 4 +++ src/Foundation/DaoCollection.php | 29 ++++++++++++--------- src/IDaoCollection.php | 5 ++++ src/Ql/Cql/CqlDao.php | 2 +- src/Ql/Cql/CqlDaoCollection.php | 6 ++--- src/Ql/QlDao.php | 8 +++--- src/Ql/QlDaoCollection.php | 23 +++++++++++++--- 7 files changed, 53 insertions(+), 24 deletions(-) diff --git a/src/Collections/IAggregateDaoCollection.php b/src/Collections/IAggregateDaoCollection.php index fa11aa5..274e822 100644 --- a/src/Collections/IAggregateDaoCollection.php +++ b/src/Collections/IAggregateDaoCollection.php @@ -3,6 +3,10 @@ use Packaged\Dal\IDaoCollection; +/** + * @template T of \Packaged\Dal\IDao + * @extends IDaoCollection + */ interface IAggregateDaoCollection extends IDaoCollection { public function min($property = 'id'); diff --git a/src/Foundation/DaoCollection.php b/src/Foundation/DaoCollection.php index a1745eb..edf0086 100644 --- a/src/Foundation/DaoCollection.php +++ b/src/Foundation/DaoCollection.php @@ -9,23 +9,26 @@ use Packaged\Helpers\Arrays; use Packaged\Helpers\Objects; use RuntimeException; -use Traversable; +/** + * @template T of IDao + * @implements IDaoCollection + */ class DaoCollection implements IDaoCollection { /** - * @var IDao[] + * @var T[] */ protected $_daos = []; /** - * @var IDao + * @var T|null */ protected $_dao; /** * Class for the DAOs contained * - * @var string + * @var class-string */ protected $_daoClass; @@ -40,7 +43,7 @@ protected function _prepareDaos() /** * @param $fresh bool Create a new instance of the DAO Class * - * @return IDao + * @return T */ public function createNewDao($fresh = true) { @@ -68,9 +71,11 @@ public function createNewDao($fresh = true) /** * Create a new collection based on a DAO class * - * @param string|object $daoClass + * @template D of IDao * - * @return static + * @param class-string|D $daoClass + * + * @return static */ public static function create($daoClass) { @@ -99,7 +104,7 @@ public function clear() } /** - * @return IDao[] + * @return T[] */ public function getRawArray() { @@ -112,7 +117,7 @@ public function getRawArray() * * @param mixed $default * - * @return IDao + * @return T|null */ public function first($default = null) { @@ -205,9 +210,7 @@ public function apull(array $properties, $keyProperty = null) * Retrieve an external iterator * * @link http://php.net/manual/en/iteratoraggregate.getiterator.php - * @return Traversable An instance of an object implementing Iterator - * or - * Traversable + * @return ArrayIterator */ #[\ReturnTypeWillChange] public function getIterator() @@ -248,7 +251,7 @@ public function offsetExists($offset) * The offset to retrieve. *

* - * @return mixed Can return all value types. + * @return T|null */ #[\ReturnTypeWillChange] public function offsetGet($offset) diff --git a/src/IDaoCollection.php b/src/IDaoCollection.php index 2260671..ebc4b9d 100644 --- a/src/IDaoCollection.php +++ b/src/IDaoCollection.php @@ -1,6 +1,11 @@ + * @extends \ArrayAccess + */ interface IDaoCollection extends \Countable, \JsonSerializable, \IteratorAggregate, \ArrayAccess diff --git a/src/Ql/Cql/CqlDao.php b/src/Ql/Cql/CqlDao.php index 5ca6757..dfc1c25 100644 --- a/src/Ql/Cql/CqlDao.php +++ b/src/Ql/Cql/CqlDao.php @@ -18,7 +18,7 @@ public function getTimestamp() /** * @param string|object|null $class * - * @return CqlDaoCollection + * @return CqlDaoCollection */ protected static function _createCollection($class = null) { diff --git a/src/Ql/Cql/CqlDaoCollection.php b/src/Ql/Cql/CqlDaoCollection.php index 221cde8..55f10ec 100644 --- a/src/Ql/Cql/CqlDaoCollection.php +++ b/src/Ql/Cql/CqlDaoCollection.php @@ -8,13 +8,13 @@ use Packaged\QueryBuilder\SelectExpression\ISelectExpression; /** - * @method CqlDao createNewDao($fresh = true) - * @method CqlDao[] getRawArray() + * @template T of CqlDao + * @extends QlDaoCollection */ class CqlDaoCollection extends QlDaoCollection { /** - * @return CqlQueryBuilder + * @return class-string */ protected function _getQueryBuilder() { diff --git a/src/Ql/QlDao.php b/src/Ql/QlDao.php index b1c2470..bea3eda 100644 --- a/src/Ql/QlDao.php +++ b/src/Ql/QlDao.php @@ -58,7 +58,7 @@ public function getTableName() * * @param $params * - * @return QlDaoCollection + * @return QlDaoCollection */ public static function loadWhere(...$params) { @@ -73,7 +73,7 @@ public static function loadWhere(...$params) /** * @param string|object|null $class * - * @return QlDaoCollection + * @return QlDaoCollection */ protected static function _createCollection($class = null) { @@ -88,7 +88,7 @@ protected static function _createCollection($class = null) /** * @param $params * - * @return QlDaoCollection + * @return QlDaoCollection */ public static function collection(...$params) { @@ -103,7 +103,7 @@ public static function collection(...$params) /** * @param $params * - * @return QlDaoCollection + * @return QlDaoCollection */ public function getCollection(...$params) { diff --git a/src/Ql/QlDaoCollection.php b/src/Ql/QlDaoCollection.php index a8b2d8d..508721e 100644 --- a/src/Ql/QlDaoCollection.php +++ b/src/Ql/QlDaoCollection.php @@ -28,8 +28,22 @@ use Packaged\QueryBuilder\Statement\QueryStatement; /** - * @method QlDao createNewDao($fresh = true) - * @method QlDao[] getRawArray() + * @template T of QlDao + * @extends DaoCollection + * @implements IAggregateDaoCollection + * + * @method $this where(...$expressions) + * @method $this andWhere(...$expressions) + * @method $this orWhere(...$expressions) + * @method $this having(...$expressions) + * @method $this orderBy($fields) + * @method $this limit($limit) + * @method $this limitWithOffset($offset, $limit) + * @method $this join($table, $sourceField, $destField = null, $where = null) + * @method $this innerJoin($table, $sourceField, $destField = null, $where = null) + * @method $this leftOuterJoin($table, $sourceField, $destField = null, $where = null) + * @method $this rightOuterJoin($table, $sourceField, $destField = null, $where = null) + * @method $this joinWithPredicates($table, ...$predicates) */ class QlDaoCollection extends DaoCollection implements IAggregateDaoCollection, IStatement @@ -166,6 +180,9 @@ public function loadWhere(...$params) return $this; } + /** + * @return $this + */ public function load() { $dao = $this->createNewDao(false); @@ -203,7 +220,7 @@ public function delete() * * @param mixed $default * - * @return QlDao|null|mixed + * @return T|null */ public function first($default = null) {