These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | /* |
||
6 | * This file is part of Symplify |
||
7 | * Copyright (c) 2016 Tomas Votruba (http://tomasvotruba.cz). |
||
8 | */ |
||
9 | |||
10 | namespace Symplify\ControllerAutowire\DependencyInjection\Compiler; |
||
11 | |||
12 | use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
||
13 | use Symfony\Component\DependencyInjection\ContainerBuilder; |
||
14 | use Symfony\Component\DependencyInjection\Definition; |
||
15 | use Symfony\Component\DependencyInjection\Reference; |
||
16 | use Symplify\ControllerAutowire\Contract\DependencyInjection\ControllerClassMapInterface; |
||
17 | use Symplify\ControllerAutowire\Controller\ControllerTrait; |
||
18 | use Symplify\ControllerAutowire\Controller\Doctrine\ControllerDoctrineTrait; |
||
19 | use Symplify\ControllerAutowire\Controller\Form\ControllerFormTrait; |
||
20 | use Symplify\ControllerAutowire\Controller\HttpKernel\ControllerHttpKernelTrait; |
||
21 | use Symplify\ControllerAutowire\Controller\Routing\ControllerRoutingTrait; |
||
22 | use Symplify\ControllerAutowire\Controller\Security\ControllerSecurityTrait; |
||
23 | use Symplify\ControllerAutowire\Controller\Serializer\ControllerSerializerTrait; |
||
24 | use Symplify\ControllerAutowire\Controller\Session\ControllerFlashTrait; |
||
25 | use Symplify\ControllerAutowire\Controller\Templating\ControllerRenderTrait; |
||
26 | |||
27 | final class AutowireControllerDependenciesPass implements CompilerPassInterface |
||
28 | { |
||
29 | /** |
||
30 | * @var ControllerClassMapInterface |
||
31 | */ |
||
32 | private $controllerClassMap; |
||
33 | |||
34 | /** |
||
35 | * @var ContainerBuilder |
||
36 | */ |
||
37 | private $containerBuilder; |
||
38 | |||
39 | /** |
||
40 | * @var array[] |
||
41 | */ |
||
42 | private $traitsToSettersToServiceNameList = [ |
||
43 | ControllerFlashTrait::class => [ |
||
44 | 'setSession' => 'session', |
||
45 | ], |
||
46 | ControllerDoctrineTrait::class => [ |
||
47 | 'setDoctrine' => 'doctrine', |
||
48 | ], |
||
49 | ControllerRoutingTrait::class => [ |
||
50 | 'setRouter' => 'router', |
||
51 | ], |
||
52 | ControllerHttpKernelTrait::class => [ |
||
53 | 'setHttpKernel' => 'http_kernel', |
||
54 | 'setRequestStack' => 'request_stack', |
||
55 | ], |
||
56 | ControllerSerializerTrait::class => [ |
||
57 | 'setSerializer' => 'serializer', |
||
58 | ], |
||
59 | ControllerSecurityTrait::class => [ |
||
60 | 'setAuthorizationChecker' => 'security.authorization_checker', |
||
61 | 'setTokenStorage' => 'security.token_storage', |
||
62 | 'setCsrfTokenManager' => 'security.csrf.token_manager', |
||
63 | ], |
||
64 | ControllerRenderTrait::class => [ |
||
65 | 'setTemplating' => 'templating', |
||
66 | 'setTwig' => 'twig', |
||
67 | ], |
||
68 | ControllerFormTrait::class => [ |
||
69 | 'setFormFactory' => 'form.factory', |
||
70 | ], |
||
71 | ]; |
||
72 | |||
73 | 1 | public function __construct(ControllerClassMapInterface $controllerClassMap) |
|
74 | { |
||
75 | 1 | $this->controllerClassMap = $controllerClassMap; |
|
76 | 1 | } |
|
77 | |||
78 | 1 | View Code Duplication | public function process(ContainerBuilder $containerBuilder) |
0 ignored issues
–
show
|
|||
79 | { |
||
80 | 1 | $this->containerBuilder = $containerBuilder; |
|
81 | |||
82 | 1 | foreach ($this->controllerClassMap->getControllers() as $serviceId => $className) { |
|
83 | 1 | $controllerDefinition = $containerBuilder->getDefinition($serviceId); |
|
84 | 1 | $this->autowireControllerTraits($controllerDefinition); |
|
85 | } |
||
86 | 1 | } |
|
87 | |||
88 | 1 | View Code Duplication | private function autowireControllerTraits(Definition $controllerDefinition) |
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository.
Loading history...
|
|||
89 | { |
||
90 | 1 | $usedTraits = class_uses($controllerDefinition->getClass()); |
|
91 | |||
92 | 1 | foreach ($this->traitsToSettersToServiceNameList as $traitClass => $setterToServiceNames) { |
|
93 | 1 | if (! $this->isTraitIncluded($traitClass, $usedTraits)) { |
|
94 | 1 | continue; |
|
95 | } |
||
96 | |||
97 | 1 | foreach ($setterToServiceNames as $setter => $serviceName) { |
|
98 | 1 | if (! $this->containerBuilder->has($serviceName)) { |
|
99 | 1 | continue; |
|
100 | } |
||
101 | |||
102 | 1 | $controllerDefinition->addMethodCall($setter, [new Reference($serviceName)]); |
|
103 | } |
||
104 | } |
||
105 | 1 | } |
|
106 | |||
107 | 1 | private function isTraitIncluded(string $traitClass, array $usedTraits) : bool |
|
108 | { |
||
109 | 1 | if (array_key_exists($traitClass, $usedTraits)) { |
|
110 | return true; |
||
111 | } |
||
112 | |||
113 | 1 | if (isset($usedTraits[ControllerTrait::class])) { |
|
114 | 1 | return true; |
|
115 | } |
||
116 | |||
117 | 1 | return false; |
|
118 | } |
||
119 | } |
||
120 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.