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) |
|
| 50 | |||
| 51 | 4 | protected function setupAlias(ContainerBuilder $container, $defaultManagerType, $type) |
|
| 68 | |||
| 69 | 4 | 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 | 4 | protected function getDirectory($managerType) |
|
| 131 | { |
||
| 132 | 4 | switch ($managerType) { |
|
| 133 | case 'mongodb': // deprecated remove in 3.0 |
||
| 134 | case 'odm': |
||
| 135 | 3 | return 'Document'; |
|
| 136 | case 'beanstalkd': |
||
| 137 | 1 | return 'Beanstalkd'; |
|
| 138 | case 'rabbit_mq': |
||
| 139 | 1 | return 'RabbitMQ'; |
|
| 140 | case 'orm': |
||
| 141 | 1 | return 'Entity'; |
|
| 142 | } |
||
| 143 | |||
| 144 | 1 | 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 | 4 | protected function getJobClass(ContainerBuilder $container) |
|
| 157 | { |
||
| 158 | 4 | $jobClass = $container->getParameter('dtc_queue.class_job'); |
|
| 159 | 4 | if (!$jobClass) { |
|
| 160 | 4 | if ($directory = $this->getDirectory($managerType = $container->getParameter('dtc_queue.default_manager'))) { |
|
| 161 | 3 | $jobClass = 'Dtc\QueueBundle\\'.$directory.'\Job'; |
|
| 162 | } else { |
||
| 163 | 1 | throw new InvalidConfigurationException('Unknown default_manager type '.$managerType.' - please specify a Job class in the \'class\' configuration parameter'); |
|
| 164 | } |
||
| 165 | } |
||
| 166 | |||
| 167 | 3 | $this->testClass($jobClass, Job::class); |
|
| 168 | |||
| 169 | 3 | return $jobClass; |
|
| 170 | } |
||
| 171 | |||
| 172 | 4 | protected function getRunManagerType(ContainerBuilder $container) |
|
| 181 | |||
| 182 | 4 | protected function getJobTimingManagerType(ContainerBuilder $container) |
|
| 183 | { |
||
| 184 | 4 | $managerType = $this->getRunManagerType($container); |
|
| 185 | 4 | if ($container->hasParameter('dtc_queue.job_timing_manager')) { |
|
| 186 | 4 | $managerType = 'dtc_queue.job_timing_manager'; |
|
| 187 | } |
||
| 188 | |||
| 189 | 4 | return $managerType; |
|
| 190 | } |
||
| 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 | case 'mongodb': // deprecated remove in 3.0 |
||
| 198 | case 'odm': |
||
| 199 | 3 | $runClass = 'Dtc\\QueueBundle\\Document\\'.$className; |
|
| 200 | 3 | break; |
|
| 201 | case 'orm': |
||
| 202 | 1 | $runClass = 'Dtc\\QueueBundle\\Entity\\'.$className; |
|
| 203 | 1 | 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
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.