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 |
||
33 | { |
||
34 | /** |
||
35 | * @var ContainerInterface |
||
36 | * |
||
37 | * Container instance |
||
38 | */ |
||
39 | private $container; |
||
40 | |||
41 | /** |
||
42 | * @var EventDispatcherInterface |
||
43 | * |
||
44 | * EventDispatcher instance |
||
45 | */ |
||
46 | protected $eventDispatcher; |
||
47 | |||
48 | /** |
||
49 | * @var OutputInterface |
||
50 | * |
||
51 | * Output instance |
||
52 | */ |
||
53 | protected $output; |
||
54 | |||
55 | /** |
||
56 | * @var OptionsResolver |
||
57 | */ |
||
58 | protected $executeOptionsResolver; |
||
|
|||
59 | |||
60 | /** |
||
61 | * Boolean to track if a system signal has been received |
||
62 | * @var boolean |
||
63 | */ |
||
64 | protected $stopWorkSignalReceived; |
||
65 | |||
66 | /** |
||
67 | * Bucket with worker objects configuration for PECL |
||
68 | * @var array |
||
69 | */ |
||
70 | protected $workersBucket = array(); |
||
71 | |||
72 | /** |
||
73 | * Construct method |
||
74 | * |
||
75 | * @param GearmanCacheWrapper $gearmanCacheWrapper GearmanCacheWrapper |
||
76 | * @param array $defaultSettings The default settings for the bundle |
||
77 | */ |
||
78 | 2 | public function __construct(GearmanCacheWrapper $gearmanCacheWrapper, array $defaultSettings) |
|
107 | |||
108 | /** |
||
109 | * Toggles that work should be stopped, we only subscribe to SIGTERM and SIGHUP |
||
110 | * @param int $signo Signal number |
||
111 | */ |
||
112 | public function handleSystemSignal($signo) |
||
116 | |||
117 | /** |
||
118 | * Set container |
||
119 | * |
||
120 | * @param ContainerInterface $container Container |
||
121 | * |
||
122 | * @return GearmanExecute self Object |
||
123 | */ |
||
124 | 1 | public function setContainer(ContainerInterface $container) |
|
130 | |||
131 | /** |
||
132 | * Set event dispatcher |
||
133 | * |
||
134 | * @param EventDispatcherInterface $eventDispatcher |
||
135 | * |
||
136 | * @return GearmanExecute self Object |
||
137 | */ |
||
138 | 2 | public function setEventDispatcher(EventDispatcherInterface $eventDispatcher) |
|
144 | |||
145 | /** |
||
146 | * Set output |
||
147 | * |
||
148 | * @param OutputInterface $output |
||
149 | * |
||
150 | * @return GearmanExecute self Object |
||
151 | */ |
||
152 | 12 | public function setOutput(OutputInterface $output) |
|
158 | |||
159 | /** |
||
160 | * Executes a job given a jobName and given settings and annotations of job |
||
161 | * |
||
162 | * @param string $jobName Name of job to be executed |
||
163 | * @param array $options Array of options passed to the callback |
||
164 | * @param \GearmanWorker $gearmanWorker Worker instance to use |
||
165 | */ |
||
166 | 1 | public function executeJob($jobName, array $options = array(), \GearmanWorker $gearmanWorker = null) |
|
174 | |||
175 | /** |
||
176 | * Given a worker, execute GearmanWorker function defined by job. |
||
177 | * |
||
178 | * @param array $worker Worker definition |
||
179 | * @param array $options Array of options passed to the callback |
||
180 | * @param \GearmanWorker $gearmanWorker Worker instance to use |
||
181 | * |
||
182 | * @throws ServerConnectionException if a connection to a server was not possible. |
||
183 | * |
||
184 | * @return GearmanExecute self Object |
||
185 | */ |
||
186 | 1 | private function callJob(Array $worker, array $options = array(), \GearmanWorker $gearmanWorker = null) |
|
244 | |||
245 | /** |
||
246 | * Given a worker settings, return Job instance |
||
247 | * |
||
248 | * @param array $worker Worker settings |
||
249 | * |
||
250 | * @return Object Job instance |
||
251 | */ |
||
252 | 1 | private function createJob(array $worker) |
|
282 | |||
283 | /** |
||
284 | * Given a GearmanWorker and an instance of Job, run it |
||
285 | * |
||
286 | * @param \GearmanWorker $gearmanWorker Gearman Worker |
||
287 | * @param Object $objInstance Job instance |
||
288 | * @param array $jobs Array of jobs to subscribe |
||
289 | * @param integer $iterations Number of iterations |
||
290 | * @param integer $timeout Timeout |
||
291 | * |
||
292 | * @return GearmanExecute self Object |
||
293 | */ |
||
294 | 1 | private function runJob(\GearmanWorker $gearmanWorker, $objInstance, array $jobs, $iterations, $timeout = null) |
|
359 | |||
360 | /** |
||
361 | * Adds into worker all defined Servers. |
||
362 | * If any is defined, performs default method |
||
363 | * |
||
364 | * @param \GearmanWorker $gmworker Worker to perform configuration |
||
365 | * @param array $servers Servers array |
||
366 | * |
||
367 | * @throws ServerConnectionException if a connection to a server was not possible. |
||
368 | * |
||
369 | * @return array Successfully added servers |
||
370 | */ |
||
371 | 1 | private function addServers(\GearmanWorker $gmworker, array $servers) |
|
390 | |||
391 | /** |
||
392 | * Executes a worker given a workerName subscribing all his jobs inside and |
||
393 | * given settings and annotations of worker and jobs |
||
394 | * |
||
395 | * @param string $workerName Name of worker to be executed |
||
396 | */ |
||
397 | public function executeWorker($workerName, array $options = array()) |
||
406 | |||
407 | /** |
||
408 | * Wrapper function handler for all registered functions |
||
409 | * This allows us to do some nice logging when jobs are started/finished |
||
410 | * |
||
411 | * @see https://github.com/brianlmoon/GearmanManager/blob/ffc828dac2547aff76cb4962bb3fcc4f454ec8a2/GearmanPeclManager.php#L95-206 |
||
412 | * |
||
413 | * @param \GearmanJob $job |
||
414 | * |
||
415 | * @return mixed |
||
416 | */ |
||
417 | 1 | public function handleJob(\GearmanJob $job) |
|
450 | } |
||
451 |
Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.