]> git.mxchange.org Git - friendica.git/commitdiff
remove deprecation of BaseCollection::map(), check return type of BaseCollection...
authorArt4 <art4@wlabs.de>
Wed, 12 Mar 2025 15:50:24 +0000 (15:50 +0000)
committerArt4 <art4@wlabs.de>
Wed, 12 Mar 2025 15:50:24 +0000 (15:50 +0000)
src/BaseCollection.php
src/Navigation/Notifications/Collection/Notifications.php
src/Navigation/Notifications/Collection/Notifies.php
src/Profile/ProfileField/Collection/ProfileFields.php

index 36c6b9a63e4c850286ef42d322820815e0555237..b30dbfc6b0fda4b25fc888f9c83863d85c9f4d6c 100644 (file)
@@ -83,16 +83,12 @@ class BaseCollection extends \ArrayIterator
        /**
         * Apply a callback function on all elements in the collection and returns a new collection with the updated elements
         *
-        * @deprecated 2025.05 Use `array_map()` instead
-        *
         * @param callable $callback
         * @return BaseCollection
         * @see array_map()
         */
        public function map(callable $callback): BaseCollection
        {
-               @trigger_error('`' . __METHOD__ . '()` is deprecated since 2025.05 and will be removed after 5 months, use `array_map()` instead.', E_USER_DEPRECATED);
-
                $class = get_class($this);
 
                return new $class(array_map($callback, $this->getArrayCopy()), $this->getTotalCount());
index 4c32cf598bafa2f59191e8fb72fd346949705686..e422283891e706e8e4070911277cc1a7a1a40f16 100644 (file)
@@ -19,33 +19,38 @@ class Notifications extends BaseCollection
 
        public function setSeen(): Notifications
        {
-               $class = get_class($this);
-
-               return new $class(
-                       array_map(
-                               function (NotificationEntity $notification) {
-                                       $notification->setSeen();
-                               },
-                               $this->getArrayCopy()
-                       ),
-                       $this->getTotalCount(),
-               );
+               $notifications = $this->map(function (NotificationEntity $notification) {
+                       $notification->setSeen();
+               });
+
+               if (!$notifications instanceof Notifications) {
+                       // Show the possible error explicitly
+                       throw new \Exception(sprintf(
+                               'BaseCollection::map() should return instance of %s, but returns %s instead.',
+                               Notifications::class,
+                               get_class($notifications),
+                       ));
+               }
+
+               return $notifications;
        }
 
        public function setDismissed(): Notifications
        {
-               $class = get_class($this);
-
-               return new $class(
-                       array_map(
-                               function (NotificationEntity $notification) {
-                                       $notification->setDismissed();
-                               },
-                               $this->getArrayCopy(),
-                       ),
-                       $this->getTotalCount(),
-               );
-
+               $notifications = $this->map(function (NotificationEntity $notification) {
+                       $notification->setDismissed();
+               });
+
+               if (!$notifications instanceof Notifications) {
+                       // Show the possible error explicitly
+                       throw new \Exception(sprintf(
+                               'BaseCollection::map() should return instance of %s, but returns %s instead.',
+                               Notifications::class,
+                               get_class($notifications),
+                       ));
+               }
+
+               return $notifications;
        }
 
        public function countUnseen(): int
index fc97880de3a01097643f2cf63d88498f9a759c8c..9c2f5dcc7437a366a7bc8b72dcc2e13f511957fe 100644 (file)
@@ -19,13 +19,19 @@ class Notifies extends BaseCollection
 
        public function setSeen(): Notifies
        {
-               $class = get_class($this);
+               $notifies = $this->map(function (NotifyEntity $notify) {
+                       $notify->setSeen();
+               });
 
-               return new $class(array_map(
-                       function (NotifyEntity $notify) {
-                               $notify->setSeen();
-                       },
-                       $this->getArrayCopy()), $this->getTotalCount(),
-               );
+               if (!$notifies instanceof Notifies) {
+                       // Show the possible error explicitly
+                       throw new \Exception(sprintf(
+                               'BaseCollection::map() should return instance of %s, but returns %s instead.',
+                               Notifies::class,
+                               get_class($notifies),
+                       ));
+               }
+
+               return $notifies;
        }
 }
index dcf5498df3f2f27f6d63bebbfcf747d16488aa98..44f13673d7682ffcff79a5c4181793ba4c9a047a 100644 (file)
@@ -19,15 +19,22 @@ class ProfileFields extends BaseCollection
 
        public function map(callable $callback): ProfileFields
        {
-               $class = get_class($this);
-
-               return new $class(array_map($callback, $this->getArrayCopy()), $this->getTotalCount());
+               $profileFields = parent::map($callback);
+
+               if (!$profileFields instanceof ProfileFields) {
+                       // Show the possible error explicitly
+                       throw new \Exception(sprintf(
+                               'BaseCollection::map() should return instance of %s, but returns %s instead.',
+                               ProfileFields::class,
+                               get_class($profileFields),
+                       ));
+               }
+
+               return $profileFields;
        }
 
        public function filter(?callable $callback = null, int $flag = 0): ProfileFields
        {
-               $class = get_class($this);
-
-               return new $class(array_filter($this->getArrayCopy(), $callback, $flag));
+               return new self(array_filter($this->getArrayCopy(), $callback, $flag));
        }
 }