Complex classes like CompilerPass often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use CompilerPass, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
24 | class CompilerPass implements CompilerPassInterface |
||
25 | { |
||
26 | /** |
||
27 | * @var array |
||
28 | */ |
||
29 | private $injectables; |
||
30 | |||
31 | /** |
||
32 | * @var array |
||
33 | */ |
||
34 | private $filter; |
||
35 | |||
36 | /** |
||
37 | * @var array |
||
38 | */ |
||
39 | private $exclude; |
||
40 | |||
41 | /** |
||
42 | * {@inheritdoc} |
||
43 | */ |
||
44 | 9 | public function process(ContainerBuilder $container) |
|
45 | { |
||
46 | 9 | if (!$container->hasParameter('runopencode.traitor.injectables')) { |
|
47 | 1 | return; |
|
48 | } |
||
49 | |||
50 | 8 | $this->injectables = $container->getParameter('runopencode.traitor.injectables'); |
|
1 ignored issue
–
show
|
|||
51 | 8 | $this->filter = [ |
|
52 | 8 | 'tags' => ($container->hasParameter('runopencode.traitor.filter.tags')) ? array_combine($tags = $container->getParameter('runopencode.traitor.filter.tags'), $tags) : [], |
|
53 | 8 | 'namespaces' => ($container->hasParameter('runopencode.traitor.filter.namespaces')) ? $container->getParameter('runopencode.traitor.filter.namespaces') : [], |
|
54 | ]; |
||
55 | 8 | $this->exclude = [ |
|
56 | 8 | 'tags' => ($container->hasParameter('runopencode.traitor.exclude.tags')) ? array_combine($tags = $container->getParameter('runopencode.traitor.exclude.tags'), $tags) : [], |
|
57 | 8 | 'namespaces' => ($container->hasParameter('runopencode.traitor.exclude.namespaces')) ? $container->getParameter('runopencode.traitor.exclude.namespaces') : [], |
|
58 | 8 | 'classes' => ($container->hasParameter('runopencode.traitor.exclude.classes')) ? array_combine($classes = $container->getParameter('runopencode.traitor.exclude.classes'), $classes) : [], |
|
59 | 8 | 'services' => ($container->hasParameter('runopencode.traitor.exclude.services')) ? array_combine($services = $container->getParameter('runopencode.traitor.exclude.services'), $services) : [], |
|
60 | ]; |
||
61 | |||
62 | 8 | if (0 === count($this->filter['tags']) + count($this->filter['namespaces'])) { |
|
63 | 6 | $this->filter = null; |
|
64 | } |
||
65 | |||
66 | 8 | if (0 === count($this->exclude['tags']) + count($this->exclude['namespaces']) + count($this->exclude['classes']) + count($this->exclude['services'])) { |
|
67 | 4 | $this->exclude = null; |
|
68 | } |
||
69 | |||
70 | 8 | $injectableServices = $this->findInjectableServices($container); |
|
71 | |||
72 | 8 | foreach ($injectableServices as $definition) { |
|
73 | 8 | $this->processInjections($definition); |
|
74 | } |
||
75 | 8 | } |
|
76 | |||
77 | /** |
||
78 | * Find all services which should be injected with services via traits. |
||
79 | * |
||
80 | * @param ContainerBuilder $container |
||
81 | * @return array |
||
82 | */ |
||
83 | 8 | private function findInjectableServices(ContainerBuilder $container) |
|
84 | { |
||
85 | 8 | $services = []; |
|
86 | |||
87 | 8 | foreach ($container->getDefinitions() as $serviceId => $definition) { |
|
88 | |||
89 | 8 | if ($definition->isSynthetic() || !$definition->getClass()) { |
|
90 | 1 | continue; |
|
91 | } |
||
92 | |||
93 | 8 | if ($this->isInjectable($serviceId, $definition) && !$this->isExcluded($serviceId, $definition)) { |
|
94 | 8 | $services[$serviceId] = $definition; |
|
95 | } |
||
96 | } |
||
97 | |||
98 | 8 | return $services; |
|
99 | } |
||
100 | |||
101 | /** |
||
102 | * Check if service definition should be injected with service via traits. |
||
103 | * |
||
104 | * @param string $serviceId |
||
105 | * @param Definition $definition |
||
106 | * @return bool |
||
107 | */ |
||
108 | 8 | private function isInjectable($serviceId, Definition $definition) |
|
109 | { |
||
110 | 8 | if (null === $this->filter) { |
|
111 | 6 | return true; |
|
112 | } |
||
113 | |||
114 | 2 | $class = $definition->getClass(); |
|
115 | |||
116 | 2 | foreach ($this->filter['namespaces'] as $namespace) { |
|
117 | |||
118 | 1 | if (ClassUtils::isWithinNamespace($class, $namespace)) { |
|
119 | 1 | return true; |
|
120 | } |
||
121 | } |
||
122 | |||
123 | 2 | foreach ($definition->getTags() as $tag => $attributes) { |
|
124 | |||
125 | 1 | if (isset($this->filter['tags'][$tag])) { |
|
126 | 1 | return true; |
|
127 | } |
||
128 | } |
||
129 | |||
130 | 2 | return false; |
|
131 | } |
||
132 | |||
133 | /** |
||
134 | * Check if service definition should be excluded from service injection via traits. |
||
135 | * |
||
136 | * @param string $serviceId |
||
137 | * @param Definition $definition |
||
138 | * @return bool |
||
139 | */ |
||
140 | 8 | private function isExcluded($serviceId, Definition $definition) |
|
141 | { |
||
142 | 8 | if (null === $this->exclude) { |
|
143 | 4 | return false; |
|
144 | } |
||
145 | |||
146 | 4 | if (isset($this->exclude['services'][$serviceId])) { |
|
147 | 1 | return true; |
|
148 | } |
||
149 | |||
150 | 4 | $class = $definition->getClass(); |
|
151 | |||
152 | 4 | if (isset($this->exclude['classes'][$class])) { |
|
153 | 1 | return true; |
|
154 | } |
||
155 | |||
156 | 4 | foreach ($this->exclude['namespaces'] as $namespace) { |
|
157 | |||
158 | 1 | if (ClassUtils::isWithinNamespace($class, $namespace)) { |
|
159 | 1 | return true; |
|
160 | } |
||
161 | } |
||
162 | |||
163 | 4 | foreach ($definition->getTags() as $tag => $attribures) { |
|
164 | |||
165 | 1 | if (isset($this->exclude['tags'][$tag])) { |
|
166 | 1 | return true; |
|
167 | } |
||
168 | } |
||
169 | |||
170 | 4 | return false; |
|
171 | } |
||
172 | |||
173 | /** |
||
174 | * Process service injections via traits. |
||
175 | * |
||
176 | * @param Definition $definition |
||
177 | */ |
||
178 | 8 | private function processInjections(Definition $definition) |
|
191 | |||
192 | /** |
||
193 | * Process service injection parameters. |
||
194 | * |
||
195 | * @param array $arguments |
||
196 | * @return array |
||
197 | */ |
||
198 | 7 | private function processArguments(array $arguments) |
|
208 | |||
209 | /** |
||
210 | * Process service as argument. |
||
211 | * |
||
212 | * @param array $argument |
||
213 | * @return Reference |
||
214 | */ |
||
215 | 7 | private function processServiceAsArgument(array $argument) |
|
229 | |||
230 | /** |
||
231 | * Process expression as argument. |
||
232 | * |
||
233 | * @param array $argument |
||
234 | * @return Expression |
||
235 | */ |
||
236 | 5 | private function processExpressionAsArgument(array $argument) |
|
240 | |||
241 | /** |
||
242 | * Process string as argument |
||
243 | * |
||
244 | * @param array $argument |
||
245 | * @return string |
||
246 | */ |
||
247 | 5 | private function processStringAsArgument(array $argument) |
|
251 | |||
252 | /** |
||
253 | * Process constant as argument. |
||
254 | * |
||
255 | * @param array $argument |
||
256 | * @return mixed |
||
257 | */ |
||
258 | 5 | private function processConstantAsArgument(array $argument) |
|
262 | } |
||
263 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..