Skip to content

Commit

Permalink
[BUGFIX] Avoid type errors in ResourceFactory methods
Browse files Browse the repository at this point in the history
With #92289 logic has been decoupled from `ResourceFactory`
into the `StorageRepository`. As a side change, native php
types have been added to a couple of methods.

`StorageRepository->findByUid()` parameter type has been set
to integer, which breaks the `null` fallback and retrieving
the default storage `uid: 0` in `ResourceFactory` method
`getStorageObjectFromCombinedIdentifier()`. Even worse is
the fact that `getObjectFromCombinedIdentifier()` does not
even try to cope with a invalid identifier provided.

This change modifies `getObjectFromCombinedIdentifier()` and
`getStorageObjectFromCombinedIdentifier` of `ResourceFactory`
to cope with missing storage identifier by falling back to
the default storage (`0`) with a shuffle of the exploded values.

As a side-effect two PHPStan ignore patterns can be removed
from the PHPStan baseline file.

Used command(s):

  Build/Scripts/runTests.sh -s phpstanGenerateBaseline

Resolves: #102692
Related: #92289
Releases: main, 12.4, 11.5
Change-Id: I8ee9736839f59db7917cb49dd0d62af17ee28cda
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/82233
Tested-by: Stefan Bürk <stefan@buerk.tech>
Tested-by: core-ci <typo3@b13.com>
Reviewed-by: Stefan Bürk <stefan@buerk.tech>
  • Loading branch information
sbuerk committed Dec 19, 2023
1 parent 6dd6df8 commit d9ab1d2
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 19 deletions.
10 changes: 0 additions & 10 deletions Build/phpstan/phpstan-baseline.neon
Expand Up @@ -1440,16 +1440,6 @@ parameters:
count: 1
path: ../../typo3/sysext/core/Classes/Resource/ResourceFactory.php

-
message: "#^Parameter \\#1 \\$uid of method TYPO3\\\\CMS\\\\Core\\\\Resource\\\\StorageRepository\\:\\:findByUid\\(\\) expects int, string given\\.$#"
count: 1
path: ../../typo3/sysext/core/Classes/Resource/ResourceFactory.php

-
message: "#^Parameter \\#1 \\$uid of method TYPO3\\\\CMS\\\\Core\\\\Resource\\\\StorageRepository\\:\\:findByUid\\(\\) expects int, string\\|null given\\.$#"
count: 1
path: ../../typo3/sysext/core/Classes/Resource/ResourceFactory.php

-
message: "#^Call to an undefined method TYPO3\\\\CMS\\\\Core\\\\Resource\\\\FileInterface\\:\\:getCombinedIdentifier\\(\\)\\.$#"
count: 2
Expand Down
26 changes: 17 additions & 9 deletions typo3/sysext/core/Classes/Resource/ResourceFactory.php
Expand Up @@ -379,9 +379,11 @@ public function getFolderObjectFromCombinedIdentifier($identifier)
*/
public function getStorageObjectFromCombinedIdentifier($identifier)
{
$parts = GeneralUtility::trimExplode(':', $identifier);
$storageUid = count($parts) === 2 ? $parts[0] : null;
return $this->storageRepository->findByUid($storageUid);
[$storageId, $objectIdentifier] = array_pad(GeneralUtility::trimExplode(':', $identifier), 2, null);
if (!MathUtility::canBeInterpretedAsInteger($storageId) && $objectIdentifier === null) {
return $this->storageRepository->findByUid(0);
}
return $this->storageRepository->findByUid((int)$storageId);
}

/**
Expand All @@ -394,13 +396,19 @@ public function getStorageObjectFromCombinedIdentifier($identifier)
*/
public function getObjectFromCombinedIdentifier($identifier)
{
[$storageId, $objectIdentifier] = GeneralUtility::trimExplode(':', $identifier);
$storage = $this->storageRepository->findByUid($storageId);
if ($storage->hasFile($objectIdentifier)) {
return $storage->getFile($objectIdentifier);
[$storageId, $objectIdentifier] = array_pad(GeneralUtility::trimExplode(':', $identifier), 2, null);
if (!MathUtility::canBeInterpretedAsInteger($storageId) && $objectIdentifier === null) {
$objectIdentifier = $storageId;
$storageId = 0;
}
if ($storage->hasFolder($objectIdentifier)) {
return $storage->getFolder($objectIdentifier);
if (MathUtility::canBeInterpretedAsInteger($storageId)) {
$storage = $this->storageRepository->findByUid($storageId);
if ($storage->hasFile($objectIdentifier)) {
return $storage->getFile($objectIdentifier);
}
if ($storage->hasFolder($objectIdentifier)) {
return $storage->getFolder($objectIdentifier);
}
}
throw new ResourceDoesNotExistException('Object with identifier "' . $identifier . '" does not exist in storage', 1329647780);
}
Expand Down

0 comments on commit d9ab1d2

Please sign in to comment.