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 | 4 | public function process(ContainerBuilder $container) |
|
56 | |||
57 | /** |
||
58 | * @param string $type |
||
59 | */ |
||
60 | 4 | protected function setupAlias(ContainerBuilder $container, $defaultManagerType, $type) |
|
77 | |||
78 | 4 | protected function setupAliases(ContainerBuilder $container) |
|
87 | |||
88 | /** |
||
89 | * @param ContainerBuilder $container |
||
90 | * @param Definition $definition |
||
91 | */ |
||
92 | 3 | protected function setupTaggedServices(ContainerBuilder $container, Definition $definition) |
|
111 | |||
112 | /** |
||
113 | * @param ContainerBuilder $container |
||
114 | */ |
||
115 | 2 | protected function setupDoctrineManagers(ContainerBuilder $container) |
|
131 | |||
132 | /** |
||
133 | * @param ContainerBuilder $container |
||
134 | */ |
||
135 | 2 | protected function addLiveJobs(ContainerBuilder $container) |
|
147 | |||
148 | /** |
||
149 | * @param $managerType |
||
150 | * |
||
151 | * @return null|string |
||
152 | */ |
||
153 | 4 | protected function getDirectory($managerType) |
|
154 | { |
||
155 | switch ($managerType) { |
||
156 | 4 | case 'odm': |
|
157 | 3 | return 'Document'; |
|
158 | 2 | case 'beanstalkd': |
|
159 | 1 | return 'Beanstalkd'; |
|
160 | 2 | case 'rabbit_mq': |
|
161 | 1 | return 'RabbitMQ'; |
|
162 | 2 | case 'orm': |
|
163 | 1 | return 'Entity'; |
|
164 | 2 | case 'redis': |
|
165 | 1 | return 'Redis'; |
|
166 | } |
||
167 | |||
168 | 1 | return null; |
|
169 | } |
||
170 | |||
171 | /** |
||
172 | * Determines the job class based on the queue manager type. |
||
173 | * |
||
174 | * @param ContainerBuilder $container |
||
175 | * |
||
176 | * @return mixed|string |
||
177 | * |
||
178 | * @throws InvalidConfigurationException |
||
179 | */ |
||
180 | 4 | protected function getJobClass(ContainerBuilder $container) |
|
195 | |||
196 | 4 | protected function getRunManagerType(ContainerBuilder $container) |
|
205 | |||
206 | 4 | protected function getJobTimingManagerType(ContainerBuilder $container) |
|
215 | |||
216 | /** |
||
217 | * @param string $managerType |
||
218 | * @param string $type |
||
219 | * @param string $className |
||
220 | */ |
||
221 | 3 | protected function getClass(ContainerBuilder $container, $managerType, $type, $className, $baseClass) |
|
222 | { |
||
223 | 3 | $runClass = $container->hasParameter('dtc_queue.class.'.$type) ? $container->getParameter('dtc_queue.class.'.$type) : null; |
|
224 | 3 | if (!$runClass) { |
|
225 | 3 | switch ($container->getParameter($managerType)) { |
|
226 | 3 | case 'odm': |
|
227 | 3 | $runClass = 'Dtc\\QueueBundle\\Document\\'.$className; |
|
228 | 3 | break; |
|
229 | 1 | case 'orm': |
|
230 | 1 | $runClass = 'Dtc\\QueueBundle\\Entity\\'.$className; |
|
231 | 1 | break; |
|
232 | default: |
||
233 | $runClass = $baseClass; |
||
234 | } |
||
235 | } |
||
236 | |||
237 | 3 | $this->testClass($runClass, $baseClass); |
|
238 | |||
239 | 3 | return $runClass; |
|
240 | } |
||
241 | |||
242 | /** |
||
243 | * @throws ClassNotFoundException |
||
244 | * @throws ClassNotSubclassException |
||
245 | */ |
||
246 | 3 | protected function testClass($className, $parent) |
|
257 | |||
258 | /** |
||
259 | * Determines the job class based on the queue manager type. |
||
260 | * |
||
261 | * @param ContainerBuilder $container |
||
262 | * |
||
263 | * @return mixed|string |
||
264 | * |
||
265 | * @throws ClassNotFoundException |
||
266 | * @throws ClassNotSubclassException |
||
267 | */ |
||
268 | 3 | protected function getJobClassArchive(ContainerBuilder $container) |
|
287 | } |
||
288 |