Skip to content

Commit 1c0bb79

Browse files
bmackgeorgringer
authored andcommitted
[!!!][TASK] Use FileType Enum in FAL
In TYPO3 v13 with #102032, the getType() and FileType constants were replaced by a BackedEnum "FileType" which was kept as return type integer for backwards-compatibility. Resolves: #106417 Related: #102032 Related: #105377 Releases: main Change-Id: Ief4926a9864e1478f0083cfc9923387ec0cdd486 Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/88697 Reviewed-by: Garvin Hicking <gh@faktor-e.de> Tested-by: core-ci <typo3@b13.com> Reviewed-by: Georg Ringer <georg.ringer@gmail.com> Tested-by: Georg Ringer <georg.ringer@gmail.com> Reviewed-by: Stefan Bürk <stefan@buerk.tech> Tested-by: Stefan Bürk <stefan@buerk.tech> Tested-by: Garvin Hicking <gh@faktor-e.de>
1 parent eddd4ce commit 1c0bb79

File tree

7 files changed

+30
-29
lines changed

7 files changed

+30
-29
lines changed

typo3/sysext/backend/Classes/Form/Element/FileInfoElement.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ protected function renderFileInformationContent(?File $file = null): string
8484
}
8585
$content .= '<strong>' . htmlspecialchars($file->getName()) . '</strong>';
8686
$content .= ' (' . htmlspecialchars(GeneralUtility::formatSize((int)$file->getSize())) . 'bytes)<br />';
87-
$content .= BackendUtility::getProcessedValue('sys_file', 'type', (string)$file->getType()) . ' (' . $file->getMimeType() . ')<br />';
87+
$content .= BackendUtility::getProcessedValue('sys_file', 'type', (string)$file->getType()->value) . ' (' . $file->getMimeType() . ')<br />';
8888
$content .= htmlspecialchars($lang->sL('LLL:EXT:core/Resources/Private/Language/locallang_misc.xlf:fileMetaDataLocation')) . ': ';
8989
$content .= '<a href="' . htmlspecialchars($file->getPublicUrl() ?? '') . '" target="_blank" title="' . $this->getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:cm.view') . '">' . htmlspecialchars($file->getStorage()->getName()) . ' - ' . htmlspecialchars($file->getIdentifier()) . '</a><br />';
9090
$content .= '<br />';

typo3/sysext/core/Classes/Resource/AbstractFile.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -222,25 +222,21 @@ public function getMimeType(): string
222222
* "video"
223223
* "other"
224224
* see FileType enum
225-
*
226-
* @return int $fileType
227-
* @todo will return an instance of FileType enum in TYPO3 v14.0
228225
*/
229-
#[\ReturnTypeWillChange]
230-
public function getType()
226+
public function getType(): FileType
231227
{
232228
// this basically extracts the mimetype and guess the filetype based
233229
// on the first part of the mimetype works for 99% of all cases, and
234230
// we don't need to make an SQL statement like EXT:media does currently
235231
if (!($this->properties['type'] ?? false)) {
236232
$this->properties['type'] = FileType::tryFromMimeType($this->getMimeType())->value;
237233
}
238-
return (int)$this->properties['type'];
234+
return $this->properties['type'] instanceof FileType ? $this->properties['type'] : FileType::from($this->properties['type']);
239235
}
240236

241237
public function isType(FileType $fileType): bool
242238
{
243-
return FileType::tryFrom($this->getType()) === $fileType;
239+
return $this->getType() === $fileType;
244240
}
245241

246242
/**

typo3/sysext/core/Classes/Resource/FileReference.php

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -296,19 +296,14 @@ public function getCreationTime(): int
296296
return (int)$this->originalFile->getCreationTime();
297297
}
298298

299-
/**
300-
* Returns the fileType of this file
301-
*
302-
* @return int $fileType
303-
*/
304-
public function getType()
299+
public function getType(): FileType
305300
{
306-
return (int)$this->originalFile->getType();
301+
return $this->originalFile->getType();
307302
}
308303

309304
public function isType(FileType $fileType): bool
310305
{
311-
return FileType::tryFrom($this->getType()) === $fileType;
306+
return $this->getType() === $fileType;
312307
}
313308

314309
/**

typo3/sysext/core/Classes/Resource/Service/ExtractorService.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
namespace TYPO3\CMS\Core\Resource\Service;
1919

2020
use TYPO3\CMS\Core\Resource\File;
21+
use TYPO3\CMS\Core\Resource\FileType;
2122
use TYPO3\CMS\Core\Resource\Index\ExtractorInterface;
2223
use TYPO3\CMS\Core\Resource\Index\ExtractorRegistry;
2324
use TYPO3\CMS\Core\Utility\GeneralUtility;
@@ -77,12 +78,19 @@ protected function getExtractionServices(string $driverType): array
7778
*/
7879
private function isFileTypeSupportedByExtractor(File $file, ExtractorInterface $extractor): bool
7980
{
80-
$isSupported = true;
81-
$fileTypeRestrictions = $extractor->getFileTypeRestrictions();
82-
if (!empty($fileTypeRestrictions) && !in_array($file->getType(), $fileTypeRestrictions, true)) {
83-
$isSupported = false;
81+
$supportedFileTypes = $extractor->getFileTypeRestrictions();
82+
if ($supportedFileTypes === []) {
83+
return true;
8484
}
85-
return $isSupported;
85+
foreach ($supportedFileTypes as $supportedFileType) {
86+
if (is_int($supportedFileType)) {
87+
$supportedFileType = FileType::tryFrom($supportedFileType);
88+
}
89+
if ($supportedFileType === $file->getType()) {
90+
return true;
91+
}
92+
}
93+
return false;
8694
}
8795

8896
/**

typo3/sysext/core/Documentation/Changelog/14.0/Breaking-105377-DeprecatedFunctionalityRemoved.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,11 @@ The following methods changed signature according to previous deprecations in v1
119119
- :php:`\TYPO3\CMS\Backend\View\BackendLayout\DataProviderContext->__construct()` - All arguments are now mandatory :ref:`(Deprecation entry) <deprecation-105252-1728471144>`
120120
- :php:`\TYPO3\CMS\Core\Imaging\IconFactory->getIcon()` (argument 4 is now of type :php:`\TYPO3\CMS\Core\Imaging\IconState|null`) :ref:`(Deprecation entry) <deprecation-101133-1687875352>`
121121
- :php:`\TYPO3\CMS\Core\Resource\AbstractFile->copyTo()` (argument 3 is now of type :php:`\TYPO3\CMS\Core\Resource\Enum\DuplicationBehavior`) :ref:`(Deprecation entry) <deprecation-101151-1688113521>`
122+
- :php:`\TYPO3\CMS\Core\Resource\AbstractFile->getType()` (return type is now :php:`\TYPO3\CMS\Core\Resource\FileType`) :ref:`(Deprecation entry) <deprecation-101151-1688113521>`
122123
- :php:`\TYPO3\CMS\Core\Resource\AbstractFile->moveTo()` (argument 3 is now of type :php:`\TYPO3\CMS\Core\Resource\Enum\DuplicationBehavior`) :ref:`(Deprecation entry) <deprecation-101151-1688113521>`
123124
- :php:`\TYPO3\CMS\Core\Resource\AbstractFile->rename()` (argument 2 is now of type :php:`\TYPO3\CMS\Core\Resource\Enum\DuplicationBehavior`) :ref:`(Deprecation entry) <deprecation-101151-1688113521>`
124125
- :php:`\TYPO3\CMS\Core\Resource\FileInterface->rename()` (argument 2 is now of type :php:`\TYPO3\CMS\Core\Resource\Enum\DuplicationBehavior`) :ref:`(Deprecation entry) <deprecation-101151-1688113521>`
126+
- :php:`\TYPO3\CMS\Core\Resource\FileReference->getType()` (return type is now :php:`\TYPO3\CMS\Core\Resource\FileType`) :ref:`(Deprecation entry) <deprecation-101151-1688113521>`
125127
- :php:`\TYPO3\CMS\Core\Resource\FileReference->rename()` (argument 2 is now of type :php:`\TYPO3\CMS\Core\Resource\Enum\DuplicationBehavior`) :ref:`(Deprecation entry) <deprecation-101151-1688113521>`
126128
- :php:`\TYPO3\CMS\Core\Resource\Folder->addFile()` (argument 3 is now of type :php:`\TYPO3\CMS\Core\Resource\Enum\DuplicationBehavior`) :ref:`(Deprecation entry) <deprecation-101151-1688113521>`
127129
- :php:`\TYPO3\CMS\Core\Resource\Folder->addUploadedFile()` (argument 2 is now of type :php:`\TYPO3\CMS\Core\Resource\Enum\DuplicationBehavior`) :ref:`(Deprecation entry) <deprecation-101151-1688113521>`

typo3/sysext/core/Tests/Unit/Resource/Index/IndexerTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public function extractMetaDataCallsSubsequentMethodsWithCorrectArguments(): voi
4343

4444
$fileMock = $this->createMock(File::class);
4545
$fileMock->method('getUid')->willReturn(42);
46-
$fileMock->method('getType')->willReturn(FileType::TEXT->value);
46+
$fileMock->method('getType')->willReturn(FileType::TEXT);
4747
$fileMock->method('getStorage')->willReturn($mockStorage);
4848

4949
$extractorServiceMock = $this->getMockBuilder(ExtractorService::class)->getMock();

typo3/sysext/core/Tests/Unit/Resource/Service/ExtractorServiceTest.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ final class ExtractorServiceTest extends UnitTestCase
3434
public function isFileTypeSupportedByExtractorReturnsFalseForFileTypeTextAndExtractorLimitedToFileTypeImage(): void
3535
{
3636
$fileMock = $this->createMock(File::class);
37-
$fileMock->method('getType')->willReturn(FileType::TEXT->value);
37+
$fileMock->method('getType')->willReturn(FileType::TEXT);
3838

3939
$extractorMock = $this->createMock(ExtractorInterface::class);
40-
$extractorMock->method('getFileTypeRestrictions')->willReturn([FileType::IMAGE->value]);
40+
$extractorMock->method('getFileTypeRestrictions')->willReturn([FileType::IMAGE]);
4141

4242
$extractorService = new ExtractorService();
4343
$method = new \ReflectionMethod($extractorService, 'isFileTypeSupportedByExtractor');
@@ -54,10 +54,10 @@ public function isFileTypeSupportedByExtractorReturnsFalseForFileTypeTextAndExtr
5454
public function isFileTypeSupportedByExtractorReturnsTrueForFileTypeImageAndExtractorLimitedToFileTypeImage(): void
5555
{
5656
$fileMock = $this->createMock(File::class);
57-
$fileMock->method('getType')->willReturn(FileType::IMAGE->value);
57+
$fileMock->method('getType')->willReturn(FileType::IMAGE);
5858

5959
$extractorMock = $this->createMock(ExtractorInterface::class);
60-
$extractorMock->method('getFileTypeRestrictions')->willReturn([FileType::IMAGE->value]);
60+
$extractorMock->method('getFileTypeRestrictions')->willReturn([FileType::IMAGE]);
6161

6262
$extractorService = new ExtractorService();
6363
$method = new \ReflectionMethod($extractorService, 'isFileTypeSupportedByExtractor');
@@ -74,7 +74,7 @@ public function isFileTypeSupportedByExtractorReturnsTrueForFileTypeImageAndExtr
7474
public function isFileTypeSupportedByExtractorReturnsTrueForFileTypeTextAndExtractorHasNoFileTypeLimitation(): void
7575
{
7676
$fileMock = $this->createMock(File::class);
77-
$fileMock->method('getType')->willReturn(FileType::TEXT->value);
77+
$fileMock->method('getType')->willReturn(FileType::TEXT);
7878

7979
$extractorMock = $this->createMock(ExtractorInterface::class);
8080
$extractorMock->method('getFileTypeRestrictions')->willReturn([]);
@@ -103,7 +103,7 @@ public function extractMetaDataComposesDataByAvailableExtractors(): void
103103

104104
$fileMock = $this->createMock(File::class);
105105
$fileMock->method('getUid')->willReturn(4711);
106-
$fileMock->method('getType')->willReturn(FileType::IMAGE->value);
106+
$fileMock->method('getType')->willReturn(FileType::IMAGE);
107107
$fileMock->method('getStorage')->willReturn($storageMock);
108108

109109
$extractorClass1 = StringUtility::getUniqueId('extractor');
@@ -222,7 +222,7 @@ public function extractMetaDataComposesDataByAvailableExtractorsWithDifferentPri
222222

223223
$fileMock = $this->createMock(File::class);
224224
$fileMock->expects(self::any())->method('getUid')->willReturn(4711);
225-
$fileMock->expects(self::any())->method('getType')->willReturn(FileType::IMAGE->value);
225+
$fileMock->expects(self::any())->method('getType')->willReturn(FileType::IMAGE);
226226
$fileMock->expects(self::any())->method('getStorage')->willReturn($storageMock);
227227

228228
$extractorClass1 = StringUtility::getUniqueId('extractor');

0 commit comments

Comments
 (0)