From 0d0c64f4191bec117fda24b3cc6ff5cdf9083288 Mon Sep 17 00:00:00 2001 From: Daniel Siepmann Date: Mon, 13 Dec 2021 09:09:13 +0100 Subject: [PATCH] Fix PHPStan issues --- Classes/Controller/DateController.php | 5 +- Classes/Domain/Repository/DateRepository.php | 24 +++- Classes/Domain/Repository/EventRepository.php | 3 + Classes/Extbase/AddSpecialProperties.php | 8 +- Classes/Service/CategoryService.php | 18 +-- Classes/Service/Cleanup/Database.php | 18 +-- Classes/Service/Cleanup/Files.php | 7 +- Classes/Service/DataProcessingForModels.php | 4 + Configuration/TCA/Overrides/sys_category.php | 2 - Configuration/TCA/Overrides/tt_content.php | 28 +---- .../tx_events_domain_model_event.php | 26 ++--- .../TCA/tx_events_domain_model_event.php | 2 - phpstan-baseline.neon | 110 +++--------------- 13 files changed, 94 insertions(+), 161 deletions(-) diff --git a/Classes/Controller/DateController.php b/Classes/Controller/DateController.php index 601ef92..b357bbe 100644 --- a/Classes/Controller/DateController.php +++ b/Classes/Controller/DateController.php @@ -111,6 +111,9 @@ class DateController extends AbstractController public function searchAction(): void { $arguments = GeneralUtility::_GET('tx_events_datelist') ?? []; + if (is_array($arguments) === false) { + $arguments = []; + } if (isset($arguments['events_search'])) { $arguments += $arguments['events_search']; unset($arguments['events_search']); @@ -144,7 +147,7 @@ class DateController extends AbstractController protected function createDemandFromSearch(): DateDemand { - $arguments = $this->request->getArguments() ?? []; + $arguments = $this->request->getArguments(); if (isset($arguments['events_search'])) { $arguments += $arguments['events_search']; unset($arguments['events_search']); diff --git a/Classes/Domain/Repository/DateRepository.php b/Classes/Domain/Repository/DateRepository.php index 06fe0fe..41ceda0 100644 --- a/Classes/Domain/Repository/DateRepository.php +++ b/Classes/Domain/Repository/DateRepository.php @@ -48,7 +48,11 @@ class DateRepository extends Repository $categories = $demand->getCategories(); if ($categories) { - $categoryConstraints = $this->createCategoryConstraint($query, $categories, $demand->getIncludeSubCategories()); + $categoryConstraints = $this->createCategoryConstraint( + $query, + $categories, + $demand->getIncludeSubCategories() + ); if ($demand->getCategoryCombination() === 'or') { $constraints['categories'] = $query->logicalOr($categoryConstraints); } else { @@ -96,6 +100,12 @@ class DateRepository extends Repository 'full', new \DateTimeImmutable() ); + if (!$now instanceof \DateTimeImmutable) { + throw new \UnexpectedValueException( + 'Could not retrieve now as DateTimeImmutable, got "' . gettype($now) . '".', + 1639382648 + ); + } $now = $now->modify('midnight'); $constraints['nowAndFuture'] = $query->logicalOr([ $query->greaterThanOrEqual('start', $now), @@ -148,8 +158,11 @@ class DateRepository extends Repository return $query->logicalOr($constraints); } - protected function createCategoryConstraint(QueryInterface $query, string $categories, bool $includeSubCategories = false): array - { + protected function createCategoryConstraint( + QueryInterface $query, + string $categories, + bool $includeSubCategories = false + ): array { $constraints = []; if ($includeSubCategories) { @@ -178,7 +191,10 @@ class DateRepository extends Repository 'tx_events_domain_model_date', 'tx_events_domain_model_event', 'event', - $queryBuilder->expr()->eq('tx_events_domain_model_date.event', $queryBuilder->quoteIdentifier('event.uid')) + $queryBuilder->expr()->eq( + 'tx_events_domain_model_date.event', + $queryBuilder->quoteIdentifier('event.uid') + ) )->where( $queryBuilder->expr()->like('event.title', $queryBuilder->createNamedParameter('%' . $search . '%')) )->orderBy('tx_events_domain_model_date.start'); diff --git a/Classes/Domain/Repository/EventRepository.php b/Classes/Domain/Repository/EventRepository.php index 62b1855..efb2c42 100644 --- a/Classes/Domain/Repository/EventRepository.php +++ b/Classes/Domain/Repository/EventRepository.php @@ -106,6 +106,9 @@ class EventRepository extends Repository return $result; } + /** + * @return ConstraintInterface[] + */ private function getConstraints(QueryInterface $query, EventDemand $demand): array { $constraints = []; diff --git a/Classes/Extbase/AddSpecialProperties.php b/Classes/Extbase/AddSpecialProperties.php index 77243d9..c61196a 100644 --- a/Classes/Extbase/AddSpecialProperties.php +++ b/Classes/Extbase/AddSpecialProperties.php @@ -66,7 +66,13 @@ class AddSpecialProperties ) { /** @var Date $date */ $date = $event->getObject(); - $date->_setProperty('originalDate', $this->getOriginalDate($date->_getProperty('_localizedUid'))); + + $localizedUid = $date->_getProperty('_localizedUid'); + if (is_numeric($localizedUid) === false) { + return; + } + + $date->_setProperty('originalDate', $this->getOriginalDate((int) $localizedUid)); } } diff --git a/Classes/Service/CategoryService.php b/Classes/Service/CategoryService.php index c57e2a0..43fc4a3 100644 --- a/Classes/Service/CategoryService.php +++ b/Classes/Service/CategoryService.php @@ -31,12 +31,12 @@ class CategoryService * @param int $counter * @return string comma separated list of category ids */ - public function getChildrenCategories($idList, int $counter = 0) + public function getChildrenCategories($idList, int $counter = 0): string { $cacheIdentifier = sha1('children' . $idList); $entry = $this->cache->get($cacheIdentifier); - if (!$entry) { + if (!$entry || is_string($entry) === false) { $entry = $this->getChildrenCategoriesRecursive($idList, $counter); $this->cache->set($cacheIdentifier, $entry); } @@ -68,9 +68,10 @@ class CategoryService $res = $queryBuilder ->select('uid') ->from('sys_category') - ->where( - $queryBuilder->expr()->in('parent', $queryBuilder->createNamedParameter(explode(',', $idList), Connection::PARAM_INT_ARRAY)) - ) + ->where($queryBuilder->expr()->in( + 'parent', + $queryBuilder->createNamedParameter(explode(',', $idList), Connection::PARAM_INT_ARRAY) + )) ->execute(); while ($row = $res->fetch()) { @@ -101,9 +102,10 @@ class CategoryService $rows = $queryBuilder ->select('uid') ->from('sys_category') - ->where( - $queryBuilder->expr()->in('uid', $queryBuilder->createNamedParameter(explode(',', $idList), Connection::PARAM_INT_ARRAY)) - ) + ->where($queryBuilder->expr()->in( + 'uid', + $queryBuilder->createNamedParameter(explode(',', $idList), Connection::PARAM_INT_ARRAY) + )) ->execute() ->fetchAll(); foreach ($rows as $row) { diff --git a/Classes/Service/Cleanup/Database.php b/Classes/Service/Cleanup/Database.php index 655db3a..b03500f 100644 --- a/Classes/Service/Cleanup/Database.php +++ b/Classes/Service/Cleanup/Database.php @@ -75,14 +75,14 @@ class Database public function getPastDates(): array { $queryBuilder = $this->connectionPool - ->getConnectionForTable(static::DATE_TABLE) + ->getConnectionForTable(self::DATE_TABLE) ->createQueryBuilder(); $queryBuilder->getRestrictions()->removeAll(); $midnightToday = new \DateTimeImmutable('midnight today'); $records = $queryBuilder->select('uid') - ->from(static::DATE_TABLE) + ->from(self::DATE_TABLE) ->where($queryBuilder->expr()->lte( 'end', $queryBuilder->createNamedParameter($midnightToday->format('Y-m-d H:i:s')) @@ -98,9 +98,9 @@ class Database public function deleteDates(int ...$uids): void { $queryBuilder = $this->connectionPool - ->getQueryBuilderForTable(static::DATE_TABLE); + ->getQueryBuilderForTable(self::DATE_TABLE); - $queryBuilder->delete(static::DATE_TABLE) + $queryBuilder->delete(self::DATE_TABLE) ->where('uid in (:uids)') ->setParameter(':uids', $uids, Connection::PARAM_INT_ARRAY) ->execute(); @@ -109,21 +109,21 @@ class Database public function deleteEventsWithoutDates(): void { $queryBuilder = $this->connectionPool - ->getConnectionForTable(static::EVENT_TABLE) + ->getConnectionForTable(self::EVENT_TABLE) ->createQueryBuilder(); $queryBuilder->getRestrictions()->removeAll(); $recordUids = $queryBuilder->select('event.uid') - ->from(static::EVENT_TABLE, 'event') - ->leftJoin('event', static::DATE_TABLE, 'date', $queryBuilder->expr()->eq('date.event', 'event.uid')) + ->from(self::EVENT_TABLE, 'event') + ->leftJoin('event', self::DATE_TABLE, 'date', $queryBuilder->expr()->eq('date.event', 'event.uid')) ->where($queryBuilder->expr()->isNull('date.uid')) ->execute() ->fetchAll(\PDO::FETCH_COLUMN); - $dataStructure = [static::EVENT_TABLE => []]; + $dataStructure = [self::EVENT_TABLE => []]; foreach ($recordUids as $recordUid) { - $dataStructure[static::EVENT_TABLE][$recordUid] = ['delete' => 1]; + $dataStructure[self::EVENT_TABLE][$recordUid] = ['delete' => 1]; } $dataHandler = clone $this->dataHandler; diff --git a/Classes/Service/Cleanup/Files.php b/Classes/Service/Cleanup/Files.php index ca7e702..9f62f4f 100644 --- a/Classes/Service/Cleanup/Files.php +++ b/Classes/Service/Cleanup/Files.php @@ -24,7 +24,6 @@ namespace Wrm\Events\Service\Cleanup; use TYPO3\CMS\Core\Database\Connection; use TYPO3\CMS\Core\Database\ConnectionPool; use TYPO3\CMS\Core\Database\Query\QueryBuilder; -use TYPO3\CMS\Core\Resource\ResourceStorage; use TYPO3\CMS\Core\Resource\StorageRepository; class Files @@ -47,12 +46,12 @@ class Files $this->storageRepository = $storageRepository; } - public function deleteAll() + public function deleteAll(): void { $this->delete($this->getFilesFromDb()); } - public function deleteDangling() + public function deleteDangling(): void { $this->delete($this->getFilesFromDb(function (QueryBuilder $queryBuilder) { $queryBuilder->leftJoin( @@ -107,7 +106,7 @@ class Files { $storage = $this->storageRepository->findByUid($storageUid); - if ($storage->hasFile($filePath) === false) { + if ($storage === null || $storage->hasFile($filePath) === false) { return; } diff --git a/Classes/Service/DataProcessingForModels.php b/Classes/Service/DataProcessingForModels.php index bc5a596..ebd9b6e 100644 --- a/Classes/Service/DataProcessingForModels.php +++ b/Classes/Service/DataProcessingForModels.php @@ -168,6 +168,10 @@ class DataProcessingForModels implements SingletonInterface } $configuration = ArrayUtility::getValueByPath($settings, 'dataProcessing.' . $className, '.'); + if (is_array($configuration) === false) { + return []; + } + $configuration = $this->typoScriptService->convertPlainArrayToTypoScriptArray($configuration); return [ 'dataProcessing.' => $configuration, diff --git a/Configuration/TCA/Overrides/sys_category.php b/Configuration/TCA/Overrides/sys_category.php index 4c79736..9a3949a 100644 --- a/Configuration/TCA/Overrides/sys_category.php +++ b/Configuration/TCA/Overrides/sys_category.php @@ -1,7 +1,5 @@ [ diff --git a/Configuration/TCA/Overrides/tt_content.php b/Configuration/TCA/Overrides/tt_content.php index 08ae561..3638720 100644 --- a/Configuration/TCA/Overrides/tt_content.php +++ b/Configuration/TCA/Overrides/tt_content.php @@ -1,71 +1,55 @@ 'Categories', - 'fieldConfiguration' => [ - 'minitems' => 0, - 'multiple' => true, +(function (string $extKey, string $table) { + \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::makeCategorizable( + $extKey, + $table, + 'categories', + [ + 'label' => 'Categories', + 'fieldConfiguration' => [ + 'minitems' => 0, + 'multiple' => true, + ] ] - ] -); + ); +})('events', 'tx_events_domain_model_event'); diff --git a/Configuration/TCA/tx_events_domain_model_event.php b/Configuration/TCA/tx_events_domain_model_event.php index 86c9574..458937b 100644 --- a/Configuration/TCA/tx_events_domain_model_event.php +++ b/Configuration/TCA/tx_events_domain_model_event.php @@ -1,7 +1,5 @@