From 87f5c1f3fd3a3604ca576e5fab2e0da44fac2565 Mon Sep 17 00:00:00 2001 From: Andreas Schempp Date: Wed, 30 Nov 2022 12:06:55 +0100 Subject: [PATCH] CS & argument and return types --- src/EventListener/AbstractTableListener.php | 13 +-- .../BackendView/AbstractViewListener.php | 14 +-- .../BackendView/ArticleViewListener.php | 26 +---- .../BackendView/PageViewListener.php | 18 +-- .../DataContainer/CalendarEventsListener.php | 19 +--- .../DataContainer/FaqListener.php | 19 +--- .../MissingLanguageIconListener.php | 20 +--- .../DataContainer/NewsListener.php | 19 +--- .../DataContainer/PageFieldsListener.php | 2 +- .../DataContainer/ParentTableListener.php | 7 +- .../DataContainer/UserLabelsListener.php | 2 +- .../Navigation/AbstractNavigationListener.php | 7 +- .../Navigation/ArticleNavigationListener.php | 9 +- .../Navigation/CalendarNavigationListener.php | 13 +-- .../Navigation/FaqNavigationListener.php | 16 +-- .../Navigation/NewsNavigationListener.php | 16 +-- src/FrontendModule/ChangeLanguageModule.php | 30 +---- src/Helper/LanguageText.php | 27 +---- src/Language.php | 19 +--- src/Navigation/NavigationFactory.php | 13 +-- src/Navigation/NavigationItem.php | 105 ++++-------------- src/Navigation/UrlParameterBag.php | 71 ++---------- src/PageFinder.php | 38 ++----- 23 files changed, 104 insertions(+), 419 deletions(-) diff --git a/src/EventListener/AbstractTableListener.php b/src/EventListener/AbstractTableListener.php index 36c7f46..2bfb2ee 100644 --- a/src/EventListener/AbstractTableListener.php +++ b/src/EventListener/AbstractTableListener.php @@ -6,17 +6,12 @@ abstract class AbstractTableListener { - /** - * @var string - */ - protected $table; + protected string $table; /** * Constructor. - * - * @param string $table */ - public function __construct($table) + public function __construct(string $table) { $this->table = $table; } @@ -28,10 +23,8 @@ abstract public function register(); /** * Gets the table name for this listener. - * - * @return string */ - protected function getTable() + protected function getTable(): string { return $this->table; } diff --git a/src/EventListener/BackendView/AbstractViewListener.php b/src/EventListener/BackendView/AbstractViewListener.php index 55996cd..9c8e6db 100644 --- a/src/EventListener/BackendView/AbstractViewListener.php +++ b/src/EventListener/BackendView/AbstractViewListener.php @@ -14,19 +14,9 @@ abstract class AbstractViewListener extends AbstractTableListener { - /** - * @var DataContainer - */ - protected $dataContainer; + protected DataContainer $dataContainer; + protected PageFinder $pageFinder; - /** - * @var PageFinder - */ - protected $pageFinder; - - /** - * {@inheritdoc} - */ public function __construct($table) { parent::__construct($table); diff --git a/src/EventListener/BackendView/ArticleViewListener.php b/src/EventListener/BackendView/ArticleViewListener.php index f40f0b8..67a68d4 100644 --- a/src/EventListener/BackendView/ArticleViewListener.php +++ b/src/EventListener/BackendView/ArticleViewListener.php @@ -16,14 +16,11 @@ class ArticleViewListener extends AbstractViewListener { /** - * @var ArticleModel + * @var ArticleModel|false|null */ private $currentArticle = false; - /** - * {@inheritdoc} - */ - protected function isSupported() + protected function isSupported(): bool { return 'article' === (string) Input::get('do') && ( @@ -32,10 +29,7 @@ protected function isSupported() ); } - /** - * {@inheritdoc} - */ - protected function getCurrentPage() + protected function getCurrentPage(): ?PageModel { if (false === $this->currentArticle) { if (Input::get('table') === $this->getTable() && !empty(Input::get('act'))) { @@ -56,10 +50,7 @@ protected function getCurrentPage() return PageModel::findWithDetails($this->currentArticle->pid); } - /** - * {@inheritdoc} - */ - protected function getAvailableLanguages(PageModel $page) + protected function getAvailableLanguages(PageModel $page): array { $options = []; $masterRoot = $this->pageFinder->findMasterRootForPage($page); @@ -103,11 +94,6 @@ protected function getAvailableLanguages(PageModel $page) return $options; } - /** - * {@inheritdoc} - * - * @throws \InvalidArgumentException - */ protected function doSwitchView($id): void { $uri = Uri::createFromString(System::getContainer()->get('request_stack')->getCurrentRequest()->getUri()); @@ -118,11 +104,9 @@ protected function doSwitchView($id): void } /** - * @param int $articleId - * * @return array */ - private function findArticlesForPage(PageModel $page, $articleId): array + private function findArticlesForPage(PageModel $page, int $articleId): array { $articles = ArticleModel::findBy( [ diff --git a/src/EventListener/BackendView/PageViewListener.php b/src/EventListener/BackendView/PageViewListener.php index 1fd7dc1..436985b 100644 --- a/src/EventListener/BackendView/PageViewListener.php +++ b/src/EventListener/BackendView/PageViewListener.php @@ -13,18 +13,12 @@ class PageViewListener extends AbstractViewListener { - /** - * {@inheritdoc} - */ - protected function isSupported() + protected function isSupported(): bool { return 'page' === Input::get('do') || ('article' === Input::get('do') && 'edit' !== Input::get('act')); } - /** - * {@inheritdoc} - */ - protected function getCurrentPage() + protected function getCurrentPage(): ?PageModel { $node = System::getContainer()->get('request_stack')->getSession()->getBag('contao_backend')->get('tl_page_node'); @@ -35,10 +29,7 @@ protected function getCurrentPage() return PageModel::findByPk($node); } - /** - * {@inheritdoc} - */ - protected function getAvailableLanguages(PageModel $page) + protected function getAvailableLanguages(PageModel $page): array { $options = []; @@ -50,9 +41,6 @@ protected function getAvailableLanguages(PageModel $page) return $options; } - /** - * {@inheritdoc} - */ protected function doSwitchView($id): void { $requestStack = System::getContainer()->get('request_stack'); diff --git a/src/EventListener/DataContainer/CalendarEventsListener.php b/src/EventListener/DataContainer/CalendarEventsListener.php index c54bdea..f95eeb9 100644 --- a/src/EventListener/DataContainer/CalendarEventsListener.php +++ b/src/EventListener/DataContainer/CalendarEventsListener.php @@ -4,36 +4,23 @@ namespace Terminal42\ChangeLanguage\EventListener\DataContainer; -use Contao\CalendarEventsModel; use Contao\Date; use Contao\Model; use Contao\Model\Collection; class CalendarEventsListener extends AbstractChildTableListener { - /** - * {@inheritdoc} - */ - protected function getTitleField() + protected function getTitleField(): string { return 'title'; } - /** - * {@inheritdoc} - */ - protected function getSorting() + protected function getSorting(): string { return 'startTime DESC'; } - /** - * {@inheritdoc} - * - * @param CalendarEventsModel $current - * @param Collection $models - */ - protected function formatOptions(Model $current, Collection $models) + protected function formatOptions(Model $current, Collection $models): array { $options = []; diff --git a/src/EventListener/DataContainer/FaqListener.php b/src/EventListener/DataContainer/FaqListener.php index cf8f1b1..7fe8309 100644 --- a/src/EventListener/DataContainer/FaqListener.php +++ b/src/EventListener/DataContainer/FaqListener.php @@ -4,35 +4,22 @@ namespace Terminal42\ChangeLanguage\EventListener\DataContainer; -use Contao\FaqModel; use Contao\Model; use Contao\Model\Collection; class FaqListener extends AbstractChildTableListener { - /** - * {@inheritdoc} - */ - protected function getTitleField() + protected function getTitleField(): string { return 'question'; } - /** - * {@inheritdoc} - */ - protected function getSorting() + protected function getSorting(): string { return 'sorting'; } - /** - * {@inheritdoc} - * - * @param FaqModel $current - * @param Collection $models - */ - protected function formatOptions(Model $current, Collection $models) + protected function formatOptions(Model $current, Collection $models): array { $options = []; diff --git a/src/EventListener/DataContainer/MissingLanguageIconListener.php b/src/EventListener/DataContainer/MissingLanguageIconListener.php index f38866d..e4a3e62 100644 --- a/src/EventListener/DataContainer/MissingLanguageIconListener.php +++ b/src/EventListener/DataContainer/MissingLanguageIconListener.php @@ -49,10 +49,8 @@ public function register($table): void * Adds missing translation warning to page tree. * * @param mixed $previousResult - * - * @return string */ - public function onPageLabel(array $args, $previousResult = null) + private function onPageLabel(array $args, $previousResult = null): string { [$row, $label] = $args; @@ -95,10 +93,8 @@ public function onPageLabel(array $args, $previousResult = null) * Adds missing translation warning to article tree. * * @param mixed $previousResult - * - * @return string */ - public function onArticleLabel(array $args, $previousResult = null) + private function onArticleLabel(array $args, $previousResult = null): string { [$row, $label] = $args; @@ -124,10 +120,8 @@ public function onArticleLabel(array $args, $previousResult = null) * Generate missing translation warning for news child records. * * @param mixed $previousResult - * - * @return string */ - public function onNewsChildRecords(array $args, $previousResult = null) + private function onNewsChildRecords(array $args, $previousResult = null): string { $row = $args[0]; $label = (string) $previousResult; @@ -153,10 +147,8 @@ public function onNewsChildRecords(array $args, $previousResult = null) * Generate missing translation warning for calendar events child records. * * @param mixed $previousResult - * - * @return string */ - public function onCalendarEventChildRecords(array $args, $previousResult = null) + private function onCalendarEventChildRecords(array $args, $previousResult = null): string { $row = $args[0]; $label = (string) $previousResult; @@ -178,10 +170,8 @@ public function onCalendarEventChildRecords(array $args, $previousResult = null) * Generate missing translation warning for faq child records. * * @param mixed $previousResult - * - * @return string */ - public function onFaqChildRecords(array $args, $previousResult = null) + private function onFaqChildRecords(array $args, $previousResult = null): string { $row = $args[0]; $label = (string) $previousResult; diff --git a/src/EventListener/DataContainer/NewsListener.php b/src/EventListener/DataContainer/NewsListener.php index 4232fd4..1292eea 100644 --- a/src/EventListener/DataContainer/NewsListener.php +++ b/src/EventListener/DataContainer/NewsListener.php @@ -7,33 +7,20 @@ use Contao\Date; use Contao\Model; use Contao\Model\Collection; -use Contao\NewsModel; class NewsListener extends AbstractChildTableListener { - /** - * {@inheritdoc} - */ - protected function getTitleField() + protected function getTitleField(): string { return 'headline'; } - /** - * {@inheritdoc} - */ - protected function getSorting() + protected function getSorting(): string { return 'date DESC, time DESC'; } - /** - * {@inheritdoc} - * - * @param NewsModel $current - * @param Collection $models - */ - protected function formatOptions(Model $current, Collection $models) + protected function formatOptions(Model $current, Collection $models): array { $sameDay = $GLOBALS['TL_LANG']['tl_news']['sameDay']; $otherDay = $GLOBALS['TL_LANG']['tl_news']['otherDay']; diff --git a/src/EventListener/DataContainer/PageFieldsListener.php b/src/EventListener/DataContainer/PageFieldsListener.php index ced0dec..9894f7c 100644 --- a/src/EventListener/DataContainer/PageFieldsListener.php +++ b/src/EventListener/DataContainer/PageFieldsListener.php @@ -93,7 +93,7 @@ public function onSaveLanguageMain($value, DataContainer $dc) * * @return array */ - public function onLanguageRootOptions(DataContainer $dc) + public function onLanguageRootOptions(DataContainer $dc): array { /** @var array $pages */ $pages = PageModel::findBy( diff --git a/src/EventListener/DataContainer/ParentTableListener.php b/src/EventListener/DataContainer/ParentTableListener.php index fb1b774..ba808bb 100644 --- a/src/EventListener/DataContainer/ParentTableListener.php +++ b/src/EventListener/DataContainer/ParentTableListener.php @@ -14,12 +14,7 @@ class ParentTableListener { private string $table; - /** - * Constructor. - * - * @param string $table - */ - public function __construct($table) + public function __construct(string $table) { $this->table = $table; } diff --git a/src/EventListener/DataContainer/UserLabelsListener.php b/src/EventListener/DataContainer/UserLabelsListener.php index be6cd14..bdeb959 100644 --- a/src/EventListener/DataContainer/UserLabelsListener.php +++ b/src/EventListener/DataContainer/UserLabelsListener.php @@ -19,7 +19,7 @@ public function __construct(Connection $connection) $this->connection = $connection; } - public function __invoke() + public function __invoke(): array { $pages = $this->connection->fetchAllAssociative("SELECT id, title FROM tl_page WHERE type='root' AND (fallback='' OR languageRoot!=0) ORDER BY pid, sorting"); diff --git a/src/EventListener/Navigation/AbstractNavigationListener.php b/src/EventListener/Navigation/AbstractNavigationListener.php index b630e8f..e90fd3c 100644 --- a/src/EventListener/Navigation/AbstractNavigationListener.php +++ b/src/EventListener/Navigation/AbstractNavigationListener.php @@ -72,13 +72,8 @@ public function onChangelanguageNavigation(ChangelanguageNavigationEvent $event) /** * Adds publishing conditions to Model query columns if backend user is not logged in. - * - * @param string $table - * @param bool $addStartStop - * - * @return array */ - protected function addPublishedConditions(array $columns, $table, $addStartStop = true) + protected function addPublishedConditions(array $columns, string $table, bool $addStartStop = true): array { if (true !== BE_USER_LOGGED_IN) { $columns[] = "$table.published='1'"; diff --git a/src/EventListener/Navigation/ArticleNavigationListener.php b/src/EventListener/Navigation/ArticleNavigationListener.php index 07eeaf3..5713582 100644 --- a/src/EventListener/Navigation/ArticleNavigationListener.php +++ b/src/EventListener/Navigation/ArticleNavigationListener.php @@ -7,6 +7,7 @@ use Contao\ArticleModel; use Contao\Database; use Contao\Date; +use Contao\PageModel; use Terminal42\ChangeLanguage\Event\ChangelanguageNavigationEvent; use Terminal42\ChangeLanguage\PageFinder; @@ -62,10 +63,8 @@ public function onChangelanguageNavigation(ChangelanguageNavigationEvent $event) * @param int $targetRootId * @param bool $currentIsFallback * @param bool $targetIsFallback - * - * @return ArticleModel|null */ - private function findTargetArticle(ArticleModel $currentArticle, $targetRootId, $currentIsFallback, $targetIsFallback) + private function findTargetArticle(ArticleModel $currentArticle, $targetRootId, $currentIsFallback, $targetIsFallback): ?ArticleModel { // If the target root is fallback, the article ID will match our current "languageMain" if ($targetIsFallback) { @@ -91,10 +90,8 @@ private function findTargetArticle(ArticleModel $currentArticle, $targetRootId, /** * Find a published article with additional conditions. - * - * @return ArticleModel|null */ - private function findPublishedArticle(array $columns, array $values = [], array $options = []) + private function findPublishedArticle(array $columns, array $values = [], array $options = []): ?ArticleModel { if (true !== BE_USER_LOGGED_IN) { $time = Date::floorToMinute(); diff --git a/src/EventListener/Navigation/CalendarNavigationListener.php b/src/EventListener/Navigation/CalendarNavigationListener.php index aa54224..2e0e720 100644 --- a/src/EventListener/Navigation/CalendarNavigationListener.php +++ b/src/EventListener/Navigation/CalendarNavigationListener.php @@ -13,18 +13,12 @@ */ class CalendarNavigationListener extends AbstractNavigationListener { - /** - * {@inheritdoc} - */ - protected function getUrlKey() + protected function getUrlKey(): string { return 'events'; } - /** - * {@inheritdoc} - */ - protected function findCurrent() + protected function findCurrent(): ?CalendarEventsModel { $alias = $this->getAutoItem(); @@ -42,9 +36,6 @@ protected function findCurrent() return CalendarEventsModel::findPublishedByParentAndIdOrAlias($alias, $calendars->fetchEach('id')); } - /** - * {@inheritdoc} - */ protected function findPublishedBy(array $columns, array $values = [], array $options = []) { return CalendarEventsModel::findOneBy( diff --git a/src/EventListener/Navigation/FaqNavigationListener.php b/src/EventListener/Navigation/FaqNavigationListener.php index 242524c..b42f85e 100644 --- a/src/EventListener/Navigation/FaqNavigationListener.php +++ b/src/EventListener/Navigation/FaqNavigationListener.php @@ -6,24 +6,19 @@ use Contao\FaqCategoryModel; use Contao\FaqModel; +use Contao\PageModel; /** * Translate URL parameters for faq items. */ class FaqNavigationListener extends AbstractNavigationListener { - /** - * {@inheritdoc} - */ - protected function getUrlKey() + protected function getUrlKey(): string { return 'items'; } - /** - * {@inheritdoc} - */ - protected function findCurrent() + protected function findCurrent(): ?FaqModel { $alias = $this->getAutoItem(); @@ -41,10 +36,7 @@ protected function findCurrent() return FaqModel::findPublishedByParentAndIdOrAlias($alias, $calendars->fetchEach('id')); } - /** - * {@inheritdoc} - */ - protected function findPublishedBy(array $columns, array $values = [], array $options = []) + protected function findPublishedBy(array $columns, array $values = [], array $options = []): ?FaqModel { return FaqModel::findOneBy( $this->addPublishedConditions($columns, FaqModel::getTable(), false), diff --git a/src/EventListener/Navigation/NewsNavigationListener.php b/src/EventListener/Navigation/NewsNavigationListener.php index 6fae62c..c152ec3 100644 --- a/src/EventListener/Navigation/NewsNavigationListener.php +++ b/src/EventListener/Navigation/NewsNavigationListener.php @@ -6,24 +6,19 @@ use Contao\NewsArchiveModel; use Contao\NewsModel; +use Contao\PageModel; /** * Translate URL parameters for news items. */ class NewsNavigationListener extends AbstractNavigationListener { - /** - * {@inheritdoc} - */ - protected function getUrlKey() + protected function getUrlKey(): string { return 'items'; } - /** - * {@inheritdoc} - */ - protected function findCurrent() + protected function findCurrent(): ?NewsModel { $alias = $this->getAutoItem(); @@ -44,10 +39,7 @@ protected function findCurrent() return NewsModel::findPublishedByParentAndIdOrAlias($alias, $archives->fetchEach('id'), $options); } - /** - * {@inheritdoc} - */ - protected function findPublishedBy(array $columns, array $values = [], array $options = []) + protected function findPublishedBy(array $columns, array $values = [], array $options = []): ?NewsModel { return NewsModel::findOneBy( $this->addPublishedConditions($columns, NewsModel::getTable()), diff --git a/src/FrontendModule/ChangeLanguageModule.php b/src/FrontendModule/ChangeLanguageModule.php index 448d29f..7ad832c 100755 --- a/src/FrontendModule/ChangeLanguageModule.php +++ b/src/FrontendModule/ChangeLanguageModule.php @@ -47,10 +47,7 @@ public function getAlternateLinks() return self::$alternateLinks; } - /** - * {@inheritdoc} - */ - public function generate() + public function generate(): string { $request = System::getContainer()->get('request_stack')->getCurrentRequest(); $scopeMatcher = System::getContainer()->get('contao.routing.scope_matcher'); @@ -75,9 +72,6 @@ public function generate() return '' === (string) $this->Template->items ? '' : $buffer; } - /** - * {@inheritdoc} - */ protected function compile(): void { $currentPage = $this->getCurrentPage(); @@ -138,10 +132,8 @@ protected function compile(): void /** * Generates array suitable for nav_default template. - * - * @return array */ - protected function generateTemplateArray(NavigationItem $item, UrlParameterBag $urlParameterBag) + protected function generateTemplateArray(NavigationItem $item, UrlParameterBag $urlParameterBag): array { return [ 'isActive' => $item->isCurrentPage(), @@ -159,10 +151,7 @@ protected function generateTemplateArray(NavigationItem $item, UrlParameterBag $ ]; } - /** - * @return string - */ - protected function generateNavigationTemplate(array $items) + protected function generateNavigationTemplate(array $items): string { $objTemplate = new FrontendTemplate($this->navigationTpl ?: 'nav_default'); @@ -173,10 +162,7 @@ protected function generateNavigationTemplate(array $items) return $objTemplate->parse(); } - /** - * @return PageModel - */ - protected function getCurrentPage() + protected function getCurrentPage(): PageModel { global $objPage; @@ -187,10 +173,8 @@ protected function getCurrentPage() * Creates an UrlParameterBag from the current environment. * * @param array $queryParameters An array of query parameters to keep - * - * @return UrlParameterBag */ - protected function createUrlParameterBag(array $queryParameters = []) + protected function createUrlParameterBag(array $queryParameters = []): UrlParameterBag { $attributes = []; $query = []; @@ -225,10 +209,8 @@ protected function createUrlParameterBag(array $queryParameters = []) /** * Returns false if navigation item should be skipped. - * - * @return bool */ - protected function executeHook(NavigationItem $navigationItem, UrlParameterBag $urlParameterBag) + protected function executeHook(NavigationItem $navigationItem, UrlParameterBag $urlParameterBag): bool { // HOOK: allow extensions to modify url parameters if ( diff --git a/src/Helper/LanguageText.php b/src/Helper/LanguageText.php index 8b3c0db..d3f476f 100644 --- a/src/Helper/LanguageText.php +++ b/src/Helper/LanguageText.php @@ -15,37 +15,25 @@ class LanguageText { private array $map = []; - /** - * Constructor. - */ public function __construct(array $map = []) { foreach ($map as $k => $v) { - $this->map[strtolower($k)] = $v; + $this->map[strtolower($k)] = (string) $v; } } /** * Returns whether a language has a label. - * - * @param string $language - * - * @return bool */ - public function has($language) + public function has(string $language): bool { return \array_key_exists(strtolower($language), $this->map); } /** * Gets the label for a language, optionally replacing insert tags. - * - * @param string $language - * @param bool $replaceInsertTags - * - * @return string */ - public function get($language, $replaceInsertTags = true) + public function get(string $language, bool $replaceInsertTags = true): string { $language = strtolower($language); @@ -64,11 +52,8 @@ public function get($language, $replaceInsertTags = true) /** * Adds a label for a language. - * - * @param string $language - * @param string $label */ - public function set($language, $label): void + public function set(string $language, string $label): void { $this->map[strtolower($language)] = $label; } @@ -101,10 +86,8 @@ static function (NavigationItem $a, NavigationItem $b) use ($languages) { * Create instance from serialized data of optionsWizard widget. * * @param mixed $config - * - * @return static */ - public static function createFromOptionWizard($config) + public static function createFromOptionWizard($config): self { $config = StringUtil::deserialize($config); diff --git a/src/Language.php b/src/Language.php index 0227ddc..54b5900 100644 --- a/src/Language.php +++ b/src/Language.php @@ -9,14 +9,9 @@ class Language /** * Normalizes a language representation by splitting language and country with given delimiter. * - * @param string $language - * @param string $delimiter - * * @throws \InvalidArgumentException - * - * @return string */ - public static function normalize($language, $delimiter) + public static function normalize(string $language, string $delimiter): string { if (!preg_match('#^([a-z]{2})((-|_)([A-Z]{2}))?$#i', $language, $matches)) { throw new \InvalidArgumentException(sprintf('"%s" is not a supported language format.', $language)); @@ -31,13 +26,9 @@ public static function normalize($language, $delimiter) * * @see http://www.w3.org/International/articles/language-tags/ * - * @param string $language - * * @throws \InvalidArgumentException - * - * @return string */ - public static function toLanguageTag($language) + public static function toLanguageTag(string $language): string { return static::normalize($language, '-'); } @@ -48,13 +39,9 @@ public static function toLanguageTag($language) * * @see http://userguide.icu-project.org/locale * - * @param string $language - * * @throws \InvalidArgumentException - * - * @return string */ - public static function toLocaleID($language) + public static function toLocaleID(string $language): string { return static::normalize($language, '_'); } diff --git a/src/Navigation/NavigationFactory.php b/src/Navigation/NavigationFactory.php index a5514e9..d98c5d9 100644 --- a/src/Navigation/NavigationFactory.php +++ b/src/Navigation/NavigationFactory.php @@ -11,14 +11,9 @@ class NavigationFactory { private PageFinder $pageFinder; - private LanguageText $languageText; - private PageModel $currentPage; - /** - * Constructor. - */ public function __construct(PageFinder $pageFinder, LanguageText $languageText, PageModel $currentPage) { $this->pageFinder = $pageFinder; @@ -31,7 +26,7 @@ public function __construct(PageFinder $pageFinder, LanguageText $languageText, * * @return array */ - public function findNavigationItems(PageModel $currentPage) + public function findNavigationItems(PageModel $currentPage): array { $rootPages = $this->pageFinder->findRootPagesForPage($currentPage, false, false); $navigationItems = $this->createNavigationItemsForRootPages($rootPages); @@ -71,7 +66,7 @@ public function findNavigationItems(PageModel $currentPage) * * @return array */ - private function createNavigationItemsForRootPages(array $rootPages) + private function createNavigationItemsForRootPages(array $rootPages): array { $navigationItems = []; @@ -123,10 +118,8 @@ private function setTargetPageForNavigationItems(array $navigationItems, array $ /** * Returns whether the given page is published. - * - * @return bool */ - private function isPagePublished(PageModel $page) + private function isPagePublished(PageModel $page): bool { if (\defined('BE_USER_LOGGED_IN') && true === BE_USER_LOGGED_IN) { return true; diff --git a/src/Navigation/NavigationItem.php b/src/Navigation/NavigationItem.php index e212ff4..a069ea8 100644 --- a/src/Navigation/NavigationItem.php +++ b/src/Navigation/NavigationItem.php @@ -11,23 +11,13 @@ class NavigationItem { private PageModel $rootPage; - private ?PageModel $targetPage = null; - private string $linkLabel; - private ?bool $newWindow = null; - private bool $isDirectFallback = false; - private bool $isCurrentPage = false; - /** - * Constructor. - * - * @param string|null $label - */ - public function __construct(PageModel $rootPage, $label = null) + public function __construct(PageModel $rootPage, string $label = null) { if ('root' !== $rootPage->type) { throw new \RuntimeException(sprintf('Page ID "%s" has type "%s" but should be "root"', $rootPage->id, $rootPage->type)); @@ -41,90 +31,59 @@ public function __construct(PageModel $rootPage, $label = null) } } - /** - * @return bool - */ - public function hasTargetPage() + public function hasTargetPage(): bool { return $this->targetPage instanceof PageModel; } - /** - * @return bool - */ - public function isDirectFallback() + public function isDirectFallback(): bool { return $this->isDirectFallback; } - /** - * @param $isDirectFallback - */ public function setIsDirectFallback($isDirectFallback): void { $this->isDirectFallback = (bool) $isDirectFallback; } - /** - * @return bool - */ - public function isCurrentPage() + public function isCurrentPage(): bool { return $this->isCurrentPage; } - /** - * @return PageModel - */ - public function getRootPage() + public function getRootPage(): PageModel { return $this->rootPage; } - /** - * @return string - */ - public function getLabel() + public function getLabel(): string { return $this->linkLabel; } - public function setLabel($label): void + public function setLabel(string $label): void { - $this->linkLabel = (string) $label; + $this->linkLabel = $label; } - /** - * @return PageModel|null - */ - public function getTargetPage() + public function getTargetPage(): ?PageModel { return $this->targetPage; } - /** - * @param bool $isDirectFallback - * @param bool $isCurrentPage - */ - public function setTargetPage(PageModel $targetPage, $isDirectFallback, $isCurrentPage = false): void + public function setTargetPage(PageModel $targetPage, bool $isDirectFallback, bool $isCurrentPage = false): void { $this->targetPage = $targetPage->loadDetails(); - $this->isDirectFallback = (bool) $isDirectFallback; - $this->isCurrentPage = (bool) $isCurrentPage; + $this->isDirectFallback = $isDirectFallback; + $this->isCurrentPage = $isCurrentPage; } - /** - * @param bool $isCurrentPage - */ - public function setIsCurrentPage($isCurrentPage): void + public function setIsCurrentPage(bool $isCurrentPage): void { $this->isCurrentPage = $isCurrentPage; } - /** - * @return bool - */ - public function isNewWindow() + public function isNewWindow(): ?bool { if (null === $this->newWindow) { $targetPage = $this->targetPage ?: $this->rootPage; @@ -135,18 +94,12 @@ public function isNewWindow() return $this->newWindow; } - /** - * @param bool|null $newWindow - */ - public function setNewWindow($newWindow): void + public function setNewWindow(?bool $newWindow): void { $this->newWindow = $newWindow; } - /** - * @return string - */ - public function getNormalizedLanguage() + public function getNormalizedLanguage(): string { return strtolower($this->getLocaleId()); } @@ -155,11 +108,9 @@ public function getNormalizedLanguage() * Returns the language formatted as IETF Language Tag (BCP 47) * Example: en, en-US, de-CH. * - * @return string - * * @see http://www.w3.org/International/articles/language-tags/ */ - public function getLanguageTag() + public function getLanguageTag(): string { return Language::toLanguageTag($this->rootPage->language); } @@ -168,21 +119,17 @@ public function getLanguageTag() * Returns the language formatted as ICU Locale ID * Example: en, en_US, de_CH. * - * @return string - * * @see http://userguide.icu-project.org/locale */ - public function getLocaleId() + public function getLocaleId(): string { return Language::toLocaleID($this->rootPage->language); } /** * @throws ExceptionInterface - * - * @return string */ - public function getHref(UrlParameterBag $urlParameterBag, bool $catch = false) + public function getHref(UrlParameterBag $urlParameterBag, bool $catch = false): string { $targetPage = $this->targetPage ?: $this->rootPage; @@ -211,19 +158,13 @@ public function getHref(UrlParameterBag $urlParameterBag, bool $catch = false) return $href; } - /** - * @return string - */ - public function getTitle() + public function getTitle(): string { - return $this->targetPage ? $this->targetPage->title : $this->rootPage->title; + return $this->targetPage->title ?? $this->rootPage->title; } - /** - * @return string - */ - public function getPageTitle() + public function getPageTitle(): string { - return $this->targetPage ? $this->targetPage->pageTitle : $this->rootPage->pageTitle; + return $this->targetPage->pageTitle ?? $this->rootPage->pageTitle; } } diff --git a/src/Navigation/UrlParameterBag.php b/src/Navigation/UrlParameterBag.php index edc4852..b261783 100644 --- a/src/Navigation/UrlParameterBag.php +++ b/src/Navigation/UrlParameterBag.php @@ -7,12 +7,9 @@ class UrlParameterBag { private array $attributes; - private array $query; /** - * Constructor. - * * @param array $attributes Route parameters (e.g. items=foobar in /alias/items/foobar.html) * @param array $query The URL query parameters */ @@ -25,10 +22,7 @@ public function __construct(array $attributes = [], array $query = []) $this->query = $query; } - /** - * @return array - */ - public function getUrlAttributes() + public function getUrlAttributes(): array { return $this->attributes; } @@ -40,49 +34,29 @@ public function setUrlAttributes(array $attributes): void $this->attributes = $attributes; } - /** - * @param string $name - * - * @return bool - */ - public function hasUrlAttribute($name) + public function hasUrlAttribute(string $name): bool { return \array_key_exists($name, $this->attributes); } - /** - * @param string $name - * - * @return mixed|null - */ - public function getUrlAttribute($name) + public function getUrlAttribute(string $name) { return $this->hasUrlAttribute($name) ? $this->attributes[$name] : null; } - /** - * @param string $name - * @param mixed $value - */ - public function setUrlAttribute($name, $value): void + public function setUrlAttribute(string $name, $value): void { $this->validateScalar($value); $this->attributes[$name] = $value; } - /** - * @param string $name - */ - public function removeUrlAttribute($name): void + public function removeUrlAttribute(string $name): void { unset($this->attributes[$name]); } - /** - * @return array - */ - public function getQueryParameters() + public function getQueryParameters(): array { return $this->query; } @@ -94,41 +68,24 @@ public function setQueryParameters(array $query): void $this->query = $query; } - /** - * @param string $name - * - * @return bool - */ - public function hasQueryParameter($name) + public function hasQueryParameter(string $name): bool { return \array_key_exists($name, $this->query); } - /** - * @param string $name - * - * @return mixed|null - */ - public function getQueryParameter($name) + public function getQueryParameter(string $name) { return $this->hasQueryParameter($name) ? $this->query[$name] : null; } - /** - * @param string $name - * @param mixed $value - */ - public function setQueryParameter($name, $value): void + public function setQueryParameter(string $name, $value): void { $this->validateScalar($value); $this->query[$name] = $value; } - /** - * @param string $name - */ - public function removeQueryParameter($name): void + public function removeQueryParameter(string $name): void { unset($this->query[$name]); } @@ -137,10 +94,8 @@ public function removeQueryParameter($name): void * Generates parameter string to generate a Contao url. * * @throws \RuntimeException - * - * @return string|null */ - public function generateParameters() + public function generateParameters(): ?string { $params = ''; $auto_item = null; @@ -192,10 +147,8 @@ static function (&$v, $k): void { /** * Generates a query string or returns null if empty. - * - * @return string|null */ - public function generateQueryString() + public function generateQueryString(): ?string { if (0 === \count($this->query)) { return null; diff --git a/src/PageFinder.php b/src/PageFinder.php index bfa5751..b29511f 100644 --- a/src/PageFinder.php +++ b/src/PageFinder.php @@ -12,12 +12,9 @@ class PageFinder { /** - * @param bool $skipCurrent - * @param bool $publishedOnly - * * @return array */ - public function findRootPagesForPage(PageModel $page, $skipCurrent = false, $publishedOnly = true) + public function findRootPagesForPage(PageModel $page, bool $skipCurrent = false, bool $publishedOnly = true): array { $page->loadDetails(); $t = $page::getTable(); @@ -66,10 +63,8 @@ public function findRootPagesForPage(PageModel $page, $skipCurrent = false, $pub /** * Finds the root page of fallback language for the given page. - * - * @return PageModel|null */ - public function findMasterRootForPage(PageModel $page) + public function findMasterRootForPage(PageModel $page): ?PageModel { $page->loadDetails(); $t = $page::getTable(); @@ -92,11 +87,9 @@ public function findMasterRootForPage(PageModel $page) } /** - * @param bool $skipCurrent - * * @return array */ - public function findAssociatedForPage(PageModel $page, $skipCurrent = false, array $rootPages = null) + public function findAssociatedForPage(PageModel $page, bool $skipCurrent = false, array $rootPages = null): array { if ('root' === $page->type) { return $this->findRootPagesForPage($page, $skipCurrent); @@ -136,12 +129,7 @@ static function (PageModel $page) use ($rootPages) { ); } - /** - * @param string $language - * - * @return PageModel - */ - public function findAssociatedForLanguage(PageModel $page, $language) + public function findAssociatedForLanguage(PageModel $page, string $language): PageModel { $language = Language::toLocaleID($language); $associated = $this->findAssociatedForPage($page); @@ -158,10 +146,7 @@ public function findAssociatedForLanguage(PageModel $page, $language) return $this->findAssociatedParentForLanguage($page, $language); } - /** - * @return PageModel|null - */ - public function findAssociatedInMaster(PageModel $page) + public function findAssociatedInMaster(PageModel $page): ?PageModel { $page->loadDetails(); $masterRoot = $this->findMasterRootForPage($page); @@ -184,14 +169,10 @@ public function findAssociatedInMaster(PageModel $page) } /** - * @param string $language - * * @throws \RuntimeException * @throws \InvalidArgumentException - * - * @return PageModel */ - public function findAssociatedParentForLanguage(PageModel $page, $language) + public function findAssociatedParentForLanguage(PageModel $page, string $language): PageModel { // Stop loop if we're at the top if (0 === $page->pid || 'root' === $page->type) { @@ -215,10 +196,7 @@ public function findAssociatedParentForLanguage(PageModel $page, $language) return $this->findAssociatedForLanguage($parent, $language); } - /** - * @param string $table - */ - private function addPublishingConditions(array &$columns, $table): void + private function addPublishingConditions(array &$columns, string $table): void { if (!System::getContainer()->get('contao.security.token_checker')->isPreviewMode()) { $start = Date::floorToMinute(); @@ -233,7 +211,7 @@ private function addPublishingConditions(array &$columns, $table): void /** * @return array */ - private function findPages(array $columns, array $values, array $options = []) + private function findPages(array $columns, array $values, array $options = []): array { /** @var Collection $collection */ $collection = PageModel::findBy($columns, $values, $options);