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 |
||
| 32 | class GearmanExecute extends AbstractGearmanService implements ContainerAwareInterface |
||
| 33 | { |
||
| 34 | use ContainerAwareTrait; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var EventDispatcherInterface |
||
| 38 | * |
||
| 39 | * EventDispatcher instance |
||
| 40 | */ |
||
| 41 | protected $eventDispatcher; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var OutputInterface |
||
| 45 | * |
||
| 46 | * Output instance |
||
| 47 | */ |
||
| 48 | protected $output; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var OptionsResolver |
||
| 52 | */ |
||
| 53 | protected $executeOptionsResolver; |
||
|
|
|||
| 54 | |||
| 55 | /** |
||
| 56 | * Boolean to track if a system signal has been received |
||
| 57 | * @var boolean |
||
| 58 | */ |
||
| 59 | protected $stopWorkSignalReceived; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Bucket with worker objects configuration for PECL |
||
| 63 | * @var array |
||
| 64 | */ |
||
| 65 | protected $workersBucket = array(); |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Construct method |
||
| 69 | * |
||
| 70 | * @param GearmanCacheWrapper $gearmanCacheWrapper GearmanCacheWrapper |
||
| 71 | * @param array $defaultSettings The default settings for the bundle |
||
| 72 | */ |
||
| 73 | 2 | public function __construct(GearmanCacheWrapper $gearmanCacheWrapper, array $defaultSettings) |
|
| 102 | |||
| 103 | /** |
||
| 104 | * Toggles that work should be stopped, we only subscribe to SIGTERM and SIGHUP |
||
| 105 | * @param int $signo Signal number |
||
| 106 | */ |
||
| 107 | public function handleSystemSignal($signo) |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Set event dispatcher |
||
| 114 | * |
||
| 115 | * @param EventDispatcherInterface $eventDispatcher |
||
| 116 | * |
||
| 117 | * @return GearmanExecute self Object |
||
| 118 | */ |
||
| 119 | 2 | public function setEventDispatcher(EventDispatcherInterface $eventDispatcher) |
|
| 125 | |||
| 126 | /** |
||
| 127 | * Set output |
||
| 128 | * |
||
| 129 | * @param OutputInterface $output |
||
| 130 | * |
||
| 131 | * @return GearmanExecute self Object |
||
| 132 | */ |
||
| 133 | 12 | public function setOutput(OutputInterface $output) |
|
| 139 | |||
| 140 | /** |
||
| 141 | * Executes a job given a jobName and given settings and annotations of job |
||
| 142 | * |
||
| 143 | * @param string $jobName Name of job to be executed |
||
| 144 | * @param array $options Array of options passed to the callback |
||
| 145 | * @param \GearmanWorker $gearmanWorker Worker instance to use |
||
| 146 | */ |
||
| 147 | 1 | public function executeJob($jobName, array $options = array(), \GearmanWorker $gearmanWorker = null) |
|
| 155 | |||
| 156 | /** |
||
| 157 | * Given a worker, execute GearmanWorker function defined by job. |
||
| 158 | * |
||
| 159 | * @param array $worker Worker definition |
||
| 160 | * @param array $options Array of options passed to the callback |
||
| 161 | * @param \GearmanWorker $gearmanWorker Worker instance to use |
||
| 162 | * |
||
| 163 | * @throws ServerConnectionException if a connection to a server was not possible. |
||
| 164 | * |
||
| 165 | * @return GearmanExecute self Object |
||
| 166 | */ |
||
| 167 | 1 | private function callJob(Array $worker, array $options = array(), \GearmanWorker $gearmanWorker = null) |
|
| 225 | |||
| 226 | /** |
||
| 227 | * Given a worker settings, return Job instance |
||
| 228 | * |
||
| 229 | * @param array $worker Worker settings |
||
| 230 | * |
||
| 231 | * @return Object Job instance |
||
| 232 | */ |
||
| 233 | 1 | private function createJob(array $worker) |
|
| 263 | |||
| 264 | /** |
||
| 265 | * Given a GearmanWorker and an instance of Job, run it |
||
| 266 | * |
||
| 267 | * @param \GearmanWorker $gearmanWorker Gearman Worker |
||
| 268 | * @param Object $objInstance Job instance |
||
| 269 | * @param array $jobs Array of jobs to subscribe |
||
| 270 | * @param integer $iterations Number of iterations |
||
| 271 | * @param integer $timeout Timeout |
||
| 272 | * |
||
| 273 | * @return GearmanExecute self Object |
||
| 274 | */ |
||
| 275 | 1 | private function runJob(\GearmanWorker $gearmanWorker, $objInstance, array $jobs, $iterations, $timeout = null) |
|
| 340 | |||
| 341 | /** |
||
| 342 | * Adds into worker all defined Servers. |
||
| 343 | * If any is defined, performs default method |
||
| 344 | * |
||
| 345 | * @param \GearmanWorker $gmworker Worker to perform configuration |
||
| 346 | * @param array $servers Servers array |
||
| 347 | * |
||
| 348 | * @throws ServerConnectionException if a connection to a server was not possible. |
||
| 349 | * |
||
| 350 | * @return array Successfully added servers |
||
| 351 | */ |
||
| 352 | 1 | private function addServers(\GearmanWorker $gmworker, array $servers) |
|
| 371 | |||
| 372 | /** |
||
| 373 | * Executes a worker given a workerName subscribing all his jobs inside and |
||
| 374 | * given settings and annotations of worker and jobs |
||
| 375 | * |
||
| 376 | * @param string $workerName Name of worker to be executed |
||
| 377 | */ |
||
| 378 | public function executeWorker($workerName, array $options = array()) |
||
| 387 | |||
| 388 | /** |
||
| 389 | * Wrapper function handler for all registered functions |
||
| 390 | * This allows us to do some nice logging when jobs are started/finished |
||
| 391 | * |
||
| 392 | * @see https://github.com/brianlmoon/GearmanManager/blob/ffc828dac2547aff76cb4962bb3fcc4f454ec8a2/GearmanPeclManager.php#L95-206 |
||
| 393 | * |
||
| 394 | * @param \GearmanJob $job |
||
| 395 | * |
||
| 396 | * @return mixed |
||
| 397 | */ |
||
| 398 | 1 | public function handleJob(\GearmanJob $job) |
|
| 431 | } |
||
| 432 |
Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.