Skip to content

Commit

Permalink
[CHORE] Change phpstan level to 6 and cleanup phpstan errors
Browse files Browse the repository at this point in the history
  • Loading branch information
garbast committed Nov 17, 2024
1 parent 01db932 commit ea66f4b
Show file tree
Hide file tree
Showing 21 changed files with 227 additions and 26 deletions.
13 changes: 13 additions & 0 deletions Build/Scripts/phpstan.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/bash

THIS_SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null && pwd)"
cd "$THIS_SCRIPT_DIR" || exit 1
cd ../../ || exit 1
CORE_ROOT="${PWD}"

Build/Scripts/runTests.sh -s composerInstall

Build/Scripts/runTests.sh -s phpstan

Build/Scripts/runTests.sh -s clean
Build/Scripts/additionalTests.sh -s clean
2 changes: 2 additions & 0 deletions Build/phpstan/phpstan-baseline.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
parameters:
ignoreErrors:
4 changes: 4 additions & 0 deletions Build/phpstan/phpstan-constants.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php

// testing-framework defines this, used in various tests.
define('ORIGINAL_ROOT', dirname(__FILE__, 2) . '/');
2 changes: 2 additions & 0 deletions Build/phpstan/phpstan.local.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
includes:
- phpstan.neon
21 changes: 21 additions & 0 deletions Build/phpstan/phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
includes:
- phpstan-baseline.neon
- ../vendor/friendsoftypo3/phpstan-typo3/extension.neon

parameters:
# Use local .cache dir instead of /tmp
tmpDir: ../.cache/phpstan

level: 6

bootstrapFiles:
- phpstan-constants.php

paths:
- ../../

excludePaths:
# we do not check required extensions
- ../../Build/*
# ext_emconf.php get the $_EXTKEY set from outside. We'll ignore all of them
- ../ext_emconf.php
21 changes: 18 additions & 3 deletions Classes/Controller/AbstractController.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@

namespace Evoweb\SfBooks\Controller;

use Evoweb\SfBooks\Domain\Model\Author;
use Evoweb\SfBooks\Domain\Model\Book;
use Evoweb\SfBooks\Domain\Model\Category;
use Evoweb\SfBooks\Domain\Model\Series;
use Evoweb\SfBooks\Domain\Repository\AuthorRepository;
use Evoweb\SfBooks\Domain\Repository\BookRepository;
use Evoweb\SfBooks\Domain\Repository\CategoryRepository;
use Evoweb\SfBooks\Domain\Repository\SeriesRepository;
use Evoweb\SfBooks\TitleTagProvider\TitleTagProvider;
use TYPO3\CMS\Core\Core\Environment;
use TYPO3\CMS\Core\Http\ImmediateResponseException;
Expand All @@ -24,18 +32,19 @@
use TYPO3\CMS\Core\Pagination\SimplePagination;
use TYPO3\CMS\Core\Resource\ResourceFactory;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\DomainObject\AbstractEntity;
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
use TYPO3\CMS\Extbase\Pagination\QueryResultPaginator;
use TYPO3\CMS\Extbase\Persistence\QueryInterface;
use TYPO3\CMS\Extbase\Persistence\QueryResultInterface;
use TYPO3\CMS\Extbase\Persistence\Repository;
use TYPO3\CMS\Frontend\Controller\ErrorController;
use TYPO3Fluid\Fluid\View\ViewInterface;

abstract class AbstractController extends ActionController
{
protected function setDefaultOrderings(Repository $repository): Repository
{
protected function setDefaultOrderings(
AuthorRepository|BookRepository|CategoryRepository|SeriesRepository $repository
): AuthorRepository|BookRepository|CategoryRepository|SeriesRepository {
$orderField = $this->getOrderField();
if ($orderField !== '') {
$orderings = [$orderField => $this->getOrderDirection()];
Expand Down Expand Up @@ -116,6 +125,9 @@ protected function displayError(string $type): void
throw new ImmediateResponseException($response);
}

/**
* @param QueryResultInterface<int, Author>|QueryResultInterface<int, Book>|QueryResultInterface<int, Category>|array<int, Series> $result
*/
protected function addPaginatorToView(QueryResultInterface|array $result): void
{
$paginator = $this->getPaginator($result);
Expand All @@ -130,6 +142,9 @@ protected function addPaginatorToView(QueryResultInterface|array $result): void
);
}

/**
* @param QueryResultInterface<int, AbstractEntity>|array<int, AbstractEntity> $result
*/
protected function getPaginator(QueryResultInterface|array $result): PaginatorInterface
{
$currentPage = $this->request->hasArgument('currentPage')
Expand Down
4 changes: 4 additions & 0 deletions Classes/Controller/CategoryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ protected function listAction(): ResponseInterface
return new HtmlResponse($this->view->render());
}

/**
* @param QueryResultInterface<int, Category> $categories
* @return QueryResultInterface<int, Category>
*/
protected function removeExcludeCategories(QueryResultInterface $categories): QueryResultInterface
{
$excludeCategories = GeneralUtility::intExplode(',', $this->settings['excludeCategories'], true);
Expand Down
10 changes: 8 additions & 2 deletions Classes/Controller/SearchController.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,14 @@ public function searchAction(): ResponseInterface
return new HtmlResponse($this->view->render());
}

/**
* @param array<string, mixed> $search
*/
public function startSearchAction(array $search): ResponseInterface
{
if (($search['query'] ?? '') != '') {
if (isset($search['searchBy'])) {
switch ((string)$search['searchFor'] ?? '') {
switch ((string)($search['searchFor'] ?? '')) {
case 'author':
$controller = 'Author';
$pageId = (int)$this->settings['authorPageId'];
Expand All @@ -49,7 +52,7 @@ public function startSearchAction(array $search): ResponseInterface
$pageId = $this->request->getAttribute('currentContentObject')->data['pid'];
}

$this->redirect('search', $controller, null, $search, $pageId, $controller);
return $this->redirect('search', $controller, null, $search, $pageId);
}
$response = new HtmlResponse($this->view->render());
} else {
Expand All @@ -59,6 +62,9 @@ public function startSearchAction(array $search): ResponseInterface
return $response;
}

/**
* @param ?array<string, mixed> $arguments
*/
protected function redirect(
$actionName,
$controllerName = null,
Expand Down
6 changes: 6 additions & 0 deletions Classes/Domain/Model/Author.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,17 @@ public function initializeObject(): void
$this->books = new ObjectStorage();
}

/**
* @param ObjectStorage<Book> $books
*/
public function setBooks(ObjectStorage $books): void
{
$this->books = $books;
}

/**
* @return ObjectStorage<Book>
*/
public function getBooks(): ObjectStorage
{
return $this->books;
Expand Down
36 changes: 36 additions & 0 deletions Classes/Domain/Model/Book.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,61 +91,97 @@ public function initializeObject(): void
$this->samplePdf = new ObjectStorage();
}

/**
* @param ObjectStorage<Author> $author
*/
public function setAuthor(ObjectStorage $author): void
{
$this->author = $author;
}

/**
* @return ObjectStorage<Author>
*/
public function getAuthor(): ObjectStorage
{
return $this->author;
}

/**
* @param ObjectStorage<Category> $category
*/
public function setCategory(ObjectStorage $category): void
{
$this->category = $category;
}

/**
* @return ObjectStorage<Category>
*/
public function getCategory(): ObjectStorage
{
return $this->category;
}

/**
* @param ObjectStorage<Extras> $extras
*/
public function setExtras(ObjectStorage $extras): void
{
$this->extras = $extras;
}

/**
* @return ObjectStorage<Extras>
*/
public function getExtras(): ObjectStorage
{
return $this->extras;
}

/**
* @param ObjectStorage<FileReference> $cover
*/
public function setCover(ObjectStorage $cover): void
{
$this->cover = $cover;
}

/**
* @return ObjectStorage<FileReference>
*/
public function getCover(): ObjectStorage
{
return $this->cover;
}

/**
* @param ObjectStorage<FileReference> $coverLarge
*/
public function setCoverLarge(ObjectStorage $coverLarge): void
{
$this->coverLarge = $coverLarge;
}

/**
* @return ObjectStorage<FileReference>
*/
public function getCoverLarge(): ObjectStorage
{
return $this->coverLarge;
}

/**
* @param ObjectStorage<FileReference> $samplePdf
*/
public function setSamplePdf(ObjectStorage $samplePdf): void
{
$this->samplePdf = $samplePdf;
}

/**
* @return ObjectStorage<FileReference>
*/
public function getSamplePdf(): ObjectStorage
{
return $this->samplePdf;
Expand Down
12 changes: 12 additions & 0 deletions Classes/Domain/Model/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,21 +53,33 @@ public function initializeObject(): void
$this->books = new ObjectStorage();
}

/**
* @param ObjectStorage<Category> $children
*/
public function setChildren(ObjectStorage $children): void
{
$this->children = $children;
}

/**
* @return ObjectStorage<Category>
*/
public function getChildren(): ObjectStorage
{
return $this->children;
}

/**
* @param ObjectStorage<Book> $books
*/
public function setBooks(ObjectStorage $books): void
{
$this->books = $books;
}

/**
* @return ObjectStorage<Book>
*/
public function getBooks(): ObjectStorage
{
return $this->books;
Expand Down
6 changes: 6 additions & 0 deletions Classes/Domain/Model/Series.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,17 @@ public function initializeObject(): void
$this->books = new ObjectStorage();
}

/**
* @param ObjectStorage<Book> $books
*/
public function setBooks(ObjectStorage $books): void
{
$this->books = $books;
}

/**
* @return ObjectStorage<Book>
*/
public function getBooks(): ObjectStorage
{
return $this->books;
Expand Down
20 changes: 19 additions & 1 deletion Classes/Domain/Repository/AuthorRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,18 @@
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Database\Query\QueryBuilder;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Persistence\Exception\InvalidQueryException;
use TYPO3\CMS\Extbase\Persistence\Generic\Exception;
use TYPO3\CMS\Extbase\Persistence\Generic\Query;
use TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface;
use TYPO3\CMS\Extbase\Persistence\QueryResultInterface;
use TYPO3\CMS\Extbase\Persistence\Repository;

/**
* A repository for authors
*
* @extends Repository<Author>
*/
class AuthorRepository extends Repository
{
public function __construct(
Expand All @@ -33,8 +41,13 @@ public function __construct(
parent::__construct();
}

/**
* @return array<string, mixed>
* @throws Exception
*/
public function findAuthorGroupedByLetters(): array
{
/** @var Query<Author> $query */
$query = $this->createQuery();

$queryBuilder = $this->getQueryBuilderForTable('tx_sfbooks_domain_model_author');
Expand All @@ -57,7 +70,7 @@ public function findAuthorGroupedByLetters(): array
/** @var Author $author */
foreach ($result as $author) {
$letter = $author->getCapitalLetter();
if (!isset($groupedAuthors[$letter]) || !is_array($groupedAuthors[$letter])) {
if (!is_array($groupedAuthors[$letter] ?? '')) {
$groupedAuthors[$letter] = [];
}

Expand All @@ -67,6 +80,11 @@ public function findAuthorGroupedByLetters(): array
return $groupedAuthors;
}

/**
* @param string[] $searchFields
* @return QueryResultInterface<int, Author>
* @throws InvalidQueryException
*/
public function findBySearch(string $searchString, array $searchFields): QueryResultInterface
{
$query = $this->createQuery();
Expand Down
Loading

0 comments on commit ea66f4b

Please sign in to comment.