[BUGFIX] Correct return type for FileReference methods
authorChris Müller <typo3@krue.ml>
Mon, 17 May 2021 06:57:38 +0000 (08:57 +0200)
committerRichard Haeser <richard@richardhaeser.com>
Mon, 17 May 2021 07:26:00 +0000 (09:26 +0200)
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>
typo3/sysext/core/Classes/Resource/FileReference.php
typo3/sysext/core/Tests/Unit/Resource/FileReferenceTest.php

index c25d9d2..0eaad8b 100644 (file)
@@ -198,7 +198,7 @@ class FileReference implements FileInterface
      */
     public function getTitle()
     {
-        return $this->getProperty('title');
+        return (string)$this->getProperty('title');
     }
 
     /**
@@ -210,7 +210,7 @@ class FileReference implements FileInterface
      */
     public function getAlternative()
     {
-        return $this->getProperty('alternative');
+        return (string)$this->getProperty('alternative');
     }
 
     /**
@@ -222,7 +222,7 @@ class FileReference implements FileInterface
      */
     public function getDescription()
     {
-        return $this->getProperty('description');
+        return (string)$this->getProperty('description');
     }
 
     /**
index 0456d3f..0249b43 100644 (file)
@@ -162,4 +162,31 @@ class FileReferenceTest extends UnitTestCase
         $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());
+    }
 }