From 80676591a5cc34afdda2df8da4d08bb39baee290 Mon Sep 17 00:00:00 2001 From: Krzysztof Grabania Date: Wed, 10 Jul 2024 13:49:04 +0200 Subject: [PATCH 1/7] fix: add notification object serialization --- phpstan-baseline.neon | 40 ++++++++++++++++++++++++++++++++++ src/Core/Notification.php | 46 ++++++++++++++++++++++++++++++++++++++- 2 files changed, 85 insertions(+), 1 deletion(-) diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 303f0179..96d2a0c7 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -1080,11 +1080,51 @@ parameters: count: 3 path: src/Core/License.php + - + message: "#^Parameter \\#1 \\$array of function array_keys expects array, mixed given\\.$#" + count: 1 + path: src/Core/Notification.php + + - + message: "#^Parameter \\#1 \\$array of function array_values expects array, mixed given\\.$#" + count: 1 + path: src/Core/Notification.php + + - + message: "#^Parameter \\#1 \\$data of method BracketSpace\\\\Notification\\\\Interfaces\\\\Sendable\\:\\:setData\\(\\) expects array\\, mixed given\\.$#" + count: 1 + path: src/Core/Notification.php + + - + message: "#^Property BracketSpace\\\\Notification\\\\Core\\\\Notification\\:\\:\\$enabled \\(bool\\) does not accept mixed\\.$#" + count: 1 + path: src/Core/Notification.php + + - + message: "#^Property BracketSpace\\\\Notification\\\\Core\\\\Notification\\:\\:\\$extras \\(array\\\\) does not accept mixed\\.$#" + count: 1 + path: src/Core/Notification.php + + - + message: "#^Property BracketSpace\\\\Notification\\\\Core\\\\Notification\\:\\:\\$hash \\(string\\) does not accept mixed\\.$#" + count: 1 + path: src/Core/Notification.php + - message: "#^Property BracketSpace\\\\Notification\\\\Core\\\\Notification\\:\\:\\$sourcePostId is never read, only written\\.$#" count: 1 path: src/Core/Notification.php + - + message: "#^Property BracketSpace\\\\Notification\\\\Core\\\\Notification\\:\\:\\$title \\(string\\) does not accept mixed\\.$#" + count: 1 + path: src/Core/Notification.php + + - + message: "#^Property BracketSpace\\\\Notification\\\\Core\\\\Notification\\:\\:\\$version \\(int\\) does not accept mixed\\.$#" + count: 1 + path: src/Core/Notification.php + - message: "#^Method BracketSpace\\\\Notification\\\\Core\\\\Resolver\\:\\:clear\\(\\) should return string but returns string\\|null\\.$#" count: 1 diff --git a/src/Core/Notification.php b/src/Core/Notification.php index e3dab9bf..ce15bdb5 100644 --- a/src/Core/Notification.php +++ b/src/Core/Notification.php @@ -36,7 +36,7 @@ class Notification * * @var Interfaces\Triggerable|null */ - private $trigger; + private $trigger = null; /** * Carriers @@ -639,4 +639,48 @@ public function to(string $type, array $config = []) { return apply_filters(sprintf('notification/to/%s', $type), $this, $config); } + + /** + * Serialized Notification instance. + * + * @return array + */ + public function __serialize(): array + { + /** @var array */ + return $this->to('array'); + } + + /** + * Unserializes Notification instance. + * + * @param array $data + * @return void + */ + public function __unserialize(array $data): void + { + $this->hash = $data['hash']; + $this->title = $data['title']; + $this->enabled = $data['enabled']; + $this->extras = $data['extras']; + $this->version = $data['version']; + $this->trigger = is_string($data['trigger']) ? Store\Trigger::get($data['trigger']) : null; + $this->carriers = array_filter( + array_map( + function($carrierSlug, $carrierData) { + $carrier = Store\Carrier::get($carrierSlug); + + if (!$carrier instanceof Interfaces\Sendable) { + return null; + } + + $carrier->setData($carrierData); + + return $carrier; + }, + array_keys($data['carriers']), + array_values($data['carriers']) + ) + ); + } } From 6be0f2f518435fdd75d6ffa22bf78a49f0bde318 Mon Sep 17 00:00:00 2001 From: Krzysztof Grabania Date: Thu, 11 Jul 2024 12:05:47 +0200 Subject: [PATCH 2/7] chore: make serialized notification types more specific --- phpstan.neon | 11 +++++++++++ src/Core/Notification.php | 6 +++--- src/Repository/Converter/ArrayConverter.php | 2 +- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/phpstan.neon b/phpstan.neon index 36493271..01c1bbe0 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -25,6 +25,17 @@ parameters: version?: int, } ''' + NotificationAsArray: ''' + array{ + hash: string, + title: string, + trigger: string, + carriers: array>, + enabled: bool, + extras: array|bool|float|int|string>, + version: int, + } + ''' NotificationUnconvertedData: ''' array{ hash?: string, diff --git a/src/Core/Notification.php b/src/Core/Notification.php index ce15bdb5..d3b308e6 100644 --- a/src/Core/Notification.php +++ b/src/Core/Notification.php @@ -643,18 +643,18 @@ public function to(string $type, array $config = []) /** * Serialized Notification instance. * - * @return array + * @return NotificationAsArray */ public function __serialize(): array { - /** @var array */ + /** @var NotificationAsArray */ return $this->to('array'); } /** * Unserializes Notification instance. * - * @param array $data + * @param NotificationAsArray $data * @return void */ public function __unserialize(array $data): void diff --git a/src/Repository/Converter/ArrayConverter.php b/src/Repository/Converter/ArrayConverter.php index bde7e6f6..04a12c73 100644 --- a/src/Repository/Converter/ArrayConverter.php +++ b/src/Repository/Converter/ArrayConverter.php @@ -71,7 +71,7 @@ public function from($data): Notification * @since [Next] * @param Notification $notification Notification instance * @param array $config The additional configuration of the converter - * @return mixed + * @return NotificationAsArray */ public function to(Notification $notification, array $config = []) { From 8b29c7f5c6437bbb6749f897c9b92f2d1557d9fc Mon Sep 17 00:00:00 2001 From: Krzysztof Grabania Date: Thu, 11 Jul 2024 12:05:57 +0200 Subject: [PATCH 3/7] fix: remove ignored errors --- phpstan-baseline.neon | 40 ---------------------------------------- 1 file changed, 40 deletions(-) diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 96d2a0c7..303f0179 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -1080,51 +1080,11 @@ parameters: count: 3 path: src/Core/License.php - - - message: "#^Parameter \\#1 \\$array of function array_keys expects array, mixed given\\.$#" - count: 1 - path: src/Core/Notification.php - - - - message: "#^Parameter \\#1 \\$array of function array_values expects array, mixed given\\.$#" - count: 1 - path: src/Core/Notification.php - - - - message: "#^Parameter \\#1 \\$data of method BracketSpace\\\\Notification\\\\Interfaces\\\\Sendable\\:\\:setData\\(\\) expects array\\, mixed given\\.$#" - count: 1 - path: src/Core/Notification.php - - - - message: "#^Property BracketSpace\\\\Notification\\\\Core\\\\Notification\\:\\:\\$enabled \\(bool\\) does not accept mixed\\.$#" - count: 1 - path: src/Core/Notification.php - - - - message: "#^Property BracketSpace\\\\Notification\\\\Core\\\\Notification\\:\\:\\$extras \\(array\\\\) does not accept mixed\\.$#" - count: 1 - path: src/Core/Notification.php - - - - message: "#^Property BracketSpace\\\\Notification\\\\Core\\\\Notification\\:\\:\\$hash \\(string\\) does not accept mixed\\.$#" - count: 1 - path: src/Core/Notification.php - - message: "#^Property BracketSpace\\\\Notification\\\\Core\\\\Notification\\:\\:\\$sourcePostId is never read, only written\\.$#" count: 1 path: src/Core/Notification.php - - - message: "#^Property BracketSpace\\\\Notification\\\\Core\\\\Notification\\:\\:\\$title \\(string\\) does not accept mixed\\.$#" - count: 1 - path: src/Core/Notification.php - - - - message: "#^Property BracketSpace\\\\Notification\\\\Core\\\\Notification\\:\\:\\$version \\(int\\) does not accept mixed\\.$#" - count: 1 - path: src/Core/Notification.php - - message: "#^Method BracketSpace\\\\Notification\\\\Core\\\\Resolver\\:\\:clear\\(\\) should return string but returns string\\|null\\.$#" count: 1 From eb17b00c2508757c8b4a2d6dfcf1e36e63ad485c Mon Sep 17 00:00:00 2001 From: Krzysztof Grabania Date: Thu, 11 Jul 2024 12:06:10 +0200 Subject: [PATCH 4/7] chore: add changelog --- readme.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/readme.txt b/readme.txt index dc9f9490..83f69d87 100644 --- a/readme.txt +++ b/readme.txt @@ -381,6 +381,7 @@ Removed deprecated hooks: * [Fixed] Stripping shortcodes in carrier fields. * [Fixed] Email carrier header "From" prioritized over header in settings. * [Fixed] User password reset link requires encoded username. +* [Fixed] Notification class serialization * [Removed] DOING_NOTIFICATION_SAVE constant. * [Removed] NotificationQueries class in favor of NotificationDatabaseService. * [Removed] Webook and Webhook JSON Carriers. From 5477924e09c118e9e91e99802a683610f964087f Mon Sep 17 00:00:00 2001 From: Krzysztof Grabania Date: Thu, 11 Jul 2024 12:10:59 +0200 Subject: [PATCH 5/7] chore: add missing dot in changelog --- readme.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/readme.txt b/readme.txt index 83f69d87..8f376792 100644 --- a/readme.txt +++ b/readme.txt @@ -381,7 +381,7 @@ Removed deprecated hooks: * [Fixed] Stripping shortcodes in carrier fields. * [Fixed] Email carrier header "From" prioritized over header in settings. * [Fixed] User password reset link requires encoded username. -* [Fixed] Notification class serialization +* [Fixed] Notification class serialization. * [Removed] DOING_NOTIFICATION_SAVE constant. * [Removed] NotificationQueries class in favor of NotificationDatabaseService. * [Removed] Webook and Webhook JSON Carriers. From 4a01640267b499cbeb9b6f53be1fab81766e3910 Mon Sep 17 00:00:00 2001 From: Krzysztof Grabania Date: Thu, 11 Jul 2024 12:11:54 +0200 Subject: [PATCH 6/7] chore: fix docblock indentation --- src/Core/Notification.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Core/Notification.php b/src/Core/Notification.php index d3b308e6..65c4717d 100644 --- a/src/Core/Notification.php +++ b/src/Core/Notification.php @@ -643,7 +643,7 @@ public function to(string $type, array $config = []) /** * Serialized Notification instance. * - * @return NotificationAsArray + * @return NotificationAsArray */ public function __serialize(): array { @@ -654,8 +654,8 @@ public function __serialize(): array /** * Unserializes Notification instance. * - * @param NotificationAsArray $data - * @return void + * @param NotificationAsArray $data Notification data as array + * @return void */ public function __unserialize(array $data): void { From 62b7ab300cc3cc4824f7bc6385cbe895143ac852 Mon Sep 17 00:00:00 2001 From: Krzysztof Grabania Date: Thu, 11 Jul 2024 12:12:22 +0200 Subject: [PATCH 7/7] chore: add missing dot --- src/Core/Notification.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Core/Notification.php b/src/Core/Notification.php index 65c4717d..a3a5dca9 100644 --- a/src/Core/Notification.php +++ b/src/Core/Notification.php @@ -654,7 +654,7 @@ public function __serialize(): array /** * Unserializes Notification instance. * - * @param NotificationAsArray $data Notification data as array + * @param NotificationAsArray $data Notification data as array. * @return void */ public function __unserialize(array $data): void