diff --git a/src/Models/AbstractCrudModel.php b/src/Models/AbstractCrudModel.php index 36fe418b..8b6a6a01 100644 --- a/src/Models/AbstractCrudModel.php +++ b/src/Models/AbstractCrudModel.php @@ -15,11 +15,15 @@ abstract class AbstractCrudModel implements IModel public static function spawnFromDatabaseRows($input) { + if (!$input) + return []; return array_map([get_called_class(), 'spawnFromDatabaseRow'], $input); } public static function spawnFromDatabaseRow($row) { + if (!$row) + return null; $entityClassName = static::getEntityClassName(); $entity = new $entityClassName(new static); $entity->fillFromDatabase($row); @@ -106,12 +110,8 @@ abstract class AbstractCrudModel implements IModel $stmt->setColumn('*'); $stmt->setTable(static::getTableName()); $stmt->setCriterion(Sql\Functors::in('id', Sql\Binding::fromArray(array_unique($ids)))); - $rows = Core::getDatabase()->fetchAll($stmt); - if ($rows) - return static::spawnFromDatabaseRows($rows); - - return []; + return static::spawnFromDatabaseRows($rows); } public static function getCount() diff --git a/src/Models/CommentModel.php b/src/Models/CommentModel.php index 85741d13..79f7ff26 100644 --- a/src/Models/CommentModel.php +++ b/src/Models/CommentModel.php @@ -58,9 +58,7 @@ final class CommentModel extends AbstractCrudModel $stmt->setCriterion(Sql\Functors::equals('post_id', new Sql\Binding($key))); $rows = Core::getDatabase()->fetchAll($stmt); - if ($rows) - return self::spawnFromDatabaseRows($rows); - return []; + return self::spawnFromDatabaseRows($rows); } diff --git a/src/Models/PostModel.php b/src/Models/PostModel.php index 96f1e33e..7063e473 100644 --- a/src/Models/PostModel.php +++ b/src/Models/PostModel.php @@ -135,9 +135,7 @@ final class PostModel extends AbstractCrudModel $stmt->setCriterion(Sql\Functors::equals('name', new Sql\Binding($key))); $row = Core::getDatabase()->fetchOne($stmt); - return $row - ? self::spawnFromDatabaseRow($row) - : null; + return self::spawnFromDatabaseRow($row); } public static function getByIdOrName($key) @@ -165,9 +163,7 @@ final class PostModel extends AbstractCrudModel $stmt->setCriterion(Sql\Functors::equals('file_hash', new Sql\Binding($key))); $row = Core::getDatabase()->fetchOne($stmt); - return $row - ? self::spawnFromDatabaseRow($row) - : null; + return self::spawnFromDatabaseRow($row); } diff --git a/src/Models/TagModel.php b/src/Models/TagModel.php index 79f814e6..58907a11 100644 --- a/src/Models/TagModel.php +++ b/src/Models/TagModel.php @@ -120,9 +120,7 @@ final class TagModel extends AbstractCrudModel $stmt->setCriterion(Sql\Functors::equals('post_tag.post_id', new Sql\Binding($key))); $rows = Core::getDatabase()->fetchAll($stmt); - if ($rows) - return self::spawnFromDatabaseRows($rows); - return []; + return self::spawnFromDatabaseRows($rows); } public static function getByName($key) @@ -141,9 +139,7 @@ final class TagModel extends AbstractCrudModel $stmt->setCriterion(Sql\Functors::noCase(Sql\Functors::equals('name', new Sql\Binding($key)))); $row = Core::getDatabase()->fetchOne($stmt); - return $row - ? self::spawnFromDatabaseRow($row) - : null; + return self::spawnFromDatabaseRow($row); } diff --git a/src/Models/TokenModel.php b/src/Models/TokenModel.php index 9c97115f..38419232 100644 --- a/src/Models/TokenModel.php +++ b/src/Models/TokenModel.php @@ -55,9 +55,7 @@ final class TokenModel extends AbstractCrudModel $stmt->setCriterion(Sql\Functors::equals('token', new Sql\Binding($key))); $row = Core::getDatabase()->fetchOne($stmt); - return $row - ? self::spawnFromDatabaseRow($row) - : null; + return self::spawnFromDatabaseRow($row); } public static function checkValidity($token) diff --git a/src/Models/UserModel.php b/src/Models/UserModel.php index 770d1129..772fa7cd 100644 --- a/src/Models/UserModel.php +++ b/src/Models/UserModel.php @@ -94,9 +94,7 @@ final class UserModel extends AbstractCrudModel $stmt->setCriterion(Sql\Functors::noCase(Sql\Functors::equals('name', new Sql\Binding(trim($key))))); $row = Core::getDatabase()->fetchOne($stmt); - return $row - ? self::spawnFromDatabaseRow($row) - : null; + return self::spawnFromDatabaseRow($row); } public static function getByEmail($key) @@ -119,9 +117,7 @@ final class UserModel extends AbstractCrudModel ->add(Sql\Functors::noCase(Sql\Functors::equals('email_confirmed', new Sql\Binding($key))))); $row = Core::getDatabase()->fetchOne($stmt); - return $row - ? self::spawnFromDatabaseRow($row) - : null; + return self::spawnFromDatabaseRow($row); }