Skip to content

Commit

Permalink
[FEATURE] Implemented PHPStan level 8, introduced some DTO classes to…
Browse files Browse the repository at this point in the history
… prevent some multidimensional array shapes, introduced BackendUserTrait and LanguageServiceTrait
  • Loading branch information
Riny van Tiggelen committed Sep 25, 2024
1 parent 15b5dc3 commit 9833842
Show file tree
Hide file tree
Showing 67 changed files with 1,024 additions and 374 deletions.
14 changes: 12 additions & 2 deletions .Build/phpstan.cms11.neon
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
parameters:
level: 0
level: 8
paths:
- ../Classes
- ../Configuration
excludePaths:
- ../Classes/Updates
ignoreErrors:
- '#Parameter \$event of method#'
- '#TYPO3\\CMS\\Frontend\\Page\\PageInformation#'
- '#TYPO3\\CMS\\Backend\\View\\BackendViewFactory#'
- '#TYPO3\\CMS\\Install\\Attribute\\UpgradeWizard#'
- '#TYPO3\\CMS\\Backend\\Template\\ModuleTemplate#'
- '#TYPO3\\CMS\\Extbase\\Mvc\\RequestInterface#'
- '#TYPO3\\CMS\\Core\\View\\ViewInterface#'
- '#TYPO3\\CMS\\Core\\Domain\\Repository\\PageRepository::getLanguageOverlay#'
- '#frontend.page.information#'
- '#ModifyPageLayoutContentEvent#'
- '#AfterCacheableContentIsGeneratedEvent#'
- '#loadJavaScriptModule#'
- '#getLanguageCode#'
- '#addJsInlineCode#'
5 changes: 4 additions & 1 deletion .Build/phpstan.cms12.neon
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
parameters:
level: 0
level: 8
paths:
- ../Classes
- ../Configuration
excludePaths:
- ../Classes/Updates
ignoreErrors:
- '#TYPO3\\CMS\\Backend\\ViewHelpers\\ModuleLayoutViewHelper#'
- '#TYPO3\\CMS\\Frontend\\Page\\PageInformation#'
- '#frontend.page.information#'
- '#protected method getRecordOverlay#'
7 changes: 6 additions & 1 deletion .Build/phpstan.cms13.neon
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
parameters:
level: 0
level: 8
paths:
- ../Classes
- ../Configuration
excludePaths:
- ../Classes/Updates
ignoreErrors:
- '#TYPO3\\CMS\\Backend\\ViewHelpers\\ModuleLayoutViewHelper#'
- '#TYPO3\\CMS\\Backend\\Template\\ModuleTemplate#'
- '#getRecordOverlay#'
- '#loadRequireJsModule#'
typo3:
requestGetAttributeMapping:
frontend.page.information: TYPO3\CMS\Frontend\Page\PageInformation
64 changes: 54 additions & 10 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -1,20 +1,64 @@
# EditorConfig is awesome: http://EditorConfig.org

# top-most EditorConfig file
root = true

[{*.rst,*.rst.txt}]
# Unix-style newlines with a newline ending every file
[*]
charset = utf-8
end_of_line = lf
indent_style = space
indent_size = 4
insert_final_newline = true
trim_trailing_whitespace = true
indent_style = space
indent_size = 3

# TS/JS-Files
[*.{ts,js,mjs}]
indent_size = 2

# JSON-Files
[*.json]
indent_style = tab

# ReST-Files
[*.{rst,rst.txt}]
indent_size = 4
max_line_length = 80

# MD-Files
# Markdown-Files
[*.md]
charset = utf-8
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
indent_style = space
indent_size = 4
max_line_length = 80

# YAML-Files
[*.{yaml,yml}]
indent_size = 2

# NEON-Files
[*.neon]
indent_size = 2
indent_style = tab

# stylelint
[.stylelintrc]
indent_size = 2

# package.json
[package.json]
indent_size = 2

# TypoScript
[*.{typoscript,tsconfig}]
indent_size = 2

# XLF-Files
[*.xlf]
indent_style = tab

# SQL-Files
[*.sql]
indent_style = tab
indent_size = 2

# .htaccess
[{_.htaccess,.htaccess}]
indent_style = tab
29 changes: 29 additions & 0 deletions Classes/Backend/Overview/DataProviderRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace YoastSeoForTypo3\YoastSeo\Backend\Overview;

class DataProviderRequest
{
public function __construct(
protected int $id = 0,
protected int $language = 0,
protected string $table = '',
) {}

public function getId(): int
{
return $this->id;
}

public function getLanguage(): int
{
return $this->language;
}

public function getTable(): string
{
return $this->table;
}
}
120 changes: 120 additions & 0 deletions Classes/Backend/Overview/LanguageMenu/LanguageMenuFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<?php

declare(strict_types=1);

namespace YoastSeoForTypo3\YoastSeo\Backend\Overview\LanguageMenu;

use TYPO3\CMS\Backend\Template\Components\Menu\Menu;
use TYPO3\CMS\Backend\Template\ModuleTemplate;
use TYPO3\CMS\Core\Exception\SiteNotFoundException;
use TYPO3\CMS\Core\Site\Entity\SiteLanguage;
use TYPO3\CMS\Core\Site\SiteFinder;
use TYPO3\CMS\Extbase\Mvc\RequestInterface;
use TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder;
use YoastSeoForTypo3\YoastSeo\Traits\BackendUserTrait;
use YoastSeoForTypo3\YoastSeo\Traits\LanguageServiceTrait;

class LanguageMenuFactory
{
use BackendUserTrait, LanguageServiceTrait;

protected RequestInterface $request;
protected ModuleTemplate $moduleTemplate;
protected int $pageUid;

public function __construct(
protected SiteFinder $siteFinder,
protected UriBuilder $uriBuilder
) {
}

public function create(
RequestInterface $request,
ModuleTemplate $moduleTemplate,
int $pageUid
): ?Menu {
try {
$site = $this->siteFinder->getSiteByPageId($pageUid);
} catch (SiteNotFoundException) {
return null;
}

$languages = $site->getAvailableLanguages($this->getBackendUser());
if (empty($languages)) {
return null;
}

$this->request = $request;
$this->moduleTemplate = $moduleTemplate;
$this->pageUid = $pageUid;

return $this->buildLanguageMenu($languages);
}

/**
* @param SiteLanguage[] $languages
*/
protected function buildLanguageMenu(
array $languages
): Menu {
$languageMenu = $this->moduleTemplate->getDocHeaderComponent()->getMenuRegistry()->makeMenu();
$languageMenu->setIdentifier('languageMenu');
$languageMenu->setLabel(
$this->getLanguageService()->sL(
'LLL:EXT:core/Resources/Private/Language/locallang_general.xlf:LGL.language'
)
);
foreach ($this->getLanguageMenuItems($languages) as $languageMenuItem) {
$menuItem = $languageMenu
->makeMenuItem()
->setTitle($languageMenuItem->getTitle())
->setHref($languageMenuItem->getHref())
->setActive($languageMenuItem->isActive());
$languageMenu->addMenuItem($menuItem);
}
return $languageMenu;
}

/**
* @param SiteLanguage[] $languages
* @return LanguageMenuItem[]
*/
protected function getLanguageMenuItems(
array $languages
): array {
$arguments = $this->getArguments();

$filter = $arguments['filter'] ?? '';
$returnUrl = $arguments['returnUrl'] ?? '';
$items = [];
foreach ($languages as $language) {
$url = $this->uriBuilder
->reset()
->setRequest($this->request)
->setTargetPageUid($this->pageUid)
->setArguments([
'tx_yoastseo_yoast_yoastseooverview' => [
'filter' => $filter,
'language' => $language->getLanguageId(),
'returnUrl' => $returnUrl,
'controller' => 'Overview',
],
])
->build();
$items[] = new LanguageMenuItem(
$language->getTitle(),
$url,
(int)($arguments['language'] ?? 0) === $language->getLanguageId()
);
}
return $items;
}

/**
* @return array<string, mixed>
*/
protected function getArguments(): array
{
return $this->request->getArguments()['tx_yoastseo_yoast_yoastseooverview'] ?? $this->request->getArguments();
}
}
45 changes: 45 additions & 0 deletions Classes/Backend/Overview/LanguageMenu/LanguageMenuItem.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

declare(strict_types=1);

namespace YoastSeoForTypo3\YoastSeo\Backend\Overview\LanguageMenu;

class LanguageMenuItem
{
public function __construct(
protected string $title = '',
protected string $href = '',
protected bool $active = false,
) {
}

public function getTitle(): string
{
return $this->title;
}

public function setTitle(string $title): void
{
$this->title = $title;
}

public function getHref(): string
{
return $this->href;
}

public function setHref(string $href): void
{
$this->href = $href;
}

public function isActive(): bool
{
return $this->active;
}

public function setActive(bool $active): void
{
$this->active = $active;
}
}
13 changes: 11 additions & 2 deletions Classes/Backend/PageLayoutHeader.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ public function __construct(
) {
}

public function render(array $params = null, $parentObj = null): string
/**
* @param array<string, string>|null $params
*/
public function render(array $params = null, PageLayoutController|ModuleTemplate|null $parentObj = null): string
{
$languageId = $this->getLanguageId();
$pageId = (int)$_GET['id'];
Expand Down Expand Up @@ -60,7 +63,10 @@ protected function renderHtml(): string
return $templateView->render();
}

protected function getCurrentPage(int $pageId, int $languageId, object $parentObj): ?array
/**
* @return array<string, string>|null
*/
protected function getCurrentPage(int $pageId, int $languageId, PageLayoutController|ModuleTemplate|null $parentObj): ?array
{
if ((!$parentObj instanceof PageLayoutController && !$parentObj instanceof ModuleTemplate) || $pageId <= 0) {
return null;
Expand Down Expand Up @@ -88,6 +94,9 @@ protected function getCurrentPage(int $pageId, int $languageId, object $parentOb
return null;
}

/**
* @param array<string, string> $pageRecord
*/
protected function shouldShowPreview(int $pageId, array $pageRecord): bool
{
if (!YoastUtility::snippetPreviewEnabled($pageId, $pageRecord)) {
Expand Down
Loading

0 comments on commit 9833842

Please sign in to comment.