From 6c887e1f48184c4b02243e1eba2bf6a890d95fc5 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 6 Jul 2024 20:31:18 +0200 Subject: [PATCH 1/5] Update post author name to fall back to username at construct time Cherry picks commit https://github.com/hydephp/develop/pull/1782/commits/0dfb841b809d41149ba870b69d09baacf072e18d from https://github.com/hydephp/develop/pull/1782 --- .../src/Framework/Features/Blogging/Models/PostAuthor.php | 4 ++-- packages/framework/tests/Feature/MarkdownPostTest.php | 2 +- packages/framework/tests/Unit/PostAuthorTest.php | 7 +++++++ 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/packages/framework/src/Framework/Features/Blogging/Models/PostAuthor.php b/packages/framework/src/Framework/Features/Blogging/Models/PostAuthor.php index ac6b850bf94..e697fb03bb0 100644 --- a/packages/framework/src/Framework/Features/Blogging/Models/PostAuthor.php +++ b/packages/framework/src/Framework/Features/Blogging/Models/PostAuthor.php @@ -48,7 +48,7 @@ class PostAuthor implements Stringable public function __construct(string $username, ?string $name = null, ?string $website = null) { $this->username = $username; - $this->name = $name; + $this->name = $name ?? $this->username; $this->website = $website; } @@ -87,7 +87,7 @@ public function __toString(): string public function getName(): string { - return $this->name ?? $this->username; + return $this->name; } /** @param array{username?: string, name?: string, website?: string} $data */ diff --git a/packages/framework/tests/Feature/MarkdownPostTest.php b/packages/framework/tests/Feature/MarkdownPostTest.php index 66c76b3a0ba..4040d1c35c7 100644 --- a/packages/framework/tests/Feature/MarkdownPostTest.php +++ b/packages/framework/tests/Feature/MarkdownPostTest.php @@ -25,7 +25,7 @@ public function testConstructorCanCreateANewAuthorInstanceFromUsernameString() $this->assertInstanceOf(PostAuthor::class, $post->author); $this->assertSame('John Doe', $post->author->username); - $this->assertNull($post->author->name); + $this->assertSame('John Doe', $post->author->name); $this->assertNull($post->author->website); } diff --git a/packages/framework/tests/Unit/PostAuthorTest.php b/packages/framework/tests/Unit/PostAuthorTest.php index c6c98a9c5aa..503c59f57d5 100644 --- a/packages/framework/tests/Unit/PostAuthorTest.php +++ b/packages/framework/tests/Unit/PostAuthorTest.php @@ -134,6 +134,13 @@ public function testGetNameHelperReturnsUsernameIfNameIsNotSet() $this->assertEquals('username', $author->getName()); } + public function testNameIsSetToUsernameIfNameIsNotSet() + { + $author = new PostAuthor('username'); + + $this->assertEquals('username', $author->name); + } + public function testToStringHelperReturnsTheName() { $author = new PostAuthor('username', 'John Doe'); From f389ef4fecd10a4ca05dcf82fc9043abbaa92585 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 6 Jul 2024 20:37:32 +0200 Subject: [PATCH 2/5] Remove nullable modifier from author name as it is now never null --- .../src/Framework/Features/Blogging/Models/PostAuthor.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/framework/src/Framework/Features/Blogging/Models/PostAuthor.php b/packages/framework/src/Framework/Features/Blogging/Models/PostAuthor.php index e697fb03bb0..978384fa704 100644 --- a/packages/framework/src/Framework/Features/Blogging/Models/PostAuthor.php +++ b/packages/framework/src/Framework/Features/Blogging/Models/PostAuthor.php @@ -26,7 +26,7 @@ class PostAuthor implements Stringable /** * The display name of the author. */ - public readonly ?string $name; + public readonly string $name; /** * The author's website URL. From afbbb64ca244d487c097f1cbfd1ccc7ea4a413b8 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 6 Jul 2024 20:36:51 +0200 Subject: [PATCH 3/5] Deprecate the `PostAuthor::getName()` method Cherry picks commit https://github.com/hydephp/develop/pull/1782/commits/2f54b9f9aad5a9065ae51ec9df5dc9ee0d023795 from https://github.com/hydephp/develop/pull/1782 --- .../src/Framework/Features/Blogging/Models/PostAuthor.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/packages/framework/src/Framework/Features/Blogging/Models/PostAuthor.php b/packages/framework/src/Framework/Features/Blogging/Models/PostAuthor.php index 978384fa704..f6e47f3fdfa 100644 --- a/packages/framework/src/Framework/Features/Blogging/Models/PostAuthor.php +++ b/packages/framework/src/Framework/Features/Blogging/Models/PostAuthor.php @@ -8,6 +8,7 @@ use Hyde\Facades\Author; use Hyde\Facades\Config; use Illuminate\Support\Collection; +use JetBrains\PhpStorm\Deprecated; use function strtolower; use function is_string; @@ -85,6 +86,10 @@ public function __toString(): string return $this->getName(); } + /** + * @deprecated This is not needed as the name property can be accessed directly. + */ + #[Deprecated(reason: 'Use the name property instead.', replacement: '%class%->name')] public function getName(): string { return $this->name; From 451b7c2d8ee394f231da856e269f0e2de6413143 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 6 Jul 2024 20:41:54 +0200 Subject: [PATCH 4/5] Replace deprecated method usage --- .../src/Framework/Features/Blogging/Models/PostAuthor.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/framework/src/Framework/Features/Blogging/Models/PostAuthor.php b/packages/framework/src/Framework/Features/Blogging/Models/PostAuthor.php index f6e47f3fdfa..3523643804a 100644 --- a/packages/framework/src/Framework/Features/Blogging/Models/PostAuthor.php +++ b/packages/framework/src/Framework/Features/Blogging/Models/PostAuthor.php @@ -83,7 +83,7 @@ public static function all(): Collection public function __toString(): string { - return $this->getName(); + return $this->name; } /** From d74d84ad3bb1dcbc40199070c3ab0e955a7a8a4c Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 6 Jul 2024 20:40:15 +0200 Subject: [PATCH 5/5] Update RELEASE_NOTES.md --- RELEASE_NOTES.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md index d15863f1304..d0b179e8309 100644 --- a/RELEASE_NOTES.md +++ b/RELEASE_NOTES.md @@ -14,9 +14,11 @@ This serves two purposes: ### Changed - Updated the `Serializable` trait to provide a default automatic `toArray` method in https://github.com/hydephp/develop/pull/1791 +- Updated the `PostAuthor` class's `name` property to fall back to the `username` property if the `name` property is not set in https://github.com/hydephp/develop/pull/1794 +- Removed the nullable type hint from the `PostAuthor` class's `name` property as it is now always set in https://github.com/hydephp/develop/pull/1794 ### Deprecated -- for soon-to-be removed features. +- The `PostAuthor::getName()` method is now deprecated and will be removed in v2. (use `$author->name` instead) in https://github.com/hydephp/develop/pull/1794 ### Removed - for now removed features.