Skip to content

Commit

Permalink
minor #48793 Leverage arrow function syntax for closure (tigitz)
Browse files Browse the repository at this point in the history
This PR was merged into the 6.3 branch.

Discussion
----------

Leverage arrow function syntax for closure

| Q             | A
| ------------- | ---
| Branch?       | 6.3
| Bug fix?      | no
| New feature?  | no <!-- please update src/**/CHANGELOG.md files -->
| Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Tickets       | Fix #47658 <!-- prefix each issue number with "Fix #", no need to create an issue if none exists, explain below instead -->
| License       | MIT
| Doc PR        | <!-- required for new features -->

Rationale in the RFC [here](https://wiki.php.net/rfc/arrow_functions_v2#introduction)

It's also notable that using arrow function syntax rather than the classic one has been enforced in the past by symfony core member: symfony/symfony#48069 (comment)

So this PR would be consistent.

Commits
-------

f5802d3a2a Leverage arrow function syntax for closure
  • Loading branch information
nicolas-grekas committed Jan 13, 2023
2 parents 7ecc52e + 8957b2b commit 514f5a1
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 6 deletions.
2 changes: 1 addition & 1 deletion DependencyInjection/RegisterListenersPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public function process(ContainerBuilder $container)
$event['method'] = 'on'.preg_replace_callback([
'/(?<=\b|_)[a-z]/i',
'/[^a-z0-9]/i',
], function ($matches) { return strtoupper($matches[0]); }, $event['event']);
], fn ($matches) => strtoupper($matches[0]), $event['event']);
$event['method'] = preg_replace('/[^a-z0-9]/i', '', $event['method']);

if (null !== ($class = $container->getDefinition($id)->getClass()) && ($r = $container->getReflectionClass($class, false)) && !$r->hasMethod($event['method']) && $r->hasMethod('__invoke')) {
Expand Down
6 changes: 3 additions & 3 deletions Tests/EventDispatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ public function testDispatchLazyListener()
public function testRemoveFindsLazyListeners()
{
$test = new TestWithDispatcher();
$factory = function () use ($test) { return $test; };
$factory = fn () => $test;

$this->dispatcher->addListener('foo', [$factory, 'foo']);
$this->assertTrue($this->dispatcher->hasListeners('foo'));
Expand All @@ -363,7 +363,7 @@ public function testRemoveFindsLazyListeners()
public function testPriorityFindsLazyListeners()
{
$test = new TestWithDispatcher();
$factory = function () use ($test) { return $test; };
$factory = fn () => $test;

$this->dispatcher->addListener('foo', [$factory, 'foo'], 3);
$this->assertSame(3, $this->dispatcher->getListenerPriority('foo', [$test, 'foo']));
Expand All @@ -376,7 +376,7 @@ public function testPriorityFindsLazyListeners()
public function testGetLazyListeners()
{
$test = new TestWithDispatcher();
$factory = function () use ($test) { return $test; };
$factory = fn () => $test;

$this->dispatcher->addListener('foo', [$factory, 'foo'], 3);
$this->assertSame([[$test, 'foo']], $this->dispatcher->getListeners('foo'));
Expand Down
4 changes: 2 additions & 2 deletions Tests/ImmutableEventDispatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public function testHasListenersDelegates()
public function testAddListenerDisallowed()
{
$this->expectException(\BadMethodCallException::class);
$this->dispatcher->addListener('event', function () { return 'foo'; });
$this->dispatcher->addListener('event', fn () => 'foo');
}

public function testAddSubscriberDisallowed()
Expand All @@ -89,7 +89,7 @@ public function testAddSubscriberDisallowed()
public function testRemoveListenerDisallowed()
{
$this->expectException(\BadMethodCallException::class);
$this->dispatcher->removeListener('event', function () { return 'foo'; });
$this->dispatcher->removeListener('event', fn () => 'foo');
}

public function testRemoveSubscriberDisallowed()
Expand Down

0 comments on commit 514f5a1

Please sign in to comment.