-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
ServiceSubscriberTrait doesn't fully support PHP8 union types #43913
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
Comments
/cc @kbond :) |
@gmichard, to clarify, are you trying to "autowire" @nicolas-grekas can you confirm that union types are not supported by I'm thinking to skip methods without |
it's never been specified, but it's trivial to define them so I think we should consider them as defined actually: the leading |
Ok, this will be in 5.4+ only, correct? In 4.4+ and 5.4's deprecation layer I'm thinking we should ignore union/intersection types. |
A more complete example could be: <?php
// File: src/Service/ExampleHandler.php
// use [...]
class ExampleHandler implements ServiceSubscriberInterface
{
use ServiceSubscriberTrait;
/**
* A regular method that return a object type or another.
*
* This method doesn't need to use the service locator.
*/
private function getSomethingOrOther(): SomethingInterface|Other
{
/** @var $something SomethingInterface|null */
$something = $this->somethingProvider()->load();
if (null !== $something) {
return $something;
}
return new Other();
}
/**
* A Service Locator (thanks to the autowiring and the ServiceSubscriberTrait)
*/
private function somethingProvider(): SomethingProviderInterface
{
return $this->container->get(__METHOD__);
}
} The both methods matches the criteria to add it as subscribed services (non-static, non-internal, etc.). I hope this words are clarifying and not confusing. |
For 5.4+ yes @kbond |
@gmichard Ok, that's what I thought. Keep in mind that you'll also have trouble with non-union return types (ie @nicolas-grekas, #43922 provides a fix for this specific issue on 4.4 - do you not want to fix on that branch? |
… `ServiceSubscriberTrait` (kbond) This PR was squashed before being merged into the 4.4 branch. Discussion ---------- [DependencyInjection] only allow `ReflectionNamedType` for `ServiceSubscriberTrait` | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Fix #43913 | License | MIT | Doc PR | n/a I'll follow this up with a PR on 5.4 to allow union/intersections when using the `SubscribedService` attribute (once `ServiceSubscriberInterface` [supports this](#43913 (comment))). Commits ------- b616042 [DependencyInjection] only allow `ReflectionNamedType` for `ServiceSubscriberTrait`
… together with `ServiceSubscriberInterface` (kbond) This PR was squashed before being merged into the 5.4 branch. Discussion ---------- [DependencyInjection] Fix support for unions/intersections together with `ServiceSubscriberInterface` | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | - | License | MIT | Doc PR | - Continuation of #43479 per [discussion](#43913 (comment)) with `@nicolas`-grekas. Todo: - [x] intersection type support/tests - [x] `ServiceSubscriberTrait` support Commits ------- 123842a [DependencyInjection] Fix support for unions/intersections together with `ServiceSubscriberInterface`
Symfony version(s) affected
5.3.10
Description
An error occurs when using the Service Subscriber Trait on a service class that have a method with more than one type (i.e. union type) for return type.
How to reproduce
ServiceSubscriberTrait
and that have a method with more that one type for return type. See the followingExampleHandler
class.bin/console
With this example, if I add a xDebug breakpoint on the line 45 of ServiceSubscriberTrait
with the condition of suspension:
(Note the
!method_exists($returnType, 'isBuiltin')
)I can check the values of
$method
and$returnType
Possible Solution
We can see in the PHP ReflectionType class "Changelog" section that:
We can see that the issue [3.4] Fix support for PHP8 union types #37340 by @nicolas-grekas mentions the "PHP8 Union types" and the
isBuilin
method.But the modifications doesn't seem to fix this use case.
Maybe a test is missing before calling the
isBuiltin
(like$returnType instanceof ReflectionNamedType
), but I'm not sure about the expections here and the eventual side-effects.Additional Context
Error trace in console.
The text was updated successfully, but these errors were encountered: