These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php declare(strict_types=1); |
||
2 | |||
3 | namespace ApiClients\Foundation; |
||
4 | |||
5 | use ApiClients\Foundation\Events\CommandLocatorEvent; |
||
6 | use ApiClients\Foundation\Hydrator\Factory as HydratorFactory; |
||
7 | use ApiClients\Foundation\Hydrator\Hydrator; |
||
8 | use ApiClients\Foundation\Transport\Client as TransportClient; |
||
9 | use ApiClients\Foundation\Transport\Factory as TransportFactory; |
||
10 | use Interop\Container\ContainerInterface; |
||
11 | use League\Container\Container; |
||
12 | use League\Container\ReflectionContainer; |
||
13 | use League\Event\Emitter; |
||
14 | use League\Event\EmitterInterface; |
||
15 | use League\Tactician\CommandBus; |
||
16 | use League\Tactician\Container\ContainerLocator; |
||
17 | use League\Tactician\Handler\CommandHandlerMiddleware; |
||
18 | use League\Tactician\Handler\CommandNameExtractor\ClassNameExtractor; |
||
19 | use League\Tactician\Handler\MethodNameInflector\HandleInflector; |
||
20 | use React\EventLoop\LoopInterface; |
||
21 | |||
22 | final class Factory |
||
23 | { |
||
24 | public static function create( |
||
25 | LoopInterface $loop = null, |
||
26 | ContainerInterface $wrappedContainer = null, |
||
27 | array $options = [] |
||
28 | ): Client { |
||
29 | $container = self::createContainer($wrappedContainer); |
||
30 | |||
31 | $container->share(EmitterInterface::class, new Emitter()); |
||
32 | $container->share(TransportClient::class, self::createTransport($container, $loop, $options)); |
||
33 | $container->share(Hydrator::class, self::createHydrator($container, $options)); |
||
34 | $container->share(CommandBus::class, function () use ($container) { |
||
35 | return self::createCommandBus($container); |
||
36 | }); |
||
37 | |||
38 | return new Client( |
||
39 | $container |
||
40 | ); |
||
41 | } |
||
42 | |||
43 | private static function createContainer(ContainerInterface $wrappedContainer = null): Container |
||
44 | { |
||
45 | $container = new Container(); |
||
46 | $container->delegate(new ReflectionContainer()); |
||
47 | |||
48 | if ($wrappedContainer instanceof ContainerInterface) { |
||
49 | $container->delegate($wrappedContainer); |
||
50 | } |
||
51 | |||
52 | return $container; |
||
53 | } |
||
54 | |||
55 | private static function createCommandBus(ContainerInterface $container): CommandBus |
||
56 | { |
||
57 | $commandToHandlerMap = self::mapCommandsToHandlers($container->get(EmitterInterface::class)); |
||
58 | |||
59 | $containerLocator = new ContainerLocator( |
||
60 | $container, |
||
61 | $commandToHandlerMap |
||
62 | ); |
||
63 | |||
64 | $commandHandlerMiddleware = new CommandHandlerMiddleware( |
||
65 | new ClassNameExtractor(), |
||
66 | $containerLocator, |
||
67 | new HandleInflector() |
||
68 | ); |
||
69 | |||
70 | return new CommandBus([ |
||
71 | $commandHandlerMiddleware, |
||
72 | ]); |
||
73 | } |
||
74 | |||
75 | private static function mapCommandsToHandlers(Emitter $emitter): array |
||
76 | { |
||
77 | return $emitter->emit(CommandLocatorEvent::create())->getMap(); |
||
78 | } |
||
79 | |||
80 | private static function createTransport( |
||
81 | ContainerInterface $container, |
||
82 | LoopInterface $loop = null, |
||
83 | array $options = [] |
||
84 | ): TransportClient { |
||
85 | return TransportFactory::create($container, $loop, $options); |
||
0 ignored issues
–
show
|
|||
86 | } |
||
87 | |||
88 | private static function createHydrator(ContainerInterface $container, array $options = []) |
||
89 | { |
||
90 | if (isset($options[Options::HYDRATOR]) && $options[Options::HYDRATOR] instanceof Hydrator) { |
||
91 | return $options[Options::HYDRATOR]; |
||
92 | } |
||
93 | |||
94 | if (!isset($options[Options::HYDRATOR_OPTIONS])) { |
||
95 | throw new \Exception('Missing Hydrator options'); |
||
96 | } |
||
97 | |||
98 | return HydratorFactory::create($container, $options[Options::HYDRATOR_OPTIONS]); |
||
0 ignored issues
–
show
The call to
Factory::create() has too many arguments starting with $options[\ApiClients\Fou...ions::HYDRATOR_OPTIONS] .
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue. If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. In this case you can add the
Loading history...
|
|||
99 | } |
||
100 | } |
||
101 |
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.