Skip to content

Commit

Permalink
[TASK] Fix phpstan checkFunctionArgumentTypes errors in ext:impexp
Browse files Browse the repository at this point in the history
This patch fixes incompatible type usage in function arguments
and is preparatory work for introducing native type hints and
strict mode in all core files.

Resolves: #92168
Releases: master, 10.4
Change-Id: I79c4576e570e175b770e8ee2929b835a4a0fa55a
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/65932
Tested-by: TYPO3com <noreply@typo3.com>
Tested-by: Benni Mack <benni@typo3.org>
Reviewed-by: Benni Mack <benni@typo3.org>
  • Loading branch information
alexanderschnitzler authored and bmack committed Sep 28, 2020
1 parent 7e1d81d commit c8b718f
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 39 deletions.
6 changes: 3 additions & 3 deletions typo3/sysext/impexp/Classes/Command/ImportCommand.php
Expand Up @@ -82,10 +82,10 @@ protected function configure()
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$fileName = $input->getArgument('file');
$fileName = (string)$input->getArgument('file');
$fileName = GeneralUtility::getFileAbsFileName($fileName);
if (empty($fileName) || !file_exists($fileName)) {
throw new InvalidFileException('The given filename "' . ($fileName ?? $input->getArgument('file')) . '" could not be found', 1484483040);
if ($fileName === '' || !file_exists($fileName)) {
throw new InvalidFileException('The given filename "' . $fileName . '" could not be found', 1484483040);
}

$io = new SymfonyStyle($input, $output);
Expand Down
8 changes: 4 additions & 4 deletions typo3/sysext/impexp/Classes/Controller/ExportController.php
Expand Up @@ -144,7 +144,7 @@ protected function exportData(array $inData)
{
// BUILDING EXPORT DATA:
// Processing of InData array values:
$inData['filename'] = trim(preg_replace('/[^[:alnum:]._-]*/', '', preg_replace('/\\.(t3d|xml)$/', '', $inData['filename'])));
$inData['filename'] = trim((string)preg_replace('/[^[:alnum:]._-]*/', '', preg_replace('/\\.(t3d|xml)$/', '', $inData['filename'])));
if ($inData['filename'] !== '') {
$inData['filename'] .= $inData['filetype'] === 'xml' ? '.xml' : '.t3d';
}
Expand Down Expand Up @@ -194,7 +194,7 @@ protected function exportData(array $inData)
if (is_array($inData['record'])) {
foreach ($inData['record'] as $ref) {
$rParts = explode(':', $ref);
$this->export->export_addRecord($rParts[0], BackendUtility::getRecord($rParts[0], $rParts[1]));
$this->export->export_addRecord($rParts[0], BackendUtility::getRecord($rParts[0], (int)$rParts[1]));
}
}
// Configure which tables to export
Expand Down Expand Up @@ -465,7 +465,7 @@ protected function makeConfigurationForm(array $inData): void
$rParts = explode(':', $ref);
[$tName, $rUid] = $rParts;
$nameSuggestion .= $tName . '_' . $rUid;
$rec = BackendUtility::getRecordWSOL($tName, $rUid);
$rec = BackendUtility::getRecordWSOL((string)$tName, (int)$rUid);
if (!empty($rec)) {
$records[] = [
'icon' => $this->iconFactory->getIconForRecord($tName, $rec, Icon::SIZE_SMALL)->render(),
Expand All @@ -491,7 +491,7 @@ protected function makeConfigurationForm(array $inData): void
if ($referenceParts[1] === '0') {
$iconAndTitle = $this->iconFactory->getIcon('apps-pagetree-root', Icon::SIZE_SMALL)->render() . $GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename'];
} else {
$record = BackendUtility::getRecordWSOL('pages', $referenceParts[1]);
$record = BackendUtility::getRecordWSOL('pages', (int)$referenceParts[1]);
$iconAndTitle = $this->iconFactory->getIconForRecord('pages', $record, Icon::SIZE_SMALL)->render()
. BackendUtility::getRecordTitle('pages', $record, true);
}
Expand Down
12 changes: 6 additions & 6 deletions typo3/sysext/impexp/Classes/Export.php
Expand Up @@ -720,7 +720,7 @@ public function export_addFile($fI, $recordRef = '', $fieldname = '')
$fileMd5 = md5_file($fI['ID_absFile']);
if (!$this->saveFilesOutsideExportFile) {
// ... and finally add the heavy stuff:
$fileRec['content'] = file_get_contents($fI['ID_absFile']);
$fileRec['content'] = (string)file_get_contents($fI['ID_absFile']);
} else {
GeneralUtility::upload_copy_move($fI['ID_absFile'], $this->getTemporaryFilesPathForExport() . $fileMd5);
}
Expand All @@ -734,7 +734,7 @@ public function export_addFile($fI, $recordRef = '', $fieldname = '')
if ($this->includeExtFileResources && GeneralUtility::inList($this->extFileResourceExtensions, strtolower($html_fI['extension']))) {
$uniquePrefix = '###' . md5($GLOBALS['EXEC_TIME']) . '###';
if (strtolower($html_fI['extension']) === 'css') {
$prefixedMedias = explode($uniquePrefix, preg_replace('/(url[[:space:]]*\\([[:space:]]*["\']?)([^"\')]*)(["\']?[[:space:]]*\\))/i', '\\1' . $uniquePrefix . '\\2' . $uniquePrefix . '\\3', $fileRec['content']));
$prefixedMedias = explode($uniquePrefix, (string)preg_replace('/(url[[:space:]]*\\([[:space:]]*["\']?)([^"\')]*)(["\']?[[:space:]]*\\))/i', '\\1' . $uniquePrefix . '\\2' . $uniquePrefix . '\\3', $fileRec['content']));
} else {
// html, htm:
$htmlParser = GeneralUtility::makeInstance(HtmlParser::class);
Expand Down Expand Up @@ -762,7 +762,7 @@ public function export_addFile($fI, $recordRef = '', $fieldname = '')
// Setting this data in the header
$this->dat['header']['files'][$EXTres_ID] = $fileRec;
// ... and finally add the heavy stuff:
$fileRec['content'] = file_get_contents($EXTres_absPath);
$fileRec['content'] = (string)file_get_contents($EXTres_absPath);
$fileRec['content_md5'] = md5($fileRec['content']);
$this->dat['files'][$EXTres_ID] = $fileRec;
}
Expand Down Expand Up @@ -1033,15 +1033,15 @@ public function doOutputCompress()
/**
* Returns a content part for a filename being build.
*
* @param array $data Data to store in part
* @param string $data Data to store in part
* @param bool $compress Compress file?
* @return string Content stream.
*/
public function addFilePart($data, $compress = false)
{
if ($compress) {
$data = gzcompress($data);
$data = (string)gzcompress($data);
}
return md5($data) . ':' . ($compress ? '1' : '0') . ':' . str_pad(strlen($data), 10, '0', STR_PAD_LEFT) . ':' . $data . ':';
return md5($data) . ':' . ($compress ? '1' : '0') . ':' . str_pad((string)strlen($data), 10, '0', STR_PAD_LEFT) . ':' . $data . ':';
}
}
40 changes: 20 additions & 20 deletions typo3/sysext/impexp/Classes/Import.php
Expand Up @@ -595,7 +595,7 @@ public function writeRecords_pages($pid)
if (!empty($pageRecords)) {
$remainingPageUids = array_keys($pageRecords);
foreach ($remainingPageUids as $pUid) {
$this->addSingle('pages', $pUid, $pid);
$this->addSingle('pages', (int)$pUid, $pid);
}
}
// Now write to database:
Expand Down Expand Up @@ -696,6 +696,7 @@ public function writeRecords_records($pid)
$this->import_data = [];
if (is_array($this->dat['header']['records'])) {
foreach ($this->dat['header']['records'] as $table => $recs) {
$table = (string)$table;
$this->addGeneralErrorsByTable($table);
if ($table !== 'pages') {
foreach ($recs as $uid => $thisRec) {
Expand Down Expand Up @@ -1418,7 +1419,7 @@ public function processSoftReferences_saveFile($relFileName, $cfg, $table, $uid)
if (GeneralUtility::isFirstPartOfStr($dirPrefix, $this->fileadminFolderName . '/')) {
// File in fileadmin/ folder:
// Create file (and possible resources)
$newFileName = $this->processSoftReferences_saveFile_createRelFile($dirPrefix, PathUtility::basename($relFileName), $cfg['file_ID'], $table, $uid);
$newFileName = $this->processSoftReferences_saveFile_createRelFile($dirPrefix, PathUtility::basename($relFileName), $cfg['file_ID'], $table, $uid) ?: '';
if (strlen($newFileName)) {
$relFileName = $newFileName;
} else {
Expand Down Expand Up @@ -1452,7 +1453,7 @@ public function processSoftReferences_saveFile_createRelFile($origDirPrefix, $fi
return PathUtility::stripPathSitePrefix($this->fileIDMap[$fileID]);
}
// Verify FileMount access to dir-prefix. Returns the best alternative relative path if any
$dirPrefix = $this->verifyFolderAccess($origDirPrefix);
$dirPrefix = (string)$this->verifyFolderAccess($origDirPrefix);
if ($dirPrefix && (!$this->update || $origDirPrefix === $dirPrefix) && $this->checkOrCreateDir($dirPrefix)) {
$fileHeaderInfo = $this->dat['header']['files'][$fileID];
$updMode = $this->update && $this->import_mapId[$table][$uid] === $uid && $this->import_mode[$table . ':' . $uid] !== 'as_new';
Expand All @@ -1465,7 +1466,7 @@ public function processSoftReferences_saveFile_createRelFile($origDirPrefix, $fi
} else {
// Create unique filename:
$fileProcObj = $this->getFileProcObj();
$newName = $fileProcObj->getUniqueName($fileName, Environment::getPublicPath() . '/' . $dirPrefix);
$newName = (string)$fileProcObj->getUniqueName($fileName, Environment::getPublicPath() . '/' . $dirPrefix);
}
if ($this->writeFileVerify($newName, $fileID)) {
// If the resource was an HTML/CSS file with resources attached, we will write those as well!
Expand Down Expand Up @@ -1500,7 +1501,7 @@ public function processSoftReferences_saveFile_createRelFile($origDirPrefix, $fi
if (GeneralUtility::mkdir($resourceDir)) {
foreach ($fileHeaderInfo['EXT_RES_ID'] as $res_fileID) {
if ($this->dat['files'][$res_fileID]['filename']) {
$absResourceFileName = $fileProcObj->getUniqueName($this->dat['files'][$res_fileID]['filename'], $resourceDir);
$absResourceFileName = (string)$fileProcObj->getUniqueName($this->dat['files'][$res_fileID]['filename'], $resourceDir);
$relResourceFileName = substr($absResourceFileName, strlen(PathUtility::dirname($resourceDir)) + 1);
$this->writeFileVerify($absResourceFileName, $res_fileID);
$tokenizedContent = str_replace('{EXT_RES_ID:' . $res_fileID . '}', $relResourceFileName, $tokenizedContent);
Expand Down Expand Up @@ -1559,7 +1560,7 @@ public function writeFileVerify($fileName, $fileID, $bypassMountCheck = false)
}
GeneralUtility::writeFile($fileName, $this->dat['files'][$fileID]['content']);
$this->fileIDMap[$fileID] = $fileName;
if (hash_equals(md5(file_get_contents($fileName)), $this->dat['files'][$fileID]['content_md5'])) {
if (hash_equals(md5((string)file_get_contents($fileName)), $this->dat['files'][$fileID]['content_md5'])) {
return true;
}
$this->error('ERROR: File content "' . $fileName . '" was corrupted');
Expand Down Expand Up @@ -1628,7 +1629,7 @@ public function loadFile($filename, $all = false)
}
if (strtolower($fI['extension']) === 'xml') {
// XML:
$xmlContent = file_get_contents($filename);
$xmlContent = (string)file_get_contents($filename);
if (strlen($xmlContent)) {
$this->dat = GeneralUtility::xml2array($xmlContent, '', true);
if (is_array($this->dat)) {
Expand All @@ -1646,18 +1647,17 @@ public function loadFile($filename, $all = false)
} else {
// T3D
if ($fd = fopen($filename, 'rb')) {
$this->dat['header'] = $this->getNextFilePart($fd, 1, 'header');
$this->dat['header'] = $this->getNextFilePart($fd, true, 'header');
if ($all) {
$this->dat['records'] = $this->getNextFilePart($fd, 1, 'records');
$this->dat['files'] = $this->getNextFilePart($fd, 1, 'files');
$this->dat['files_fal'] = $this->getNextFilePart($fd, 1, 'files_fal');
$this->dat['records'] = $this->getNextFilePart($fd, true, 'records');
$this->dat['files'] = $this->getNextFilePart($fd, true, 'files');
$this->dat['files_fal'] = $this->getNextFilePart($fd, true, 'files_fal');
}
$this->loadInit();
fclose($fd);
return true;
}
$this->error('Error opening file: ' . $filename);

fclose($fd);
}
return false;
}
Expand Down Expand Up @@ -1690,12 +1690,12 @@ public function getNextFilePart($fd, $unserialize = false, $name = '')
$this->error('File read error: InitString had a wrong length. (' . $name . ')');
return null;
}
$datString = fread($fd, (int)$initStrDat[2]);
$datString = (string)fread($fd, (int)$initStrDat[2]);
fread($fd, 1);
if (hash_equals($initStrDat[0], md5($datString))) {
if ($initStrDat[1]) {
if ($this->compress) {
$datString = gzuncompress($datString);
$datString = (string)gzuncompress($datString);
} else {
$this->error('Content read error: This file requires decompression, but this server does not offer gzcompress()/gzuncompress() functions.');
return null;
Expand All @@ -1717,9 +1717,9 @@ public function getNextFilePart($fd, $unserialize = false, $name = '')
public function loadContent($filecontent)
{
$pointer = 0;
$this->dat['header'] = $this->getNextContentPart($filecontent, $pointer, 1, 'header');
$this->dat['records'] = $this->getNextContentPart($filecontent, $pointer, 1, 'records');
$this->dat['files'] = $this->getNextContentPart($filecontent, $pointer, 1, 'files');
$this->dat['header'] = $this->getNextContentPart($filecontent, $pointer, true, 'header');
$this->dat['records'] = $this->getNextContentPart($filecontent, $pointer, true, 'records');
$this->dat['files'] = $this->getNextContentPart($filecontent, $pointer, true, 'files');
$this->loadInit();
}

Expand All @@ -1743,12 +1743,12 @@ public function getNextContentPart($filecontent, &$pointer, $unserialize = false
$this->error('Content read error: InitString had a wrong length. (' . $name . ')');
return null;
}
$datString = substr($filecontent, $pointer, (int)$initStrDat[2]);
$datString = (string)substr($filecontent, $pointer, (int)$initStrDat[2]);
$pointer += (int)$initStrDat[2] + 1;
if (hash_equals($initStrDat[0], md5($datString))) {
if ($initStrDat[1]) {
if ($this->compress) {
$datString = gzuncompress($datString);
$datString = (string)gzuncompress($datString);
return $unserialize ? unserialize($datString, ['allowed_classes' => false]) : $datString;
}
$this->error('Content read error: This file requires decompression, but this server does not offer gzcompress()/gzuncompress() functions.');
Expand Down
13 changes: 7 additions & 6 deletions typo3/sysext/impexp/Classes/ImportExport.php
Expand Up @@ -365,6 +365,7 @@ public function traversePageTree($pT, &$lines, $preCode = '')
// Subrecords:
if (is_array($this->dat['header']['pid_lookup'][$k])) {
foreach ($this->dat['header']['pid_lookup'][$k] as $t => $recUidArr) {
$t = (string)$t;
if ($t !== 'pages') {
foreach ($recUidArr as $ruid => $value) {
$this->singleRecordLines($t, $ruid, $lines, $preCode . '&nbsp;&nbsp;&nbsp;&nbsp;');
Expand Down Expand Up @@ -429,19 +430,19 @@ protected function excludePageAndRecords($pageUid, $pageTree)
/**
* Go through remaining pages (not in tree)
*
* @param array $pT Page tree array with uid/subrow (from ->dat[header][pagetree]
* @param array<int, array> $pT Page tree array with uid/subrow (from ->dat[header][pagetree]
* @param array $lines Output lines array (is passed by reference and modified)
*/
public function traversePageRecords($pT, &$lines)
{
foreach ($pT as $k => $rHeader) {
$this->singleRecordLines('pages', $k, $lines, '', 1);
$this->singleRecordLines('pages', (int)$k, $lines, '', true);
// Subrecords:
if (is_array($this->dat['header']['pid_lookup'][$k])) {
foreach ($this->dat['header']['pid_lookup'][$k] as $t => $recUidArr) {
if ($t !== 'pages') {
foreach ($recUidArr as $ruid => $value) {
$this->singleRecordLines($t, $ruid, $lines, '&nbsp;&nbsp;&nbsp;&nbsp;');
$this->singleRecordLines((string)$t, (int)$ruid, $lines, '&nbsp;&nbsp;&nbsp;&nbsp;');
}
}
}
Expand All @@ -463,7 +464,7 @@ public function traverseAllRecords($pT, &$lines)
if ($t !== 'pages') {
$preCode = '';
foreach ($recUidArr as $ruid => $value) {
$this->singleRecordLines($t, $ruid, $lines, $preCode, 1);
$this->singleRecordLines((string)$t, (int)$ruid, $lines, $preCode, true);
}
}
}
Expand Down Expand Up @@ -1133,7 +1134,7 @@ public function compareRecords($databaseRecord, $importRecord, $table, $inverseD
if (isset($importRecord[$fN])) {
if (trim($databaseRecord[$fN]) !== trim($importRecord[$fN])) {
// Create diff-result:
$output[$fN] = $diffUtility->makeDiffDisplay(BackendUtility::getProcessedValue($table, $fN, !$inverseDiff ? $importRecord[$fN] : $databaseRecord[$fN], 0, 1, 1), BackendUtility::getProcessedValue($table, $fN, !$inverseDiff ? $databaseRecord[$fN] : $importRecord[$fN], 0, 1, 1));
$output[$fN] = $diffUtility->makeDiffDisplay(BackendUtility::getProcessedValue($table, $fN, !$inverseDiff ? $importRecord[$fN] : $databaseRecord[$fN], 0, true, true), BackendUtility::getProcessedValue($table, $fN, !$inverseDiff ? $databaseRecord[$fN] : $importRecord[$fN], 0, true, true));
}
unset($importRecord[$fN]);
}
Expand All @@ -1151,7 +1152,7 @@ public function compareRecords($databaseRecord, $importRecord, $table, $inverseD
foreach ($output as $fN => $state) {
$tRows[] = '
<tr>
<td>' . htmlspecialchars($this->getLanguageService()->sL($GLOBALS['TCA'][$table]['columns'][$fN]['label'])) . ' (' . htmlspecialchars($fN) . ')</td>
<td>' . htmlspecialchars($this->getLanguageService()->sL($GLOBALS['TCA'][$table]['columns'][$fN]['label'])) . ' (' . htmlspecialchars((string)$fN) . ')</td>
<td>' . $state . '</td>
</tr>
';
Expand Down

0 comments on commit c8b718f

Please sign in to comment.