-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
Leverage First-class callable syntax #47672
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
Leverage First-class callable syntax #47672
Conversation
There are related failures in the test suite, I'm looking into it. |
src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php
Outdated
Show resolved
Hide resolved
3a2b728
to
34532a1
Compare
So I've dug a little more to properly implement these improvements. Hopefully, I got a good enough understanding now. Mainly I've been reminded that $this->foo(...) === $this->foo(...) // false
[$this, 'foo'] === [$this, 'foo'] // true Even though both are callables, they are This has consequences on unit tests where equality is tested. Symfony public api returning callables with the array syntax form must be kept that way and therefore their related tests too. A couple more points:
--- a/src/Symfony/Component/EventDispatcher/EventDispatcher.php
+++ b/src/Symfony/Component/EventDispatcher/EventDispatcher.php
@@ -163,10 +163,10 @@ class EventDispatcher implements EventDispatcherInterface
if (\is_string($params)) {
$this->addListener($eventName, [$subscriber, $params]);
} elseif (\is_string($params[0])) {
- $this->addListener($eventName, [$subscriber, $params[0]], $params[1] ?? 0);
+ $this->addListener($eventName, $subscriber->{$params[0]}(...), $params[1] ?? 0);
} else {
foreach ($params as $listener) {
- $this->addListener($eventName, [$subscriber, $listener[0]], $listener[1] ?? 0);
+ $this->addListener($eventName, $subscriber->{$params[0]}(...), $listener[1] ?? 0);
}
}
} As it changes the expected behavior of what's stored inside the
Ready to be reviewed again. Test failures are not related. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mainly I've been reminded that
$this->foo(...) === $this->foo(...) // false [$this, 'foo'] === [$this, 'foo'] // true
Note this also: $this->foo(...) == $this->foo(...) // true
, see #46262
As stated by @stof
\Symfony\Component\DependencyInjection\Definition::setFactory
doesn't support closures.
It might but we first need to teach PhpDumper how to dump such closures. There might be an issue with closures created out of non-public methods though.
I refrained from applying this in
\Symfony\Component\EventDispatcher\EventDispatcher::addSubscriber
You did right, this would badly break BC.
I've reverted updates on
EventDispatcherTest::testGetLazyListeners
We test lazy-listeners there: those are not native php callables so they cannot use first-class callable syntax (they are [\Closure, string]
tuples.)
I've reverted updates on the
ErrorHandler
component as it breaks a lot of tests and there's too much dark magic going on to make the modernizing effort worth it.
No dark magic here, just regular php error handling ;) Yes, adopting a new syntax should not lead to invasive changes.
It seems
ServiceValueResolver::resolve
doesn't support$request->attributes->get('_controller')
returning aClosure
.
It does: it skips them. That's expected: controllers-as-services are never dumped as closures anyway.
src/Symfony/Component/EventDispatcher/Tests/EventDispatcherTest.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/HttpKernel/Tests/DataCollector/RequestDataCollectorTest.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/HttpKernel/Tests/DataCollector/RequestDataCollectorTest.php
Outdated
Show resolved
Hide resolved
34532a1
to
26d9ce9
Compare
@nicolas-grekas Thanks for your thorough review, very motivating :) Ready to be reviewed again. 👍 |
Thank you @tigitz. |
Rationale
https://wiki.php.net/rfc/first_class_callable_syntax
Mainly:
I'd argue that it also improves readability and IDE color syntax also helps:
I've manually reviewed each changes and discarded some of them where
[Foo::class, 'method']
was intended to be tested with this specific syntax