Complex classes like GearmanExecute 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 GearmanExecute, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 35 | class GearmanExecute extends AbstractGearmanService |
||
| 36 | { |
||
| 37 | /** |
||
| 38 | * @var ContainerInterface |
||
| 39 | * |
||
| 40 | * Container instance |
||
| 41 | */ |
||
| 42 | private $container; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var EventDispatcherInterface |
||
| 46 | * |
||
| 47 | * EventDispatcher instance |
||
| 48 | */ |
||
| 49 | protected $eventDispatcher; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var OutputInterface |
||
| 53 | * |
||
| 54 | * Output instance |
||
| 55 | */ |
||
| 56 | protected $output; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var OptionsResolver |
||
| 60 | */ |
||
| 61 | protected $executeOptionsResolver; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Boolean to track if a system signal has been received |
||
| 65 | * @var boolean |
||
| 66 | */ |
||
| 67 | protected $stopWorkSignalReceived; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Bucket with worker objects configuration for PECL |
||
| 71 | * @var array |
||
| 72 | */ |
||
| 73 | protected $workersBucket = []; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Construct method |
||
| 77 | * |
||
| 78 | * @param GearmanCacheWrapper $gearmanCacheWrapper GearmanCacheWrapper |
||
| 79 | * @param array $defaultSettings The default settings for the bundle |
||
| 80 | */ |
||
| 81 | 2 | public function __construct(GearmanCacheWrapper $gearmanCacheWrapper, array $defaultSettings) |
|
| 110 | |||
| 111 | /** |
||
| 112 | * Toggles that work should be stopped, we only subscribe to SIGTERM and SIGHUP |
||
| 113 | * @param int $signno Signal number |
||
|
|
|||
| 114 | */ |
||
| 115 | public function handleSystemSignal($signo) |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Set container |
||
| 122 | * |
||
| 123 | * @param ContainerInterface $container Container |
||
| 124 | * |
||
| 125 | * @return GearmanExecute self Object |
||
| 126 | */ |
||
| 127 | 1 | public function setContainer(ContainerInterface $container) |
|
| 133 | |||
| 134 | /** |
||
| 135 | * Set event dispatcher |
||
| 136 | * |
||
| 137 | * @param EventDispatcherInterface $eventDispatcher |
||
| 138 | * |
||
| 139 | * @return GearmanExecute self Object |
||
| 140 | */ |
||
| 141 | 2 | public function setEventDispatcher(EventDispatcherInterface $eventDispatcher) |
|
| 147 | |||
| 148 | /** |
||
| 149 | * Set output |
||
| 150 | * |
||
| 151 | * @param OutputInterface $output |
||
| 152 | * |
||
| 153 | * @return GearmanExecute self Object |
||
| 154 | */ |
||
| 155 | 12 | public function setOutput(OutputInterface $output) |
|
| 161 | |||
| 162 | /** |
||
| 163 | * Executes a job given a jobName and given settings and annotations of job |
||
| 164 | * |
||
| 165 | * @param string $jobName Name of job to be executed |
||
| 166 | * @param array $options Array of options passed to the callback |
||
| 167 | * @param \GearmanWorker $gearmanWorker Worker instance to use |
||
| 168 | */ |
||
| 169 | 1 | public function executeJob($jobName, array $options = array(), \GearmanWorker $gearmanWorker = null) |
|
| 177 | |||
| 178 | /** |
||
| 179 | * Given a worker, execute GearmanWorker function defined by job. |
||
| 180 | * |
||
| 181 | * @param array $worker Worker definition |
||
| 182 | * @param array $options Array of options passed to the callback |
||
| 183 | * @param \GearmanWorker $gearmanWorker Worker instance to use |
||
| 184 | * |
||
| 185 | * @throws ServerConnectionException if a connection to a server was not possible. |
||
| 186 | * |
||
| 187 | * @return GearmanExecute self Object |
||
| 188 | */ |
||
| 189 | 1 | private function callJob(Array $worker, array $options = array(), \GearmanWorker $gearmanWorker = null) |
|
| 247 | |||
| 248 | /** |
||
| 249 | * Given a worker settings, return Job instance |
||
| 250 | * |
||
| 251 | * @param array $worker Worker settings |
||
| 252 | * |
||
| 253 | * @return Object Job instance |
||
| 254 | */ |
||
| 255 | 1 | private function createJob(array $worker) |
|
| 285 | |||
| 286 | /** |
||
| 287 | * Given a GearmanWorker and an instance of Job, run it |
||
| 288 | * |
||
| 289 | * @param \GearmanWorker $gearmanWorker Gearman Worker |
||
| 290 | * @param Object $objInstance Job instance |
||
| 291 | * @param array $jobs Array of jobs to subscribe |
||
| 292 | * @param integer $iterations Number of iterations |
||
| 293 | * @param integer $timeout Timeout |
||
| 294 | * |
||
| 295 | * @return GearmanExecute self Object |
||
| 296 | */ |
||
| 297 | 1 | private function runJob(\GearmanWorker $gearmanWorker, $objInstance, array $jobs, $iterations, $timeout = null) |
|
| 361 | |||
| 362 | /** |
||
| 363 | * Adds into worker all defined Servers. |
||
| 364 | * If any is defined, performs default method |
||
| 365 | * |
||
| 366 | * @param \GearmanWorker $gmworker Worker to perform configuration |
||
| 367 | * @param array $servers Servers array |
||
| 368 | * |
||
| 369 | * @throws ServerConnectionException if a connection to a server was not possible. |
||
| 370 | * |
||
| 371 | * @return array Successfully added servers |
||
| 372 | */ |
||
| 373 | 1 | private function addServers(\GearmanWorker $gmworker, array $servers) |
|
| 392 | |||
| 393 | /** |
||
| 394 | * Executes a worker given a workerName subscribing all his jobs inside and |
||
| 395 | * given settings and annotations of worker and jobs |
||
| 396 | * |
||
| 397 | * @param string $workerName Name of worker to be executed |
||
| 398 | */ |
||
| 399 | public function executeWorker($workerName, array $options = array()) |
||
| 408 | |||
| 409 | /** |
||
| 410 | * Wrapper function handler for all registered functions |
||
| 411 | * This allows us to do some nice logging when jobs are started/finished |
||
| 412 | * |
||
| 413 | * @see https://github.com/brianlmoon/GearmanManager/blob/ffc828dac2547aff76cb4962bb3fcc4f454ec8a2/GearmanPeclManager.php#L95-206 |
||
| 414 | * |
||
| 415 | * @param \GearmanJob $job |
||
| 416 | * @param mixed $context |
||
| 417 | * |
||
| 418 | * @return mixed |
||
| 419 | */ |
||
| 420 | 1 | public function handleJob(\GearmanJob $job) |
|
| 453 | } |
||
| 454 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.
Consider the following example. The parameter
$irelandis not defined by the methodfinale(...).The most likely cause is that the parameter was changed, but the annotation was not.