Complex classes like WorkerCompilerPass 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 WorkerCompilerPass, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
18 | class WorkerCompilerPass implements CompilerPassInterface |
||
19 | { |
||
20 | 3 | public function process(ContainerBuilder $container) |
|
50 | |||
51 | 3 | protected function setupAlias(ContainerBuilder $container, $defaultManagerType, $type) |
|
68 | |||
69 | 3 | protected function setupAliases(ContainerBuilder $container) |
|
78 | |||
79 | /** |
||
80 | * @param ContainerBuilder $container |
||
81 | * @param Reference[] $jobManagerRef |
||
|
|||
82 | * @param string $jobClass |
||
83 | */ |
||
84 | 3 | protected function setupTaggedServices(ContainerBuilder $container, Definition $definition, $jobClass) |
|
105 | |||
106 | 2 | protected function setupDoctrineManagers(ContainerBuilder $container) |
|
124 | |||
125 | /** |
||
126 | * @param $managerType |
||
127 | * |
||
128 | * @return null|string |
||
129 | */ |
||
130 | 3 | protected function getDirectory($managerType) |
|
131 | { |
||
132 | switch ($managerType) { |
||
133 | 3 | case 'mongodb': // deprecated remove in 3.0 |
|
134 | 3 | case 'odm': |
|
135 | 3 | return 'Document'; |
|
136 | case 'beanstalkd': |
||
137 | return 'Beanstalkd'; |
||
138 | case 'rabbit_mq': |
||
139 | return 'RabbitMQ'; |
||
140 | case 'orm': |
||
141 | return 'Entity'; |
||
142 | } |
||
143 | |||
144 | return null; |
||
145 | } |
||
146 | |||
147 | /** |
||
148 | * Determines the job class based on the queue manager type. |
||
149 | * |
||
150 | * @param ContainerBuilder $container |
||
151 | * |
||
152 | * @return mixed|string |
||
153 | * |
||
154 | * @throws InvalidConfigurationException |
||
155 | */ |
||
156 | 3 | protected function getJobClass(ContainerBuilder $container) |
|
171 | |||
172 | 3 | protected function getRunManagerType(ContainerBuilder $container) |
|
181 | |||
182 | 3 | protected function getJobTimingManagerType(ContainerBuilder $container) |
|
191 | |||
192 | 3 | protected function getClass(ContainerBuilder $container, $managerType, $type, $className, $baseClass) |
|
193 | { |
||
194 | 3 | $runClass = $container->hasParameter('dtc_queue.class_'.$type) ? $container->getParameter('dtc_queue.class_'.$type) : null; |
|
195 | 3 | if (!$runClass) { |
|
196 | 3 | switch ($container->getParameter($managerType)) { |
|
197 | 3 | case 'mongodb': // deprecated remove in 3.0 |
|
198 | 3 | case 'odm': |
|
199 | 3 | $runClass = 'Dtc\\QueueBundle\\Document\\'.$className; |
|
200 | 3 | break; |
|
201 | case 'orm': |
||
202 | $runClass = 'Dtc\\QueueBundle\\Entity\\'.$className; |
||
203 | break; |
||
204 | default: |
||
205 | $runClass = $baseClass; |
||
206 | } |
||
207 | } |
||
208 | |||
209 | 3 | $this->testClass($runClass, $baseClass); |
|
210 | |||
211 | 3 | return $runClass; |
|
212 | } |
||
213 | |||
214 | /** |
||
215 | * @throws ClassNotFoundException |
||
216 | * @throws ClassNotSubclassException |
||
217 | */ |
||
218 | 3 | protected function testClass($className, $parent) |
|
229 | |||
230 | /** |
||
231 | * Determines the job class based on the queue manager type. |
||
232 | * |
||
233 | * @param ContainerBuilder $container |
||
234 | * |
||
235 | * @return mixed|string |
||
236 | * |
||
237 | * @throws ClassNotFoundException |
||
238 | * @throws ClassNotSubclassException |
||
239 | */ |
||
240 | 3 | protected function getJobClassArchive(ContainerBuilder $container) |
|
260 | } |
||
261 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.