Skip to content

Commit

Permalink
[TASK] Make return types of repositories more strict
Browse files Browse the repository at this point in the history
  • Loading branch information
benjaminkott committed May 22, 2023
1 parent c61fb3f commit dec9d38
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 73 deletions.
6 changes: 2 additions & 4 deletions Classes/Domain/Model/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use TYPO3\CMS\Extbase\DomainObject\AbstractEntity;
use TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder;
use TYPO3\CMS\Extbase\Persistence\ObjectStorage;
use TYPO3\CMS\Extbase\Persistence\QueryResultInterface;

class Post extends AbstractEntity
{
Expand Down Expand Up @@ -236,10 +237,7 @@ public function getComments(): ObjectStorage
return $this->comments;
}

/**
* @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface
*/
public function getActiveComments()
public function getActiveComments(): QueryResultInterface
{
return GeneralUtility::makeInstance(CommentRepository::class)
->findAllByPost($this);
Expand Down
11 changes: 3 additions & 8 deletions Classes/Domain/Repository/CategoryRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
use TYPO3\CMS\Extbase\Persistence\Generic\Typo3QuerySettings;
use TYPO3\CMS\Extbase\Persistence\QueryInterface;
use TYPO3\CMS\Extbase\Persistence\QueryResultInterface;
use TYPO3\CMS\Extbase\Persistence\Repository;

class CategoryRepository extends Repository
Expand All @@ -41,21 +42,15 @@ public function initializeObject(): void
];
}

/**
* @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface
*/
public function findByUids(array $uids)
public function findByUids(array $uids): QueryResultInterface
{
$query = $this->createQuery();
$query->matching($query->in('uid', $uids));

return $query->execute();
}

/**
* @return array|null|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface
*/
public function getByReference(string $table, int $uid, string $field = 'categories')
public function getByReference(string $table, int $uid, string $field = 'categories'): ?QueryResultInterface
{
$connectionPool = GeneralUtility::makeInstance(ConnectionPool::class);
$queryBuilder = $connectionPool->getConnectionForTable('sys_category_record_mm')->createQueryBuilder();
Expand Down
18 changes: 5 additions & 13 deletions Classes/Domain/Repository/CommentRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
use TYPO3\CMS\Extbase\Persistence\Generic\Typo3QuerySettings;
use TYPO3\CMS\Extbase\Persistence\QueryInterface;
use TYPO3\CMS\Extbase\Persistence\QueryResultInterface;
use TYPO3\CMS\Extbase\Persistence\Repository;

class CommentRepository extends Repository
Expand All @@ -43,10 +44,7 @@ public function initializeObject(): void
];
}

/**
* @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface
*/
public function findAllByPost(Post $post)
public function findAllByPost(Post $post): QueryResultInterface
{
$query = $this->createQuery();
$constraints = [];
Expand All @@ -58,10 +56,7 @@ public function findAllByPost(Post $post)
return $result;
}

/**
* @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface
*/
public function findAllByFilter(string $filter = null, int $blogSetup = null)
public function findAllByFilter(string $filter = null, int $blogSetup = null): QueryResultInterface
{
$query = $this->createQuery();
$querySettings = $query->getQuerySettings();
Expand Down Expand Up @@ -95,13 +90,10 @@ public function findAllByFilter(string $filter = null, int $blogSetup = null)
return $query->matching($query->logicalAnd(...$constraints))->execute();
}

return $this->findAll();
return $this->createQuery()->execute();
}

/**
* @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface
*/
public function findActiveComments(int $limit = null, int $blogSetup = null)
public function findActiveComments(int $limit = null, int $blogSetup = null): QueryResultInterface
{
$query = $this->createQuery();

Expand Down
44 changes: 8 additions & 36 deletions Classes/Domain/Repository/PostRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use TYPO3\CMS\Extbase\Persistence\Generic\Typo3QuerySettings;
use TYPO3\CMS\Extbase\Persistence\ObjectStorage;
use TYPO3\CMS\Extbase\Persistence\QueryInterface;
use TYPO3\CMS\Extbase\Persistence\QueryResultInterface;
use TYPO3\CMS\Extbase\Persistence\Repository;
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;

Expand Down Expand Up @@ -139,18 +140,12 @@ public function findByRepositoryDemand(PostRepositoryDemand $repositoryDemand):
return $result;
}

/**
* @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface
*/
public function findAll()
public function findAll(): QueryResultInterface
{
return $this->getFindAllQuery()->execute();
}

/**
* @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface
*/
public function findAllByPid(?int $blogSetup = null)
public function findAllByPid(?int $blogSetup = null): QueryResultInterface
{
$query = $this->getFindAllQuery();

Expand All @@ -166,10 +161,7 @@ public function findAllByPid(?int $blogSetup = null)
return $query->execute();
}

/**
* @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface
*/
public function findAllWithLimit(int $limit)
public function findAllWithLimit(int $limit): QueryResultInterface
{
$query = $this->getFindAllQuery();
$query->setLimit($limit);
Expand All @@ -195,10 +187,7 @@ protected function getFindAllQuery(): QueryInterface
return $query;
}

/**
* @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface
*/
public function findAllByAuthor(Author $author)
public function findAllByAuthor(Author $author): QueryResultInterface
{
$query = $this->createQuery();
$constraints = $this->defaultConstraints;
Expand All @@ -211,10 +200,7 @@ public function findAllByAuthor(Author $author)
return $query->matching($query->logicalAnd(...$constraints))->execute();
}

/**
* @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface
*/
public function findAllByCategory(Category $category)
public function findAllByCategory(Category $category): QueryResultInterface
{
$query = $this->createQuery();
$constraints = $this->defaultConstraints;
Expand All @@ -227,10 +213,7 @@ public function findAllByCategory(Category $category)
return $query->matching($query->logicalAnd(...$constraints))->execute();
}

/**
* @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface
*/
public function findAllByTag(Tag $tag)
public function findAllByTag(Tag $tag): QueryResultInterface
{
$query = $this->createQuery();
$constraints = $this->defaultConstraints;
Expand All @@ -243,10 +226,7 @@ public function findAllByTag(Tag $tag)
return $query->matching($query->logicalAnd(...$constraints))->execute();
}

/**
* @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface
*/
public function findByMonthAndYear(int $year, int $month = null)
public function findByMonthAndYear(int $year, int $month = null): QueryResultInterface
{
$query = $this->createQuery();
$constraints = $this->defaultConstraints;
Expand Down Expand Up @@ -439,8 +419,6 @@ protected function getStoragePidsFromTypoScript(): array

/**
* @return null|ComparisonInterface
* @throws \TYPO3\CMS\Extbase\Configuration\Exception\InvalidConfigurationTypeException
* @throws \TYPO3\CMS\Extbase\Persistence\Exception\InvalidQueryException
*/
protected function getStoragePidConstraint(): ?ComparisonInterface
{
Expand All @@ -452,9 +430,6 @@ protected function getStoragePidConstraint(): ?ComparisonInterface
return null;
}

/**
* @throws \TYPO3\CMS\Extbase\Configuration\Exception\InvalidConfigurationTypeException
*/
protected function getPidsForConstraints(): array
{
// only add non empty pids (pid 0 will be removed as well
Expand All @@ -472,9 +447,6 @@ protected function getPidsForConstraints(): array
return $pids;
}

/**
* @return TypoScriptFrontendController
*/
protected function getTypoScriptFrontendController(): ?TypoScriptFrontendController
{
return $GLOBALS['TSFE'] ?? null;
Expand Down
11 changes: 3 additions & 8 deletions Classes/Domain/Repository/TagRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
use TYPO3\CMS\Extbase\Persistence\QueryInterface;
use TYPO3\CMS\Extbase\Persistence\QueryResultInterface;
use TYPO3\CMS\Extbase\Persistence\Repository;

class TagRepository extends Repository
Expand All @@ -30,10 +31,7 @@ public function initializeObject(): void
];
}

/**
* @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface
*/
public function findByUids(array $uids)
public function findByUids(array $uids): QueryResultInterface
{
$query = $this->createQuery();
$query->matching(
Expand All @@ -43,10 +41,7 @@ public function findByUids(array $uids)
return $query->execute();
}

/**
* @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface
*/
public function findTopByUsage(int $limit = 20)
public function findTopByUsage(int $limit = 20): array
{
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
->getQueryBuilderForTable('tx_blog_domain_model_tag');
Expand Down
6 changes: 2 additions & 4 deletions Classes/Service/CommentService.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use TYPO3\CMS\Core\Context\Context;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface;
use TYPO3\CMS\Extbase\Persistence\QueryResultInterface;

/**
* Class CommentService.
Expand Down Expand Up @@ -99,10 +100,7 @@ protected function approvedCommentExistsForSameEmail(Comment $comment): bool
)->execute()->count() > 0;
}

/**
* @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface
*/
public function getCommentsByPost(Post $post)
public function getCommentsByPost(Post $post): QueryResultInterface
{
return $this->commentRepository->findAllByPost($post);
}
Expand Down

0 comments on commit dec9d38

Please sign in to comment.