From 95558ac62c9bd81af15a1969bc23656a2c6dfabe Mon Sep 17 00:00:00 2001 From: Riad Date: Mon, 7 Oct 2024 13:13:57 +0200 Subject: [PATCH] [TASK] Correct return types, properties and parameters Resolve PHPStan reported issues till level 5 as preperation for future upgrades --- Classes/Controller/PollController.php | 6 ++--- Classes/Domain/Model/BasePoll.php | 8 +++--- Classes/Domain/Repository/PollRepository.php | 27 ++++++++++++++----- Classes/Event/EditSuggestionEvent.php | 2 +- Classes/Event/NewPollEvent.php | 2 +- Classes/Event/NewSuggestionEvent.php | 2 +- .../Event/Permission/PermissionCheckEvent.php | 4 +-- .../Event/PollRepository/FindPollsEvent.php | 8 +++--- Classes/Event/ShowPollEvent.php | 2 +- Classes/Event/VotePollEvent.php | 5 ++++ .../EventListener/UpdatePollSlugListener.php | 5 +++- .../Traits/Model/DynamicUserProperties.php | 10 +++++-- Classes/Utility/SlugUtility.php | 17 +----------- Classes/Utility/TranslateUtility.php | 3 ++- .../ViewHelpers/GetOptionValueViewHelper.php | 2 +- Classes/ViewHelpers/Math/AddViewHelper.php | 4 +-- Classes/ViewHelpers/PermissionViewHelper.php | 4 +-- .../Schedule/ParseDayOptionViewHelper.php | 4 +-- .../Schedule/SplitDayOptionsViewHelper.php | 4 +-- Classes/ViewHelpers/SvgViewHelper.php | 16 ++++++++--- 20 files changed, 81 insertions(+), 54 deletions(-) diff --git a/Classes/Controller/PollController.php b/Classes/Controller/PollController.php index ae56d7a..2265773 100644 --- a/Classes/Controller/PollController.php +++ b/Classes/Controller/PollController.php @@ -100,7 +100,7 @@ class PollController extends ActionController protected PersistenceManagerInterface $persistenceManager; - public function injectPersistenceManager(PersistenceManagerInterface $persistenceManager) + public function injectPersistenceManager(PersistenceManagerInterface $persistenceManager): void { $this->persistenceManager = $persistenceManager; } @@ -616,7 +616,7 @@ public function createAction( BasePoll $poll, bool $publishDirectly, bool $acceptTerms = false - ) { + ): void { if ($poll->isSimplePoll()) { $this->pollPermission->isAllowed($poll, 'newSimplePoll', true); } else { @@ -650,7 +650,7 @@ public function createAction( AbstractMessage::OK ); if ($publishDirectly) { - return (new ForwardResponse('publish')) + (new ForwardResponse('publish')) ->withControllerName('Poll') ->withExtensionName('t3oodle') ->withArguments(['poll' => $poll]); diff --git a/Classes/Domain/Model/BasePoll.php b/Classes/Domain/Model/BasePoll.php index 1d5d6f8..1e9ac96 100644 --- a/Classes/Domain/Model/BasePoll.php +++ b/Classes/Domain/Model/BasePoll.php @@ -314,7 +314,7 @@ public function removeOption(Option $optionToRemove): void * * @return \TYPO3\CMS\Extbase\Persistence\ObjectStorage $options */ - public function getOptions($skipMarkedToDeleted = false): \TYPO3\CMS\Extbase\Persistence\ObjectStorage + public function getOptions(bool $skipMarkedToDeleted = false): \TYPO3\CMS\Extbase\Persistence\ObjectStorage { if ($skipMarkedToDeleted) { /** @var \TYPO3\CMS\Extbase\Persistence\ObjectStorage $options */ @@ -482,7 +482,7 @@ public function removeVote(Vote $voteToRemove): void } /** - * @return \TYPO3\CMS\Extbase\Persistence\ObjectStorage|Vote[] + * @return \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\FGTCLB\T3oodle\Domain\Model\Vote> */ public function getVotes(): \TYPO3\CMS\Extbase\Persistence\ObjectStorage { @@ -625,7 +625,9 @@ public function areOptionsModified(): bool return false; } - + /** + * @return \FGTCLB\T3oodle\Domain\Model\Option[] + */ public function getAvailableOptions(): array { if (!$this->getSettingMaxVotesPerOption()) { diff --git a/Classes/Domain/Repository/PollRepository.php b/Classes/Domain/Repository/PollRepository.php index 067bf33..085e957 100644 --- a/Classes/Domain/Repository/PollRepository.php +++ b/Classes/Domain/Repository/PollRepository.php @@ -13,6 +13,7 @@ use FGTCLB\T3oodle\Service\UserService; use FGTCLB\T3oodle\Utility\UserIdentUtility; use Psr\EventDispatcher\EventDispatcherInterface; +use TYPO3\CMS\Core\Database\Connection; use TYPO3\CMS\Core\Database\ConnectionPool; use TYPO3\CMS\Core\Utility\GeneralUtility; use TYPO3\CMS\Extbase\Object\ObjectManagerInterface; @@ -26,8 +27,9 @@ class PollRepository extends \TYPO3\CMS\Extbase\Persistence\Repository protected $eventDispatcher; private UserService $userService; + /** - * @var string[] Show unpublished first, then order by publishDate + * @var array Show unpublished first, then order by publishDate */ protected $defaultOrderings = [ 'isPublished' => 'ASC', @@ -83,7 +85,7 @@ public function findPolls( $andConstraints[] = $query->equals('isPublished', true); } - if (!empty($orConstraints)) { + if ($orConstraints !== []) { $andConstraints[] = $query->logicalOr($orConstraints); } @@ -108,13 +110,26 @@ public function getPollTypeByUid(int $poll): string { /** @var ConnectionPool $pool */ $pool = GeneralUtility::makeInstance(ConnectionPool::class); - $result = $pool->getQueryBuilderForTable('tx_t3oodle_domain_model_poll') + $queryBuilder = $pool->getQueryBuilderForTable('tx_t3oodle_domain_model_poll'); + $result = $queryBuilder ->select('type') ->from('tx_t3oodle_domain_model_poll') - ->where('uid = ' . $poll) - ->execute() - ->fetch(); + ->where($queryBuilder->expr()->eq( + 'uid', + $queryBuilder->createNamedParameter($poll, Connection::PARAM_INT) + )) + ->executeQuery() + ->fetchAssociative(); return $result['type']; } + + public function countBySlug(string $slug): int + { + $query = $this->createQuery(); + return $query + ->matching($query->equals('slug', $slug)) + ->execute() + ->count(); + } } diff --git a/Classes/Event/EditSuggestionEvent.php b/Classes/Event/EditSuggestionEvent.php index 3c29adb..1ba27b6 100644 --- a/Classes/Event/EditSuggestionEvent.php +++ b/Classes/Event/EditSuggestionEvent.php @@ -17,7 +17,7 @@ final class EditSuggestionEvent private array $settings; private PollController $caller; - public function __construct(Option $option, SuggestionDto $suggestionDto, array $settings, ViewInterface $view, PollController $caller) + public function __construct(Option $option, SuggestionDto $suggestionDto, array $settings, $view, PollController $caller) { $this->option = $option; $this->suggestionDto = $suggestionDto; diff --git a/Classes/Event/NewPollEvent.php b/Classes/Event/NewPollEvent.php index 2ad65e2..2229045 100644 --- a/Classes/Event/NewPollEvent.php +++ b/Classes/Event/NewPollEvent.php @@ -17,7 +17,7 @@ final class NewPollEvent private ViewInterface $view; private PollController $caller; - public function __construct(Poll $poll, bool $publishDirectly, array $newOptions, array $settings, ViewInterface $view, PollController $caller) + public function __construct(Poll $poll, bool $publishDirectly, array $newOptions, array $settings, $view, PollController $caller) { $this->poll = $poll; $this->publishDirectly = $publishDirectly; diff --git a/Classes/Event/NewSuggestionEvent.php b/Classes/Event/NewSuggestionEvent.php index 37b3501..7346fe0 100644 --- a/Classes/Event/NewSuggestionEvent.php +++ b/Classes/Event/NewSuggestionEvent.php @@ -17,7 +17,7 @@ final class NewSuggestionEvent private PollController $caller; private SuggestionDto $suggestionDto; - public function __construct(Poll $poll, SuggestionDto $suggestionDto, array $settings, ViewInterface $view, PollController $caller) + public function __construct(Poll $poll, SuggestionDto $suggestionDto, array $settings, $view, PollController $caller) { $this->poll = $poll; $this->suggestionDto = $suggestionDto; diff --git a/Classes/Event/Permission/PermissionCheckEvent.php b/Classes/Event/Permission/PermissionCheckEvent.php index abafc35..f090451 100644 --- a/Classes/Event/Permission/PermissionCheckEvent.php +++ b/Classes/Event/Permission/PermissionCheckEvent.php @@ -10,7 +10,7 @@ final class PermissionCheckEvent private ?array $arguments = []; private mixed $caller; - public function __construct(bool $currentStatus, ?array $arguments, $caller) + public function __construct(bool $currentStatus, ?array $arguments, mixed $caller) { $this->currentStatus = $currentStatus; $this->arguments = $arguments; @@ -22,7 +22,7 @@ public function getCurrentStatus(): bool return $this->currentStatus; } - public function setCurrentStatus($status): void + public function setCurrentStatus(bool $status = null): void { $status ?? $this->currentStatus; } diff --git a/Classes/Event/PollRepository/FindPollsEvent.php b/Classes/Event/PollRepository/FindPollsEvent.php index 9d734db..2ae5f37 100644 --- a/Classes/Event/PollRepository/FindPollsEvent.php +++ b/Classes/Event/PollRepository/FindPollsEvent.php @@ -9,9 +9,9 @@ final class FindPollsEvent { - private $constraints; - private $query; - private $caller; + private array $constraints; + private QueryInterface $query; + private PollRepository $caller; public function __construct( array $constraints, @@ -32,7 +32,7 @@ public function getQuery(): QueryInterface { return $this->query; } - public function setConstraints($constraints): void + public function setConstraints(array $constraints): void { $this->constraints = $constraints; } diff --git a/Classes/Event/ShowPollEvent.php b/Classes/Event/ShowPollEvent.php index e525a17..737e4c4 100644 --- a/Classes/Event/ShowPollEvent.php +++ b/Classes/Event/ShowPollEvent.php @@ -45,7 +45,7 @@ public function getView(): ViewInterface return $this->view; } - public function getCaller() + public function getCaller(): PollController { return $this->caller; } diff --git a/Classes/Event/VotePollEvent.php b/Classes/Event/VotePollEvent.php index e28a12c..faaddf2 100644 --- a/Classes/Event/VotePollEvent.php +++ b/Classes/Event/VotePollEvent.php @@ -47,4 +47,9 @@ public function getCaller(): PollController { return $this->caller; } + + public function getSettings(): array + { + return $this->settings; + } } diff --git a/Classes/EventListener/UpdatePollSlugListener.php b/Classes/EventListener/UpdatePollSlugListener.php index c834e85..5553ff9 100644 --- a/Classes/EventListener/UpdatePollSlugListener.php +++ b/Classes/EventListener/UpdatePollSlugListener.php @@ -83,7 +83,10 @@ public function beforeUpdate( */ protected function updatePollSlug(BasePoll $poll): void { - $slugUtility = GeneralUtility::makeInstance(SlugUtility::class, 'tx_t3oodle_domain_model_poll', 'slug'); + $slugUtility = new SlugUtility( + 'tx_t3oodle_domain_model_poll', + 'slug' + ); // Create slug and update created entity if ($poll->getVisibility() === \FGTCLB\T3oodle\Domain\Enumeration\Visibility::NOT_LISTED) { diff --git a/Classes/Traits/Model/DynamicUserProperties.php b/Classes/Traits/Model/DynamicUserProperties.php index d6a077c..7b45e9b 100644 --- a/Classes/Traits/Model/DynamicUserProperties.php +++ b/Classes/Traits/Model/DynamicUserProperties.php @@ -10,6 +10,7 @@ * | (c) 2020-2021 Armin Vieweg */ use FGTCLB\T3oodle\Utility\SettingsUtility; +use TYPO3\CMS\Core\Database\Connection; use TYPO3\CMS\Core\Utility\GeneralUtility; use TYPO3\CMS\Extbase\Domain\Model\FrontendUser; @@ -69,8 +70,13 @@ private function getUserRow(int $uid): ?array self::$userRowCache[$uid] = $queryBuilder ->select('uid', self::$typoscriptSettings['frontendUserNameField']) ->from('fe_users') - ->where($queryBuilder->expr()->eq('uid', $uid)) - ->execute()->fetch(\PDO::FETCH_ASSOC); + ->where($queryBuilder->expr()->eq( + 'uid', + $queryBuilder->createNamedParameter($uid, Connection::PARAM_INT) + )) + ->setMaxResults(1) + ->executeQuery() + ->fetchAssociative(); return self::$userRowCache[$uid]; } diff --git a/Classes/Utility/SlugUtility.php b/Classes/Utility/SlugUtility.php index 923f72c..ac0970e 100644 --- a/Classes/Utility/SlugUtility.php +++ b/Classes/Utility/SlugUtility.php @@ -14,25 +14,10 @@ class SlugUtility { - /** - * @var string - */ - private $tableName; - - /** - * @var string - */ - private $fieldName; - - /** - * @var SlugHelper - */ - private $slugHelper; + private SlugHelper $slugHelper; public function __construct(string $tableName, string $fieldName) { - $this->tableName = $tableName; - $this->fieldName = $fieldName; $this->slugHelper = GeneralUtility::makeInstance( SlugHelper::class, $tableName, diff --git a/Classes/Utility/TranslateUtility.php b/Classes/Utility/TranslateUtility.php index d8f9b2e..ffdc428 100644 --- a/Classes/Utility/TranslateUtility.php +++ b/Classes/Utility/TranslateUtility.php @@ -18,7 +18,8 @@ final class TranslateUtility public static function translate(string $key, array $arguments = [], string $default = ''): string { $translation = LocalizationUtility::translate($key, self::EXTENSION_NAME, $arguments); - if (!$translation || empty($translation)) { + + if (!$translation) { return $default; } diff --git a/Classes/ViewHelpers/GetOptionValueViewHelper.php b/Classes/ViewHelpers/GetOptionValueViewHelper.php index b35bdea..cf10046 100644 --- a/Classes/ViewHelpers/GetOptionValueViewHelper.php +++ b/Classes/ViewHelpers/GetOptionValueViewHelper.php @@ -19,7 +19,7 @@ class GetOptionValueViewHelper extends AbstractViewHelper { use CompileWithRenderStatic; - public function initializeArguments() + public function initializeArguments(): void { $this->registerArgument('vote', 'object', 'Vote object', false); $this->registerArgument('option', 'object', 'Option object', true); diff --git a/Classes/ViewHelpers/Math/AddViewHelper.php b/Classes/ViewHelpers/Math/AddViewHelper.php index 95d149c..91e5aef 100644 --- a/Classes/ViewHelpers/Math/AddViewHelper.php +++ b/Classes/ViewHelpers/Math/AddViewHelper.php @@ -17,7 +17,7 @@ class AddViewHelper extends AbstractViewHelper { use CompileWithRenderStatic; - public function initializeArguments() + public function initializeArguments(): void { $this->registerArgument('subject', 'int', 'Subject'); $this->registerArgument('number', 'int', 'Number to add to subject', true); @@ -27,7 +27,7 @@ public static function renderStatic( array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext - ) { + ): int { $subject = $arguments['subject'] ?? $renderChildrenClosure(); return (int)$subject + (int)$arguments['number']; diff --git a/Classes/ViewHelpers/PermissionViewHelper.php b/Classes/ViewHelpers/PermissionViewHelper.php index ad8a5e6..b9c4cd6 100644 --- a/Classes/ViewHelpers/PermissionViewHelper.php +++ b/Classes/ViewHelpers/PermissionViewHelper.php @@ -28,7 +28,7 @@ class PermissionViewHelper extends AbstractViewHelper */ private static $permission; - public function initializeArguments() + public function initializeArguments(): void { $this->registerArgument('permissionClassName', 'string', '', false, PollPermission::class); $this->registerArgument('poll', 'object', 'Poll object', false); @@ -55,7 +55,7 @@ public static function renderStatic( array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext - ) { + ): bool { self::init($arguments, $renderingContext); $poll = $arguments['poll'] ?? $renderChildrenClosure(); diff --git a/Classes/ViewHelpers/Schedule/ParseDayOptionViewHelper.php b/Classes/ViewHelpers/Schedule/ParseDayOptionViewHelper.php index b8004d8..bc04c07 100644 --- a/Classes/ViewHelpers/Schedule/ParseDayOptionViewHelper.php +++ b/Classes/ViewHelpers/Schedule/ParseDayOptionViewHelper.php @@ -18,7 +18,7 @@ class ParseDayOptionViewHelper extends AbstractViewHelper { use CompileWithRenderStatic; - public function initializeArguments() + public function initializeArguments(): void { $this->registerArgument('value', 'string', 'Date with option as string', false); } @@ -27,7 +27,7 @@ public static function renderStatic( array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext - ) { + ): array { $value = $arguments['value'] ?? (string)$renderChildrenClosure(); return ScheduleOptionUtility::parseOptionName($value); diff --git a/Classes/ViewHelpers/Schedule/SplitDayOptionsViewHelper.php b/Classes/ViewHelpers/Schedule/SplitDayOptionsViewHelper.php index 4d71e19..1e25aac 100644 --- a/Classes/ViewHelpers/Schedule/SplitDayOptionsViewHelper.php +++ b/Classes/ViewHelpers/Schedule/SplitDayOptionsViewHelper.php @@ -20,7 +20,7 @@ class SplitDayOptionsViewHelper extends AbstractViewHelper { use CompileWithRenderStatic; - public function initializeArguments() + public function initializeArguments(): void { $this->registerArgument('get', 'string', '"dates" or "options" allowed', true); $this->registerArgument('options', 'array', 'Iterable with options', false); @@ -30,7 +30,7 @@ public static function renderStatic( array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext - ) { + ): array { $options = $arguments['options'] ?? $renderChildrenClosure(); if (!$options || !is_iterable($options)) { throw new \InvalidArgumentException( diff --git a/Classes/ViewHelpers/SvgViewHelper.php b/Classes/ViewHelpers/SvgViewHelper.php index 6fbd5f7..269627d 100644 --- a/Classes/ViewHelpers/SvgViewHelper.php +++ b/Classes/ViewHelpers/SvgViewHelper.php @@ -28,7 +28,7 @@ class SvgViewHelper extends AbstractViewHelper */ protected static $cache = []; - public function initializeArguments() + public function initializeArguments(): void { $this->registerArgument('path', 'string', 'SVG path, relative to Resources/Public/', true); $this->registerArgument('size', 'string', 'SVG size in pixel', false, ''); @@ -37,12 +37,22 @@ public function initializeArguments() $this->registerArgument('title', 'string', 'Optional title', false, ''); } + /** + * Return array element by key. + * + * @param array $arguments + * @param \Closure $renderChildrenClosure + * @param RenderingContextInterface $renderingContext + * @throws \RuntimeException + * @return string + */ public static function renderStatic( array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext - ) { - $extensionName = $renderingContext->getControllerContext()->getRequest()->getControllerExtensionName(); + ): string { + $request = $renderingContext->getRequest(); + $extensionName = $request->getControllerExtensionName(); $uri = 'EXT:' . GeneralUtility::camelCaseToLowerCaseUnderscored($extensionName) . '/Resources/Public/' . $arguments['path']; $path = GeneralUtility::getFileAbsFileName($uri);