22
33namespace SimpleBus \SymfonyBridge \DependencyInjection \Compiler ;
44
5+ use ReflectionClass ;
6+ use ReflectionMethod ;
7+ use ReflectionNamedType ;
58use Symfony \Component \DependencyInjection \Compiler \CompilerPassInterface ;
69use Symfony \Component \DependencyInjection \ContainerBuilder ;
710
811final class AutoRegister implements CompilerPassInterface
912{
10- private $ tagName ;
11- private $ tagAttribute ;
13+ private string $ tagName ;
14+ private string $ tagAttribute ;
1215
13- public function __construct ($ tagName , $ tagAttribute )
16+ public function __construct (string $ tagName , string $ tagAttribute )
1417 {
1518 $ this ->tagName = $ tagName ;
1619 $ this ->tagAttribute = $ tagAttribute ;
1720 }
1821
19- public function process (ContainerBuilder $ container )
22+ public function process (ContainerBuilder $ container ): void
2023 {
2124 foreach ($ container ->findTaggedServiceIds ($ this ->tagName ) as $ serviceId => $ tags ) {
2225 foreach ($ tags as $ tagAttributes ) {
@@ -33,9 +36,10 @@ public function process(ContainerBuilder $container)
3336 $ definition = $ container ->getDefinition ($ serviceId );
3437
3538 // check if service id is class name
36- $ reflectionClass = new \ReflectionClass ($ definition ->getClass () ?: $ serviceId );
39+ /** @phpstan-ignore-next-line */
40+ $ reflectionClass = new ReflectionClass ($ definition ->getClass () ?: $ serviceId );
3741
38- $ methods = $ reflectionClass ->getMethods (\ ReflectionMethod::IS_PUBLIC );
42+ $ methods = $ reflectionClass ->getMethods (ReflectionMethod::IS_PUBLIC );
3943
4044 $ tagAttributes = [];
4145 foreach ($ methods as $ method ) {
@@ -54,12 +58,17 @@ public function process(ContainerBuilder $container)
5458 $ parameters = $ method ->getParameters ();
5559
5660 // if no param, optional param or non-class param, skip
57- if (1 !== count ($ parameters ) || $ parameters [0 ]->isOptional () || null === $ parameters [0 ]->getType ()) {
61+ if (1 !== count ($ parameters ) || $ parameters [0 ]->isOptional ()) {
62+ continue ;
63+ }
64+
65+ $ type = $ parameters [0 ]->getType ();
66+ if (null === $ type || !$ type instanceof ReflectionNamedType) {
5867 continue ;
5968 }
6069
6170 // get the class name
62- $ handles = $ parameters [ 0 ]-> getType () ->getName ();
71+ $ handles = $ type ->getName ();
6372
6473 $ tagAttributes [] = [
6574 $ this ->tagAttribute => $ handles ,
0 commit comments