From 1c96bea27e0d785fe176c9a65f2090103f71da1c Mon Sep 17 00:00:00 2001 From: Alex Pott Date: Mon, 6 Mar 2023 12:07:59 +0000 Subject: [PATCH] Issue #3262358 by mfb, smustgrave, joachim: Fix type hints in FileInterface to align with reality --- core/modules/file/src/Entity/File.php | 6 ++- core/modules/file/src/FileInterface.php | 38 ++++++++++--------- .../src/Kernel/Migrate/d7/MigrateFileTest.php | 2 +- .../Migrate/d7/MigratePrivateFileTest.php | 2 +- .../d6/MigrateUserPictureD6FileTest.php | 2 +- 5 files changed, 28 insertions(+), 22 deletions(-) diff --git a/core/modules/file/src/Entity/File.php b/core/modules/file/src/Entity/File.php index 2b10770d6a5..fee3b5e2d3d 100644 --- a/core/modules/file/src/Entity/File.php +++ b/core/modules/file/src/Entity/File.php @@ -113,7 +113,8 @@ class File extends ContentEntityBase implements FileInterface { * {@inheritdoc} */ public function getSize() { - return $this->get('filesize')->value; + $filesize = $this->get('filesize')->value; + return isset($filesize) ? (int) $filesize : NULL; } /** @@ -127,7 +128,8 @@ class File extends ContentEntityBase implements FileInterface { * {@inheritdoc} */ public function getCreatedTime() { - return $this->get('created')->value; + $created = $this->get('created')->value; + return isset($created) ? (int) $created : NULL; } /** diff --git a/core/modules/file/src/FileInterface.php b/core/modules/file/src/FileInterface.php index c90575aa4f9..588009cb475 100644 --- a/core/modules/file/src/FileInterface.php +++ b/core/modules/file/src/FileInterface.php @@ -28,25 +28,27 @@ interface FileInterface extends ContentEntityInterface, EntityChangedInterface, * This may differ from the basename of the URI if the file is renamed to * avoid overwriting an existing file. * - * @return string - * Name of the file. + * @return string|null + * Name of the file, or NULL if unknown. */ public function getFilename(); /** * Sets the name of the file. * - * @param string $filename - * The file name that corresponds to this file. May differ from the basename - * of the URI and changing the filename does not change the URI. + * @param string|null $filename + * The file name that corresponds to this file, or NULL if unknown. May + * differ from the basename of the URI and changing the filename does not + * change the URI. */ public function setFilename($filename); /** * Returns the URI of the file. * - * @return string - * The URI of the file, e.g. public://directory/file.jpg. + * @return string|null + * The URI of the file, e.g. public://directory/file.jpg, or NULL if it has + * not yet been set. */ public function getFileUri(); @@ -75,32 +77,34 @@ interface FileInterface extends ContentEntityInterface, EntityChangedInterface, /** * Returns the MIME type of the file. * - * @return string - * The MIME type of the file, e.g. image/jpeg or text/xml. + * @return string|null + * The MIME type of the file, e.g. image/jpeg or text/xml, or NULL if it + * could not be determined. */ public function getMimeType(); /** * Sets the MIME type of the file. * - * @param string $mime - * The MIME type of the file, e.g. image/jpeg or text/xml. + * @param string|null $mime + * The MIME type of the file, e.g. image/jpeg or text/xml, or NULL if it + * could not be determined. */ public function setMimeType($mime); /** * Returns the size of the file. * - * @return string - * The size of the file in bytes. + * @return int|null + * The size of the file in bytes, or NULL if it could not be determined. */ public function getSize(); /** * Sets the size of the file. * - * @param int $size - * The size of the file in bytes. + * @param int|null $size + * The size of the file in bytes, or NULL if it could not be determined. */ public function setSize($size); @@ -133,8 +137,8 @@ interface FileInterface extends ContentEntityInterface, EntityChangedInterface, /** * Returns the file entity creation timestamp. * - * @return int - * Creation timestamp of the file entity. + * @return int|null + * Creation timestamp of the file entity, or NULL if unknown. */ public function getCreatedTime(); diff --git a/core/modules/file/tests/src/Kernel/Migrate/d7/MigrateFileTest.php b/core/modules/file/tests/src/Kernel/Migrate/d7/MigrateFileTest.php index fcfe5adcfe6..90e021cf32a 100644 --- a/core/modules/file/tests/src/Kernel/Migrate/d7/MigrateFileTest.php +++ b/core/modules/file/tests/src/Kernel/Migrate/d7/MigrateFileTest.php @@ -43,7 +43,7 @@ class MigrateFileTest extends MigrateDrupal7TestBase { * Tests that all expected files are migrated. */ public function testFileMigration() { - $this->assertEntity(1, 'cube.jpeg', 'public://cube.jpeg', 'image/jpeg', '3620', '1421727515', '1421727515', '1'); + $this->assertEntity(1, 'cube.jpeg', 'public://cube.jpeg', 'image/jpeg', 3620, 1421727515, '1421727515', '1'); // Ensure temporary file was not migrated. $this->assertNull(File::load(4)); } diff --git a/core/modules/file/tests/src/Kernel/Migrate/d7/MigratePrivateFileTest.php b/core/modules/file/tests/src/Kernel/Migrate/d7/MigratePrivateFileTest.php index f9ecfc7fb6e..6244efaa5a4 100644 --- a/core/modules/file/tests/src/Kernel/Migrate/d7/MigratePrivateFileTest.php +++ b/core/modules/file/tests/src/Kernel/Migrate/d7/MigratePrivateFileTest.php @@ -53,7 +53,7 @@ class MigratePrivateFileTest extends MigrateDrupal7TestBase { * Tests that all expected files are migrated. */ public function testFileMigration() { - $this->assertEntity(3, 'Babylon5.txt', 'private://Babylon5.txt', 'text/plain', '3', '1486104045', '1486104045', '1'); + $this->assertEntity(3, 'Babylon5.txt', 'private://Babylon5.txt', 'text/plain', 3, 1486104045, '1486104045', '1'); } } diff --git a/core/modules/user/tests/src/Kernel/Migrate/d6/MigrateUserPictureD6FileTest.php b/core/modules/user/tests/src/Kernel/Migrate/d6/MigrateUserPictureD6FileTest.php index 814fcb4eff8..10cbcbf31da 100644 --- a/core/modules/user/tests/src/Kernel/Migrate/d6/MigrateUserPictureD6FileTest.php +++ b/core/modules/user/tests/src/Kernel/Migrate/d6/MigrateUserPictureD6FileTest.php @@ -40,7 +40,7 @@ class MigrateUserPictureD6FileTest extends MigrateDrupal6TestBase { $this->assertSame('image-test.jpg', $file->getFilename()); $this->assertSame('public://image-test.jpg', $file->getFileUri()); $this->assertSame('2', $file->getOwnerId()); - $this->assertSame('1901', $file->getSize()); + $this->assertSame(1901, $file->getSize()); $this->assertSame('image/jpeg', $file->getMimeType()); $file = array_shift($files);