Skip to content

Commit

Permalink
Improve phpstan
Browse files Browse the repository at this point in the history
  • Loading branch information
aschempp committed Jul 19, 2024
1 parent 2fa71a8 commit eb2354b
Show file tree
Hide file tree
Showing 33 changed files with 161 additions and 17 deletions.
1 change: 0 additions & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
parameters:
ignoreErrors:
- '#Access to an undefined property Contao\\DataContainer::\$activeRecord.#'
- '#Parameter \#2 \$models#'
4 changes: 3 additions & 1 deletion src/EventListener/BackendView/AbstractViewListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ abstract protected function getCurrentPage();
/**
* Returns a list of languages the user can switch to.
*
* @return array
* @return array<int|string, string>
*/
abstract protected function getAvailableLanguages(PageModel $page);

Expand Down Expand Up @@ -120,6 +120,8 @@ protected function getLanguageLabel($languageCode)

/**
* Returns HTML markup for the global operation.
*
* @param array<int|string, string> $languages
*/
private function getSwitchButton(PageModel $page, array $languages): string
{
Expand Down
3 changes: 3 additions & 0 deletions src/EventListener/BackendView/ArticleViewListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ protected function getCurrentPage(): ?PageModel
return PageModel::findWithDetails($this->currentArticle->pid);
}

/**
* @return array<int|string, string>
*/
protected function getAvailableLanguages(PageModel $page): array
{
$options = [];
Expand Down
3 changes: 3 additions & 0 deletions src/EventListener/BackendView/PageViewListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ protected function getCurrentPage(): ?PageModel
return PageModel::findById($node);
}

/**
* @return array<int|string, string>
*/
protected function getAvailableLanguages(PageModel $page): array
{
$options = [];
Expand Down
5 changes: 4 additions & 1 deletion src/EventListener/BackendView/ParentChildViewListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,10 @@ protected function getCurrentPage()
return PageModel::findWithDetails($pageId);
}

protected function getAvailableLanguages(PageModel $page)
/**
* @return array<int|string, string>
*/
protected function getAvailableLanguages(PageModel $page): array
{
$options = [];
$masterRoot = $this->pageFinder->findMasterRootForPage($page);
Expand Down
6 changes: 6 additions & 0 deletions src/EventListener/CallbackSetupListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
*/
class CallbackSetupListener
{
/**
* @var array<string, array<string>>|null
*/
private static ?array $listeners = null;

public function __invoke(string $table): void
Expand All @@ -34,6 +37,9 @@ public function __invoke(string $table): void
}
}

/**
* @return array<string, array<string>>
*/
private static function getListeners(): array
{
if (null !== self::$listeners) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ public function onLoad(DataContainer $dc): void
}

/**
* @return array
* @return array<int|string, string>
*/
public function onLanguageMainOptions(DataContainer $dc)
public function onLanguageMainOptions(DataContainer $dc): array
{
try {
if (
Expand Down Expand Up @@ -106,7 +106,7 @@ abstract protected function getSorting();
/**
* @param Collection<Model> $models
*
* @return array
* @return array<int|string, string>
*/
abstract protected function formatOptions(Model $current, Collection $models);
abstract protected function formatOptions(Model $current, Collection $models): array;
}
3 changes: 3 additions & 0 deletions src/EventListener/DataContainer/ArticleListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ public function onLoad(DataContainer $dc): void
}
}

/**
* @return array<int|string, string>
*/
public function onLanguageMainOptions(DataContainer $dc): array
{
$pageFinder = new PageFinder();
Expand Down
3 changes: 3 additions & 0 deletions src/EventListener/DataContainer/CalendarEventsListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ protected function getSorting(): string
return 'startTime DESC';
}

/**
* @return array<int|string, string>
*/
protected function formatOptions(Model $current, Collection $models): array
{
$options = [];
Expand Down
2 changes: 2 additions & 0 deletions src/EventListener/DataContainer/FaqListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ protected function getSorting(): string

/**
* @param Collection<FaqModel> $models
*
* @return array<int|string, string>
*/
protected function formatOptions(Model $current, Collection $models): array
{
Expand Down
17 changes: 17 additions & 0 deletions src/EventListener/DataContainer/MissingLanguageIconListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,23 @@
*/
class MissingLanguageIconListener implements ResetInterface
{
/**
* @var array<string, string>|null
*/
private static ?array $callbacks = null;

private TokenStorageInterface $tokenStorage;

private Connection $connection;

/**
* @var array<int|string, array>|null
*/
private ?array $pageCache = null;

/**
* @var array<string, array<int, int>>|null
*/
private ?array $translationCache = null;

public function __construct(TokenStorageInterface $tokenStorage, Connection $connection)
Expand Down Expand Up @@ -63,6 +72,7 @@ public function reset(): void
/**
* Adds missing translation warning to page tree.
*
* @param array<string|array> $args
* @param string|array|null $previousResult;
*/
private function onPageLabel(array $args, $previousResult = null): string
Expand Down Expand Up @@ -107,6 +117,7 @@ private function onPageLabel(array $args, $previousResult = null): string
/**
* Adds missing translation warning to article tree.
*
* @param array<int, string> $args
* @param string|array|null $previousResult
*/
private function onArticleLabel(array $args, $previousResult = null): string
Expand All @@ -131,6 +142,7 @@ private function onArticleLabel(array $args, $previousResult = null): string
/**
* Generate missing translation warning for news child records.
*
* @param array<int, string> $args
* @param string|array|null $previousResult
*/
private function onNewsChildRecords(array $args, $previousResult = null): string
Expand All @@ -157,6 +169,7 @@ private function onNewsChildRecords(array $args, $previousResult = null): string
/**
* Generate missing translation warning for calendar events child records.
*
* @param array<int, string> $args
* @param string|array|null $previousResult
*/
private function onCalendarEventChildRecords(array $args, $previousResult = null): string
Expand All @@ -179,6 +192,7 @@ private function onCalendarEventChildRecords(array $args, $previousResult = null
/**
* Generate missing translation warning for faq child records.
*
* @param array<int, string> $args
* @param string|array|null $previousResult
*/
private function onFaqChildRecords(array $args, $previousResult = null): string
Expand Down Expand Up @@ -209,6 +223,9 @@ private function generateLabelWithWarning(string $label, string $imgStyle = ''):
);
}

/**
* @return array<string, string>
*/
private static function getCallbacks(): array
{
if (null !== self::$callbacks) {
Expand Down
2 changes: 2 additions & 0 deletions src/EventListener/DataContainer/NewsListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ protected function getSorting(): string

/**
* @param Collection<NewsModel> $models
*
* @return array<string, array<int|string, string>>
*/
protected function formatOptions(Model $current, Collection $models): array
{
Expand Down
2 changes: 2 additions & 0 deletions src/EventListener/DataContainer/PageFieldsListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ public function onSaveLanguageMain($value, DataContainer $dc)
* Gets list of options for language root selection (linking multiple fallback
* roots on different domains).
*
* @return array<int|string, string>
*
* @Callback(table="tl_page", target="fields.languageRoot.options")
*/
public function onLanguageRootOptions(DataContainer $dc): array
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ private function addRootLanguageFields(): void
}

/**
* @param array|string $palettes
* @param array<string>|string $palettes
*/
private function addRegularLanguageFields($palettes, bool $addLanguageMain = true): void
{
Expand Down
2 changes: 2 additions & 0 deletions src/EventListener/DataContainer/PageOperationListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ private function onDelete(DataContainer $dc): void

/**
* Handles undo of a deleted page and resets tl_page.languageMain if necessary.
*
* @param array<string, int|string> $row
*/
private function onUndo(array $row): void
{
Expand Down
4 changes: 2 additions & 2 deletions src/EventListener/DataContainer/ParentTableListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ public function register(): void
}

/**
* @return array
* @return array<int|string, string>
*/
public function onMasterOptions(DataContainer $dc)
public function onMasterOptions(DataContainer $dc): array
{
if (null === ($jumpTo = PageModel::findById($dc->activeRecord->jumpTo))) {
return [];
Expand Down
3 changes: 3 additions & 0 deletions src/EventListener/DataContainer/UserLabelsListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ public function __construct(Connection $connection)
$this->connection = $connection;
}

/**
* @return array<int|string, string>
*/
public function __invoke(): array
{
return $this->connection->fetchAllKeyValue("SELECT id, title FROM tl_page WHERE type='root' AND (fallback='' OR languageRoot!=0) ORDER BY pid, sorting");
Expand Down
8 changes: 8 additions & 0 deletions src/EventListener/Navigation/AbstractNavigationListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ public function onChangelanguageNavigation(ChangelanguageNavigationEvent $event)

/**
* Adds publishing conditions to Model query columns if backend user is not logged in.
*
* @param array<string> $columns
*
* @return array<string>
*/
protected function addPublishedConditions(array $columns, string $table, bool $addStartStop = true): array
{
Expand Down Expand Up @@ -144,6 +148,10 @@ abstract protected function getUrlKey();
abstract protected function findCurrent();

/**
* @param array<string> $columns
* @param array<string> $values
* @param array<string, string> $options
*
* @return Model|null
*/
abstract protected function findPublishedBy(array $columns, array $values = [], array $options = []);
Expand Down
7 changes: 5 additions & 2 deletions src/EventListener/Navigation/ArticleNavigationListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,11 @@ private function findTargetArticle(ArticleModel $currentArticle, $targetRootId,

/**
* Find a published article with additional conditions.
*
* @param array<string> $columns
* @param array<string> $values
*/
private function findPublishedArticle(array $columns, array $values = [], array $options = []): ?ArticleModel
private function findPublishedArticle(array $columns, array $values = []): ?ArticleModel
{
if (!$this->tokenChecker->isPreviewMode()) {
$time = Date::floorToMinute();
Expand All @@ -112,6 +115,6 @@ private function findPublishedArticle(array $columns, array $values = [], array
$columns[] = "tl_article.published='1'";
}

return ArticleModel::findOneBy($columns, $values, $options);
return ArticleModel::findOneBy($columns, $values);
}
}
5 changes: 5 additions & 0 deletions src/EventListener/Navigation/CalendarNavigationListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ protected function findCurrent(): ?CalendarEventsModel
return CalendarEventsModel::findPublishedByParentAndIdOrAlias($alias, $calendars->fetchEach('id'));
}

/**
* @param array<string> $columns
* @param array<string> $values
* @param array<string, string> $options
*/
protected function findPublishedBy(array $columns, array $values = [], array $options = [])
{
return CalendarEventsModel::findOneBy(
Expand Down
5 changes: 5 additions & 0 deletions src/EventListener/Navigation/FaqNavigationListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ protected function findCurrent(): ?FaqModel
return FaqModel::findPublishedByParentAndIdOrAlias($alias, $calendars->fetchEach('id'));
}

/**
* @param array<string> $columns
* @param array<string> $values
* @param array<string, string> $options
*/
protected function findPublishedBy(array $columns, array $values = [], array $options = []): ?FaqModel
{
return FaqModel::findOneBy(
Expand Down
6 changes: 6 additions & 0 deletions src/EventListener/Navigation/NewsNavigationListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ protected function findCurrent(): ?NewsModel
return NewsModel::findPublishedByParentAndIdOrAlias($alias, $archives->fetchEach('id'), $options);
}

/**
* @param array<string> $columns
* @param array<string> $values
* @param array<string, string> $options
* @return NewsModel|null
*/
protected function findPublishedBy(array $columns, array $values = [], array $options = []): ?NewsModel
{
return NewsModel::findOneBy(
Expand Down
17 changes: 17 additions & 0 deletions src/FrontendModule/ChangeLanguageModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,23 @@ protected function compile(): void

/**
* Generates array suitable for nav_default template.
*
* @return array{
* isActive: bool,
* class: string,
* link: string,
* subitems: string,
* href: string,
* title: string,
* pageTitle: string,
* accesskey: string,
* tabindex: string,
* nofollow: bool,
* rel: string,
* target: string,
* item: NavigationItem,
* languageTag: string,
* }
*/
protected function generateTemplateArray(NavigationItem $item, UrlParameterBag $urlParameterBag): array
{
Expand Down
3 changes: 3 additions & 0 deletions src/Helper/AlternateLinks.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
*/
class AlternateLinks
{
/**
* @var array<string, array{language: string, href: string, title: string}>
*/
private array $links = [];

/**
Expand Down
1 change: 1 addition & 0 deletions src/Helper/LabelCallback.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public static function createAndRegister(string $table, callable $callback): sel

/**
* @param callable|array $callback
* @param array<mixed> $args
*
* @return mixed
*/
Expand Down
Loading

0 comments on commit eb2354b

Please sign in to comment.