Skip to content

Commit

Permalink
[TASK] Prevent undefined array key warnings in ext:filelist
Browse files Browse the repository at this point in the history
This change addresses several "undefined array key" issues that have
been identified by PsalmPHP (see issue #98321).

Resolves: #98411
Related: #98321
Releases: main, 11.5
Change-Id: Iec52d5b511c8c4af9fa97e3a5081a7a1ac1e5e3d
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/75843
Tested-by: core-ci <typo3@b13.com>
Tested-by: Christian Kuhn <lolli@schwarzbu.ch>
Tested-by: Stefan Bürk <stefan@buerk.tech>
Tested-by: Oliver Hader <oliver.hader@typo3.org>
Reviewed-by: Oliver Klee <typo3-coding@oliverklee.de>
Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch>
Reviewed-by: Stefan Bürk <stefan@buerk.tech>
Reviewed-by: Oliver Hader <oliver.hader@typo3.org>
  • Loading branch information
ohader committed Oct 4, 2022
1 parent 564b3f6 commit 063c2e4
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 20 deletions.
Expand Up @@ -29,6 +29,7 @@
/**
* Contains translation tools
*
* @phpstan-type LanguageRef -1|0|positive-int
* @internal The whole class is subject to be removed, fetch all language info from the current site object.
*/
class TranslationConfigurationProvider
Expand All @@ -40,7 +41,7 @@ class TranslationConfigurationProvider
* The property flagIcon returns a string <flags-xx>.
*
* @param int $pageId Page id (used to get TSconfig configuration setting flag and label for default language)
* @return array Array with languages (uid, title, ISOcode, flagIcon)
* @return array<LanguageRef, array{uid: int, title: string, ISOcode: string, flagIcon: string}> Array with languages
*/
public function getSystemLanguages($pageId = 0)
{
Expand Down
11 changes: 8 additions & 3 deletions typo3/sysext/core/Classes/Site/Entity/Site.php
Expand Up @@ -35,6 +35,8 @@

/**
* Entity representing a single site with available languages
*
* @phpstan-type LanguageRef -1|0|positive-int
*/
class Site implements SiteInterface
{
Expand Down Expand Up @@ -64,7 +66,7 @@ class Site implements SiteInterface
protected $configuration;

/**
* @var SiteLanguage[]
* @var array<LanguageRef, SiteLanguage>
*/
protected $languages;

Expand Down Expand Up @@ -208,7 +210,7 @@ public function getRootPageId(): int
/**
* Returns all available languages of this site
*
* @return SiteLanguage[]
* @return array<LanguageRef, SiteLanguage>
*/
public function getLanguages(): array
{
Expand All @@ -224,7 +226,7 @@ public function getLanguages(): array
/**
* Returns all available languages of this site, even the ones disabled for frontend usages
*
* @return SiteLanguage[]
* @return array<LanguageRef, SiteLanguage>
*/
public function getAllLanguages(): array
{
Expand Down Expand Up @@ -254,6 +256,9 @@ public function getDefaultLanguage(): SiteLanguage
return reset($this->languages);
}

/**
* @return array<LanguageRef, SiteLanguage>
*/
public function getAvailableLanguages(BackendUserAuthentication $user, bool $includeAllLanguagesFlag = false, int $pageId = null): array
{
$availableLanguages = [];
Expand Down
Expand Up @@ -418,7 +418,7 @@ protected function getAdditionalAttributes(string $itemName): array
if ($itemName === 'delete' && $this->backendUser->jsConfirmation(JsConfirmation::DELETE)) {
$title = $this->languageService->sL('LLL:EXT:core/Resources/Private/Language/locallang_mod_web_list.xlf:delete');

$recordInfo = GeneralUtility::fixed_lgd_cs($this->record->getName(), (int)$this->backendUser->uc['titleLen']);
$recordInfo = GeneralUtility::fixed_lgd_cs($this->record->getName(), (int)($this->backendUser->uc['titleLen'] ?? 0));
if ($this->isFolder()) {
if ($this->backendUser->shallDisplayDebugInformation()) {
$recordInfo .= ' [' . $this->record->getIdentifier() . ']';
Expand Down
Expand Up @@ -161,7 +161,7 @@ public function mainAction(ServerRequestInterface $request): ResponseInterface
$view->assignMultiple([
'moduleUrlTceFile' => (string)$this->uriBuilder->buildUriFromRoute('tce_file'),
'fileName' => $file->getName(),
'form' => $formResultCompiler->addCssFiles() . $resultArray['html'] . $formResultCompiler->printNeededJSFunctions(),
'form' => $formResultCompiler->addCssFiles() . ($resultArray['html'] ?? '') . $formResultCompiler->printNeededJSFunctions(),
]);
$content = $view->render('File/EditFile');

Expand Down
Expand Up @@ -125,10 +125,11 @@ protected function init(ServerRequestInterface $request): void
// rename the folder
if ($this->fileOrFolderObject instanceof Folder) {
$parsedUrl = parse_url($this->returnUrl);
$queryParts = GeneralUtility::explodeUrl2Array(urldecode($parsedUrl['query']));
if ($queryParts['id'] === $this->fileOrFolderObject->getCombinedIdentifier()) {
$queryParts = GeneralUtility::explodeUrl2Array(urldecode($parsedUrl['query'] ?? ''));
$queryPartsId = $queryParts['id'] ?? null;
if ($queryPartsId === $this->fileOrFolderObject->getCombinedIdentifier()) {
$this->returnUrl = str_replace(
urlencode($queryParts['id']),
urlencode($queryPartsId),
urlencode($this->fileOrFolderObject->getStorage()->getRootLevelFolder()->getCombinedIdentifier()),
$this->returnUrl
);
Expand Down
Expand Up @@ -117,10 +117,11 @@ protected function init(ServerRequestInterface $request): void
// rename the folder
if ($this->fileOrFolderObject instanceof Folder) {
$parsedUrl = parse_url($this->returnUrl);
$queryParts = GeneralUtility::explodeUrl2Array(urldecode($parsedUrl['query']));
if ($queryParts['id'] === $this->fileOrFolderObject->getCombinedIdentifier()) {
$queryParts = GeneralUtility::explodeUrl2Array(urldecode($parsedUrl['query'] ?? ''));
$queryPartsId = $queryParts['id'] ?? null;
if ($queryPartsId === $this->fileOrFolderObject->getCombinedIdentifier()) {
$this->returnUrl = str_replace(
urlencode($queryParts['id']),
urlencode($queryPartsId),
urlencode($this->fileOrFolderObject->getStorage()->getRootLevelFolder()->getCombinedIdentifier()),
$this->returnUrl
);
Expand Down
21 changes: 13 additions & 8 deletions typo3/sysext/filelist/Classes/FileList.php
Expand Up @@ -761,21 +761,24 @@ public function formatFileList(array $files)
* Fetch the translations for a sys_file_metadata record
*
* @param array $metaDataRecord
* @return array keys are the site language ids, values are the $rows
* @return array<int, array<string, mixed>> keys are the site language ids, values are the $rows
*/
protected function getTranslationsForMetaData($metaDataRecord)
{
$languageField = $GLOBALS['TCA']['sys_file_metadata']['ctrl']['languageField'] ?? '';
$languageParentField = $GLOBALS['TCA']['sys_file_metadata']['ctrl']['transOrigPointerField'] ?? '';

$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('sys_file_metadata');
$queryBuilder->getRestrictions()->removeAll();
$translationRecords = $queryBuilder->select('*')
->from('sys_file_metadata')
->where(
$queryBuilder->expr()->eq(
$GLOBALS['TCA']['sys_file_metadata']['ctrl']['transOrigPointerField'],
$languageParentField,
$queryBuilder->createNamedParameter($metaDataRecord['uid'] ?? 0, \PDO::PARAM_INT)
),
$queryBuilder->expr()->gt(
$GLOBALS['TCA']['sys_file_metadata']['ctrl']['languageField'],
$languageField,
$queryBuilder->createNamedParameter(0, \PDO::PARAM_INT)
)
)
Expand All @@ -784,7 +787,8 @@ protected function getTranslationsForMetaData($metaDataRecord)

$translations = [];
foreach ($translationRecords as $record) {
$translations[$record[$GLOBALS['TCA']['sys_file_metadata']['ctrl']['languageField']]] = $record;
$languageId = $record[$languageField];
$translations[$languageId] = $record;
}
return $translations;
}
Expand Down Expand Up @@ -1125,7 +1129,7 @@ public function makeEdit($fileOrFolderObject)
if (empty($title)) {
preg_match('/aria-label="([^"]*)"/', $action, $title);
}
if (!empty($title[1] ?? '')) {
if (!empty($title[1])) {
$action = str_replace(
[
'</a>',
Expand All @@ -1138,7 +1142,7 @@ public function makeEdit($fileOrFolderObject)
);
// In case we added the title as tag content, we can remove the attribute,
// since this is duplicated and would trigger a tooltip with the same content.
if (!empty($title[0] ?? '')) {
if (!empty($title[0])) {
$action = str_replace($title[0], '', $action);
}
$cellOutput .= '<li>' . $action . '</li>';
Expand Down Expand Up @@ -1267,12 +1271,13 @@ protected function makeTranslations(File $file): string
// Set options for "create new" action of a new translation
$title = sprintf($this->getLanguageService()->getLL('createMetadataForLanguage'), $language['title']);
$actionType = 'new';
$metaDataRecordId = (int)($metaDataRecord['uid'] ?? 0);
$url = BackendUtility::getLinkToDataHandlerAction(
'&cmd[sys_file_metadata][' . $metaDataRecord['uid'] . '][localize]=' . $languageId,
'&cmd[sys_file_metadata][' . $metaDataRecordId . '][localize]=' . $languageId,
(string)$this->uriBuilder->buildUriFromRoute(
'record_edit',
[
'justLocalized' => 'sys_file_metadata:' . $metaDataRecord['uid'] . ':' . $languageId,
'justLocalized' => 'sys_file_metadata:' . $metaDataRecordId . ':' . $languageId,
'returnUrl' => $this->listURL(),
]
)
Expand Down

0 comments on commit 063c2e4

Please sign in to comment.