Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to latest PHP 8.1 syntax #60

Merged
merged 1 commit into from
Nov 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/LazyEventListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,6 @@ public function getEvent()
*/
public function getPriority($default = 1)
{
return null !== $this->priority ? $this->priority : $default;
return $this->priority ?? $default;
}
}
21 changes: 9 additions & 12 deletions src/LazyListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,6 @@
*/
class LazyListener
{
/** @var ContainerInterface Container from which to pull listener. */
private $container;

/** @var array Variables/options to use during service creation, if any. */
private $env;

/** @var callable Marshaled listener callback. */
private $listener;

Expand All @@ -42,8 +36,13 @@ class LazyListener
/** @var string Service name of listener. */
private $service;

public function __construct(array $definition, ContainerInterface $container, array $env = [])
{
public function __construct(
array $definition,
/** @var ContainerInterface Container from which to pull listener. */
private readonly ContainerInterface $container,
/** @var array Variables/options to use during service creation, if any. */
private readonly array $env = []
) {
if (
! isset($definition['listener'])
|| ! is_string($definition['listener'])
Expand All @@ -64,10 +63,8 @@ public function __construct(array $definition, ContainerInterface $container, ar
);
}

$this->service = $definition['listener'];
$this->method = $definition['method'];
$this->container = $container;
$this->env = $env;
$this->service = $definition['listener'];
$this->method = $definition['method'];
}

/**
Expand Down
26 changes: 11 additions & 15 deletions src/LazyListenerAggregate.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,6 @@ class LazyListenerAggregate implements ListenerAggregateInterface
{
use ListenerAggregateTrait;

// phpcs:disable SlevomatCodingStandard.Classes.UnusedPrivateElements.WriteOnlyProperty

/** @var ContainerInterface Container from which to pull lazy listeners. */
private $container;

/** @var array Additional environment/option variables to use when creating listener. */
private $env;

// phpcs:enable

/**
* Generated LazyEventListener instances.
*
Expand All @@ -62,11 +52,17 @@ class LazyListenerAggregate implements ListenerAggregateInterface
* to pass to the LazyEventListener constructor.
* @throws Exception\InvalidArgumentException For invalid listener items.
*/
public function __construct(array $listeners, ContainerInterface $container, array $env = [])
{
$this->container = $container;
$this->env = $env;

public function __construct(
array $listeners,
/**
* @var ContainerInterface Container from which to pull lazy listeners
*/
private ContainerInterface $container,
/**
* @var array Additional environment/option variables to use when creating listener
*/
private array $env = []
) {
// This would raise an exception for invalid structs
foreach ($listeners as $listener) {
if (is_array($listener)) {
Expand Down
91 changes: 41 additions & 50 deletions test/EventManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ class EventManagerTest extends TestCase
use DeprecatedAssertions;

private EventManager $events;
private string|null $message;

private string|null $message = null;

protected function setUp(): void
{
Expand Down Expand Up @@ -181,7 +182,7 @@ public function testTriggerUntilShouldReturnAsSoonAsCallbackReturnsTrue(): void
return strstr($string, $search);
});
$responses = $this->events->triggerUntil(
[$this, 'evaluateStringCallback'],
$this->evaluateStringCallback(...),
'foo.bar',
$this,
['string' => 'foo', 'search' => 'f'],
Expand Down Expand Up @@ -216,15 +217,13 @@ public function evaluateStringCallback(mixed $value): bool
public function testTriggerUntilShouldMarkResponseCollectionStoppedWhenConditionMet(): void
{
// @codingStandardsIgnoreStart
$this->events->attach('foo.bar', function () { return 'bogus'; }, 4);
$this->events->attach('foo.bar', function () { return 'nada'; }, 3);
$this->events->attach('foo.bar', function () { return 'found'; }, 2);
$this->events->attach('foo.bar', function () { return 'zero'; }, 1);
$this->events->attach('foo.bar', fn() => 'bogus', 4);
$this->events->attach('foo.bar', fn() => 'nada', 3);
$this->events->attach('foo.bar', fn() => 'found', 2);
$this->events->attach('foo.bar', fn() => 'zero', 1);
// @codingStandardsIgnoreEnd

$responses = $this->events->triggerUntil(function ($result) {
return $result === 'found';
}, 'foo.bar', $this);
$responses = $this->events->triggerUntil(fn($result) => $result === 'found', 'foo.bar', $this);
self::assertInstanceOf(ResponseCollection::class, $responses);
self::assertTrue($responses->stopped());
$result = $responses->last();
Expand All @@ -236,15 +235,13 @@ public function testTriggerUntilShouldMarkResponseCollectionStoppedWhenCondition
public function testTriggerUntilShouldMarkResponseCollectionStoppedWhenConditionMetByLastListener(): void
{
// @codingStandardsIgnoreStart
$this->events->attach('foo.bar', function () { return 'bogus'; });
$this->events->attach('foo.bar', function () { return 'nada'; });
$this->events->attach('foo.bar', function () { return 'zero'; });
$this->events->attach('foo.bar', function () { return 'found'; });
$this->events->attach('foo.bar', fn() => 'bogus');
$this->events->attach('foo.bar', fn() => 'nada');
$this->events->attach('foo.bar', fn() => 'zero');
$this->events->attach('foo.bar', fn() => 'found');
// @codingStandardsIgnoreEnd

$responses = $this->events->triggerUntil(function ($result): bool {
return $result === 'found';
}, 'foo.bar', $this);
$responses = $this->events->triggerUntil(fn($result): bool => $result === 'found', 'foo.bar', $this);
self::assertInstanceOf(ResponseCollection::class, $responses);
self::assertTrue($responses->stopped());
self::assertEquals('found', $responses->last());
Expand All @@ -253,15 +250,18 @@ public function testTriggerUntilShouldMarkResponseCollectionStoppedWhenCondition
public function testResponseCollectionIsNotStoppedWhenNoCallbackMatchedByTriggerUntil(): void
{
// @codingStandardsIgnoreStart
$this->events->attach('foo.bar', static function () { return 'bogus'; }, 4);
$this->events->attach('foo.bar', static function () { return 'nada'; }, 3);
$this->events->attach('foo.bar', static function () { return 'found'; }, 2);
$this->events->attach('foo.bar', static function () { return 'zero'; }, 1);
$this->events->attach('foo.bar', static fn() => 'bogus', 4);
$this->events->attach('foo.bar', static fn() => 'nada', 3);
$this->events->attach('foo.bar', static fn() => 'found', 2);
$this->events->attach('foo.bar', static fn() => 'zero', 1);
// @codingStandardsIgnoreEnd

$responses = $this->events->triggerUntil(static function (mixed $result): bool {
return $result === 'never found';
}, 'foo.bar', $this);
$responses = $this->events->triggerUntil(
static fn(mixed $result): bool =>
$result === 'never found',
'foo.bar',
$this
);
self::assertInstanceOf(ResponseCollection::class, $responses);
self::assertFalse($responses->stopped());
self::assertEquals('zero', $responses->last());
Expand All @@ -270,10 +270,10 @@ public function testResponseCollectionIsNotStoppedWhenNoCallbackMatchedByTrigger
public function testCallingEventsStopPropagationMethodHaltsEventEmission(): void
{
// @codingStandardsIgnoreStart
$this->events->attach('foo.bar', static function (): string { return 'bogus'; }, 4);
$this->events->attach('foo.bar', static fn(): string => 'bogus', 4);
$this->events->attach('foo.bar', static function (EventInterface $e): string { $e->stopPropagation(true); return 'nada'; }, 3);
$this->events->attach('foo.bar', static function (): string { return 'found'; }, 2);
$this->events->attach('foo.bar', static function (): string { return 'zero'; }, 1);
$this->events->attach('foo.bar', static fn(): string => 'found', 2);
$this->events->attach('foo.bar', static fn(): string => 'zero', 1);
// @codingStandardsIgnoreEnd

$responses = $this->events->trigger('foo.bar');
Expand Down Expand Up @@ -343,9 +343,7 @@ public function testCanPassEventObjectAsSoleArgumentToTriggerEvent(): void
$event->setName(__FUNCTION__);
$event->setTarget($this);
$event->setParams(['foo' => 'bar']);
$this->events->attach(__FUNCTION__, static function (EventInterface $e): EventInterface {
return $e;
});
$this->events->attach(__FUNCTION__, static fn(EventInterface $e): EventInterface => $e);
$responses = $this->events->triggerEvent($event);
self::assertSame($event, $responses->last());
}
Expand All @@ -356,12 +354,15 @@ public function testCanPassEventObjectAndCallbackToTriggerEventUntil(): void
$event->setName(__FUNCTION__);
$event->setTarget($this);
$event->setParams(['foo' => 'bar']);
$this->events->attach(__FUNCTION__, static function (EventInterface $e): EventInterface {
return $e;
});
$responses = $this->events->triggerEventUntil(static function (mixed $r): bool {
return $r instanceof EventInterface;
}, $event);
$this->events->attach(
__FUNCTION__,
static fn(EventInterface $e): EventInterface => $e
);
$responses = $this->events->triggerEventUntil(
static fn(mixed $r): bool =>
$r instanceof EventInterface,
$event
);
self::assertTrue($responses->stopped());
self::assertSame($event, $responses->last());
}
Expand Down Expand Up @@ -439,9 +440,7 @@ public function testTriggerEventUntilSetsStopPropagationFlagToFalse(): void
$marker->propagationIsStopped = $e->propagationIsStopped();
});

$criteria = static function (): bool {
return false;
};
$criteria = static fn(): bool => false;
$event = new Event();
$event->setName('foo');
$event->stopPropagation(true);
Expand Down Expand Up @@ -624,12 +623,8 @@ public function testDetachDoesNothingIfEventIsNotPresentInManager(): void
public function testCanDetachWildcardListeners(): array
{
$events = ['foo', 'bar'];
$listener = static function (): string {
return 'non-wildcard';
};
$wildcardListener = static function (): string {
return 'wildcard';
};
$listener = static fn(): string => 'non-wildcard';
$wildcardListener = static fn(): string => 'wildcard';

array_walk($events, function (string $event) use ($listener): void {
$this->events->attach($event, $listener);
Expand Down Expand Up @@ -679,9 +674,7 @@ public function testNotPassingEventNameToDetachDetachesListenerFromAllEvents():
{
$eventNames = ['foo', 'bar'];
$events = $this->events;
$listener = static function (): string {
return 'listener';
};
$listener = static fn(): string => 'listener';

foreach ($eventNames as $event) {
$events->attach($event, $listener);
Expand Down Expand Up @@ -860,9 +853,7 @@ public function testTriggerEventUntilAcceptsEventInstanceAndTriggersListenersUnt
->method('propagationIsStopped')
->willReturn(false);

$callback = static function (mixed $result): bool {
return $result === true;
};
$callback = static fn(mixed $result): bool => $result === true;

$triggeredOne = false;
$this->events->attach('test', static function (EventInterface $e) use ($event, &$triggeredOne): void {
Expand Down
2 changes: 1 addition & 1 deletion test/FilterChainTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class FilterChainTest extends TestCase
{
private FilterChain $filterchain;

private string|null $message;
private string|null $message = null;

protected function setUp(): void
{
Expand Down