Browse Source

Remove deprecated assocation of multiple methods with an event

Corey Taylor 3 years ago
parent
commit
022d6ea270

+ 9 - 42
src/Event/EventManager.php

@@ -139,10 +139,9 @@ class EventManager implements EventManagerInterface
      */
     protected function _attachSubscriber(EventListenerInterface $subscriber): void
     {
-        foreach ($subscriber->implementedEvents() as $eventKey => $handlers) {
-            foreach ($this->normalizeHandlers($subscriber, $handlers) as $handler) {
-                $this->on($eventKey, $handler['settings'], $handler['callable']);
-            }
+        foreach ($subscriber->implementedEvents() as $eventKey => $handler) {
+            $handler = $this->normalizeHandler($subscriber, $handler);
+            $this->on($eventKey, $handler['settings'], $handler['callable']);
         }
     }
 
@@ -212,50 +211,18 @@ class EventManager implements EventManagerInterface
         if (!empty($eventKey)) {
             $events = [$eventKey => $events[$eventKey]];
         }
-        foreach ($events as $key => $handlers) {
-            foreach ($this->normalizeHandlers($subscriber, $handlers) as $handler) {
-                $this->off($key, $handler['callable']);
-            }
+        foreach ($events as $key => $handler) {
+            $handler = $this->normalizeHandler($subscriber, $handler);
+            $this->off($key, $handler['callable']);
         }
     }
 
     /**
-     * Builds an array of normalized handlers.
-     *
-     * A normalized handler is an aray with these keys:
-     *
-     *  - `callable` - The event handler callable
-     *  - `settings` - The event handler settings
-     *
-     * @param \Cake\Event\EventListenerInterface $subscriber Event subscriber
-     * @param array|string $handlers Event handlers
-     * @return array
-     */
-    protected function normalizeHandlers(EventListenerInterface $subscriber, array|string $handlers): array
-    {
-        // Check if an array of handlers not single handler config array
-        if (is_array($handlers) && !isset($handlers['callable'])) {
-            deprecationWarning(
-                '4.4.0',
-                'Registering multiple methods with an event is deprecated. ' .
-                'Assign a single method and call others from it.'
-            );
-            foreach ($handlers as &$handler) {
-                $handler = $this->normalizeHandler($subscriber, $handler);
-            }
-
-            return $handlers;
-        }
-
-        return [$this->normalizeHandler($subscriber, $handlers)];
-    }
-
-    /**
      * Builds a single normalized handler.
      *
      * A normalized handler is an aray with these keys:
      *
-     *  - `callable` - The event handler callable
+     *  - `callable` - The event handler closure
      *  - `settings` - The event handler settings
      *
      * @param \Cake\Event\EventListenerInterface $subscriber Event subscriber
@@ -265,13 +232,13 @@ class EventManager implements EventManagerInterface
     protected function normalizeHandler(EventListenerInterface $subscriber, array|string $handler): array
     {
         if (is_string($handler)) {
-            return ['callable' => [$subscriber, $handler], 'settings' => []];
+            return ['callable' => $subscriber->$handler(...), 'settings' => []];
         }
 
         $method = $handler['callable'];
         unset($handler['callable']);
 
-        return ['callable' => [$subscriber, $method], 'settings' => $handler];
+        return ['callable' => $subscriber->$method(...), 'settings' => $handler];
     }
 
     /**

+ 0 - 40
tests/TestCase/Event/EventManagerTest.php

@@ -22,11 +22,9 @@ use Cake\Event\EventList;
 use Cake\Event\EventListenerInterface;
 use Cake\Event\EventManager;
 use Cake\TestSuite\TestCase;
-use Closure;
 use InvalidArgumentException;
 use TestApp\TestCase\Event\CustomTestEventListenerInterface;
 use TestApp\TestCase\Event\EventTestListener;
-use TestApp\TestCase\Event\MultiMethodEventListenerInterface;
 
 /**
  * Tests the Cake\Event\EventManager class functionality
@@ -347,28 +345,6 @@ class EventManagerTest extends TestCase
     }
 
     /**
-     * Test implementedEvents binding multiple callbacks to the same event name.
-     *
-     * @triggers multiple.handlers
-     */
-    public function testOnSubscriberMultiple(): void
-    {
-        $manager = new EventManager();
-        $listener = $this->getMockBuilder(MultiMethodEventListenerInterface::class)
-            ->onlyMethods(['listenerFunction', 'secondListenerFunction'])
-            ->getMock();
-        $this->deprecated(fn () => $manager->on($listener));
-        $event = new Event('multiple.handlers');
-        $listener->expects($this->once())
-            ->method('listenerFunction')
-            ->with($event);
-        $listener->expects($this->once())
-            ->method('secondListenerFunction')
-            ->with($event);
-        $manager->dispatch($event);
-    }
-
-    /**
      * Tests subscribing a listener object and firing the events it subscribed to
      */
     public function testDetachSubscriber(): void
@@ -391,22 +367,6 @@ class EventManagerTest extends TestCase
         $this->assertEquals([], $manager->listeners('another.event'));
     }
 
-    public function testDetachSubscriberMultiple(): void
-    {
-        $manager = new EventManager();
-        $listener = $this->getMockBuilder(MultiMethodEventListenerInterface::class)
-            ->onlyMethods(['listenerFunction', 'secondListenerFunction'])
-            ->getMock();
-        $this->deprecated(fn () => $manager->on($listener));
-        $expected = [
-            ['callable' => Closure::fromCallable([$listener, 'listenerFunction'])],
-            ['callable' => Closure::fromCallable([$listener, 'secondListenerFunction'])],
-        ];
-        $this->assertEquals($expected, $manager->listeners('multiple.handlers'));
-        $this->deprecated(fn () => $manager->off($listener));
-        $this->assertEquals([], $manager->listeners('multiple.handlers'));
-    }
-
     /**
      * Tests that it is possible to get/set the manager singleton
      */

+ 0 - 8
tests/test_app/TestApp/TestCase/Event/CustomTestEventListenerInterface.php

@@ -20,12 +20,4 @@ class CustomTestEventListenerInterface extends EventTestListener implements Even
             'another.event' => ['callable' => 'secondListenerFunction'],
         ];
     }
-
-    /**
-     * Test function to be used in event dispatching
-     */
-    public function thirdListenerFunction(): void
-    {
-        $this->callList[] = __FUNCTION__;
-    }
 }

+ 8 - 0
tests/test_app/TestApp/TestCase/Event/EventTestListener.php

@@ -33,6 +33,14 @@ class EventTestListener
     }
 
     /**
+     * Test function to be used in event dispatching
+     */
+    public function thirdListenerFunction(): void
+    {
+        $this->callList[] = __FUNCTION__;
+    }
+
+    /**
      * Auxiliary function to help in stopPropagation testing
      *
      * @return bool|void

+ 0 - 25
tests/test_app/TestApp/TestCase/Event/MultiMethodEventListenerInterface.php

@@ -1,25 +0,0 @@
-<?php
-declare(strict_types=1);
-
-namespace TestApp\TestCase\Event;
-
-use Cake\Event\EventListenerInterface;
-
-/**
- * Mock used for testing the subscriber objects
- */
-class MultiMethodEventListenerInterface extends EventTestListener implements EventListenerInterface
-{
-    /**
-     * @return array<string, mixed>
-     */
-    public function implementedEvents(): array
-    {
-        return [
-            'multiple.handlers' => [
-                ['callable' => 'listenerFunction'],
-                ['callable' => 'secondListenerFunction'],
-            ],
-        ];
-    }
-}