Skip to content

Commit

Permalink
moved deprecated methods to trait
Browse files Browse the repository at this point in the history
  • Loading branch information
overclokk committed Sep 23, 2023
1 parent a5ad7b2 commit 344a2be
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 129 deletions.
126 changes: 1 addition & 125 deletions src/GlobalDispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,118 +4,15 @@

namespace ItalyStrap\Event;

use function add_filter;
use function apply_filters;
use function current_filter;
use function do_action;
use function has_filter;
use function remove_all_filters;
use function remove_filter;

/**
* @psalm-api
*/
class GlobalDispatcher implements EventDispatcherInterface, ListenerRegisterInterface
{
private const M_DEPRECATION_PATTERN = 'This method %1$s::%2$s() is deprecated, use %3$s() instead.';

public function addListener(
string $eventName,
callable $listener,
int $priority = 10,
int $accepted_args = 3
): bool {

$this->deprecated(
self::M_DEPRECATION_PATTERN,
__FUNCTION__,
'\ItalyStrap\Event\GlobalOrderedListenerProvider::' . __FUNCTION__
);

return add_filter($eventName, $listener, $priority, $accepted_args);
}

public function removeListener(
string $eventName,
callable $listener,
int $priority = 10
): bool {

$this->deprecated(
self::M_DEPRECATION_PATTERN,
__FUNCTION__,
'\ItalyStrap\Event\GlobalOrderedListenerProvider::' . __FUNCTION__
);

return remove_filter($eventName, $listener, $priority);
}

public function removeAllListener(string $eventName, $priority = false): bool
{

$this->deprecated(
self::M_DEPRECATION_PATTERN,
__FUNCTION__,
'\ItalyStrap\Event\GlobalOrderedListenerProvider::' . __FUNCTION__
);

return remove_all_filters($eventName, $priority);
}

public function hasListener(string $eventName, $callback = false)
{

$this->deprecated(
self::M_DEPRECATION_PATTERN,
__FUNCTION__,
'\ItalyStrap\Event\GlobalOrderedListenerProvider::' . __FUNCTION__
);

return has_filter($eventName, $callback);
}

/**
* @param string $event_name
* @param mixed ...$args
* @return object
* @infection-ignore-all
* @psalm-suppress PossiblyUnusedParam
*/
public function dispatch($event_name, ...$args): object
{

$this->deprecated(
self::M_DEPRECATION_PATTERN,
__FUNCTION__,
'\Psr\EventDispatcher\EventDispatcherInterface::' . __FUNCTION__
);

$this->trigger($event_name, ...$args);

if (isset($args[0]) && \is_object($args[0])) {
return $args[0];
}

return new class {
};
}

/**
* @deprecated
* @infection-ignore-all
* @param mixed ...$args
* @psalm-suppress PossiblyUnusedParam
*/
public function execute(string $event_name, ...$args): void
{
$pattern = <<<'D_MESSAGE'
This method %1$s::%2$s() is deprecated, use %1$s::%3$s() instead.
D_MESSAGE;

$this->deprecated($pattern, __FUNCTION__, 'trigger');

$this->trigger($event_name, ...$args);
}
use LegacyEventDispatcherMethodsDeprecatedTrait;

public function trigger(string $event_name, ...$args): void
{
Expand All @@ -126,25 +23,4 @@ public function filter(string $event_name, $value, ...$args)
{
return apply_filters($event_name, $value, ...$args);
}

public function currentEventName(): string
{
return current_filter();
}

/**
* @psalm-suppress UnusedMethod
*/
private function deprecated(string $pattern, string $oldMethodName, string $newMethodName): void
{
\trigger_error(
\sprintf(
$pattern,
self::class,
$oldMethodName,
$newMethodName
),
\E_USER_DEPRECATED
);
}
}
6 changes: 3 additions & 3 deletions src/GlobalOrderedListenerProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@ final class GlobalOrderedListenerProvider implements ListenerProviderInterface,
public function addListener(
string $eventName,
callable $listener,
int $priority = 10,
int $accepted_args = 3
int $priority = ListenerRegisterInterface::PRIORITY,
int $accepted_args = ListenerRegisterInterface::ACCEPTED_ARGS
): bool {
return \add_filter($eventName, $listener, $priority, $accepted_args);
}

public function removeListener(
string $eventName,
callable $listener,
int $priority = 10
int $priority = ListenerRegisterInterface::PRIORITY
): bool {
return \remove_filter($eventName, $listener, $priority);
}
Expand Down
142 changes: 142 additions & 0 deletions src/LegacyEventDispatcherMethodsDeprecatedTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
<?php

declare(strict_types=1);

namespace ItalyStrap\Event;

use function add_filter;
use function current_filter;
use function has_filter;
use function remove_all_filters;
use function remove_filter;

trait LegacyEventDispatcherMethodsDeprecatedTrait
{
private string $deprecationPattern = 'This method %1$s::%2$s() is deprecated, use %3$s() instead.';

public function addListener(
string $eventName,
callable $listener,
int $priority = 10,
int $accepted_args = 3
): bool {

$this->deprecated(
$this->deprecationPattern,
__FUNCTION__,
'\ItalyStrap\Event\GlobalOrderedListenerProvider::' . __FUNCTION__
);

return add_filter($eventName, $listener, $priority, $accepted_args);
}

public function removeListener(
string $eventName,
callable $listener,
int $priority = 10
): bool {

$this->deprecated(
$this->deprecationPattern,
__FUNCTION__,
'\ItalyStrap\Event\GlobalOrderedListenerProvider::' . __FUNCTION__
);

return remove_filter($eventName, $listener, $priority);
}

public function removeAllListener(string $eventName, $priority = false): bool
{

$this->deprecated(
$this->deprecationPattern,
__FUNCTION__,
'\ItalyStrap\Event\GlobalOrderedListenerProvider::' . __FUNCTION__
);

return remove_all_filters($eventName, $priority);
}

public function hasListener(string $eventName, $callback = false)
{

$this->deprecated(
$this->deprecationPattern,
__FUNCTION__,
'\ItalyStrap\Event\GlobalOrderedListenerProvider::' . __FUNCTION__
);

return has_filter($eventName, $callback);
}

/**
* @param string $event_name
* @param mixed ...$args
* @return object
* @infection-ignore-all
* @psalm-suppress PossiblyUnusedParam
*/
public function dispatch($event_name, ...$args): object
{

$this->deprecated(
$this->deprecationPattern,
__FUNCTION__,
'\Psr\EventDispatcher\EventDispatcherInterface::' . __FUNCTION__
);

$this->trigger($event_name, ...$args);

if (isset($args[0]) && \is_object($args[0])) {
return $args[0];
}

return new class {
};
}

/**
* @deprecated
* @infection-ignore-all
* @param mixed ...$args
* @psalm-suppress PossiblyUnusedParam
*/
public function execute(string $event_name, ...$args): void
{
$pattern = <<<'D_MESSAGE'
This method %1$s::%2$s() is deprecated, use %1$s::%3$s() instead.
D_MESSAGE;

$this->deprecated($pattern, __FUNCTION__, 'trigger');

$this->trigger($event_name, ...$args);
}

public function currentEventName(): string
{

$this->deprecated(
$this->deprecationPattern,
__FUNCTION__,
'\ItalyStrap\Event\GlobalState::' . __FUNCTION__
);

return current_filter();
}

/**
* @psalm-suppress UnusedMethod
*/
private function deprecated(string $pattern, string $oldMethodName, string $newMethodName): void
{
\trigger_error(
\sprintf(
$pattern,
self::class,
$oldMethodName,
$newMethodName
),
\E_USER_DEPRECATED
);
}
}
3 changes: 3 additions & 0 deletions src/ListenerRegisterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
*/
interface ListenerRegisterInterface
{
public const PRIORITY = 10;
public const ACCEPTED_ARGS = 5;

/**
* Adds the given event listener to the list of event listeners
* that listen to the given event.
Expand Down
2 changes: 1 addition & 1 deletion src/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function __invoke(): array
],
'sharing' => [
// Global
EventDispatcher::class,
GlobalDispatcher::class,
SubscriberRegister::class,
// PSR-14
Dispatcher::class,
Expand Down

0 comments on commit 344a2be

Please sign in to comment.