Skip to content

Commit

Permalink
[BUGFIX] Correct return type for FileReference methods
Browse files Browse the repository at this point in the history
The @return annotation for the following FileReference methods is set
to "string", but these methods can also return null:

- getTitle()
- getAlternative()
- getDescription()

To avoid problems with code quality tools and to ensure type
safety the return value of these methods is always casted to a
string.

Resolves: #94144
Releases: master, 10.4
Change-Id: Id28281ca9b55f20fb02e0de3adfe510f88580141
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/69155
Tested-by: core-ci <typo3@b13.com>
Tested-by: Benni Mack <benni@typo3.org>
Tested-by: Richard Haeser <richard@richardhaeser.com>
Reviewed-by: Benni Mack <benni@typo3.org>
Reviewed-by: Richard Haeser <richard@richardhaeser.com>
  • Loading branch information
brotkrueml authored and haassie committed May 17, 2021
1 parent b50d8a7 commit 023864e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
6 changes: 3 additions & 3 deletions typo3/sysext/core/Classes/Resource/FileReference.php
Expand Up @@ -198,7 +198,7 @@ public function getName()
*/
public function getTitle()
{
return $this->getProperty('title');
return (string)$this->getProperty('title');
}

/**
Expand All @@ -210,7 +210,7 @@ public function getTitle()
*/
public function getAlternative()
{
return $this->getProperty('alternative');
return (string)$this->getProperty('alternative');
}

/**
Expand All @@ -222,7 +222,7 @@ public function getAlternative()
*/
public function getDescription()
{
return $this->getProperty('description');
return (string)$this->getProperty('description');
}

/**
Expand Down
27 changes: 27 additions & 0 deletions typo3/sysext/core/Tests/Unit/Resource/FileReferenceTest.php
Expand Up @@ -162,4 +162,31 @@ public function getReferencePropertyThrowsExceptionForPropertyOnlyAvailableInOri
$fixture = $this->prepareFixture($fileReferenceProperties, $originalFileProperties);
$fixture->getReferenceProperty('file_only_property');
}

/**
* @test
*/
public function getTitleReturnsEmptyStringWhenPropertyValueIsNull(): void
{
$fixture = $this->prepareFixture(['title' => null], []);
self::assertSame('', $fixture->getTitle());
}

/**
* @test
*/
public function getAlternativeReturnsEmptyStringWhenPropertyValueIsNull(): void
{
$fixture = $this->prepareFixture(['alternative' => null], []);
self::assertSame('', $fixture->getAlternative());
}

/**
* @test
*/
public function getDescriptionReturnsEmptyStringWhenPropertyValueIsNull(): void
{
$fixture = $this->prepareFixture(['description' => null], []);
self::assertSame('', $fixture->getDescription());
}
}

0 comments on commit 023864e

Please sign in to comment.