Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like ComplexJob 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 ComplexJob, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
11 | class ComplexJob implements \ArrayAccess { |
||
12 | use \PHPDaemon\Traits\ClassWatchdog; |
||
13 | use \PHPDaemon\Traits\StaticObjectWatchdog; |
||
14 | |||
15 | /** |
||
16 | * State: waiting |
||
17 | */ |
||
18 | const STATE_WAITING = 1; |
||
19 | |||
20 | /** |
||
21 | * State: running |
||
22 | */ |
||
23 | const STATE_RUNNING = 2; |
||
24 | |||
25 | /** |
||
26 | * State: done |
||
27 | */ |
||
28 | const STATE_DONE = 3; |
||
29 | |||
30 | /** |
||
31 | * @var array Listeners [callable, ...] |
||
32 | */ |
||
33 | public $listeners = []; |
||
34 | |||
35 | /** |
||
36 | * @var array Hash of results [jobname -> result, ...] |
||
37 | */ |
||
38 | public $results = []; |
||
39 | |||
40 | /** |
||
41 | * @var integer Current state |
||
42 | */ |
||
43 | public $state; |
||
44 | |||
45 | /** |
||
46 | * @var array Hash of jobs [jobname -> callback, ...] |
||
47 | */ |
||
48 | public $jobs = []; |
||
49 | |||
50 | /** |
||
51 | * @var integer Number of results |
||
52 | */ |
||
53 | public $resultsNum = 0; |
||
54 | |||
55 | /** |
||
56 | * @var integer Number of jobs |
||
57 | */ |
||
58 | public $jobsNum = 0; |
||
59 | |||
60 | protected $keep = false; |
||
61 | |||
62 | protected $more = null; |
||
63 | |||
64 | protected $moreFirstFlag; |
||
65 | |||
66 | protected $maxConcurrency = -1; |
||
67 | |||
68 | protected $backlog; |
||
69 | |||
70 | public $vars = []; |
||
71 | |||
72 | /** |
||
73 | * Constructor |
||
74 | * @param callable $cb Listener |
||
|
|||
75 | */ |
||
76 | public function __construct($cb = null) { |
||
82 | |||
83 | /** |
||
84 | * Handler of isset($job[$name]) |
||
85 | * @param string $j Job name |
||
86 | * @return boolean |
||
87 | */ |
||
88 | public function offsetExists($j) { |
||
91 | |||
92 | /** |
||
93 | * Handler of $job[$name] |
||
94 | * @param string $j Job name |
||
95 | * @return mixed |
||
96 | */ |
||
97 | public function offsetGet($j) { |
||
100 | |||
101 | /** |
||
102 | * Handler of $job[$name] = $value |
||
103 | * @param string $j Job name |
||
104 | * @param mixed $v Job result |
||
105 | * @return void |
||
106 | */ |
||
107 | public function offsetSet($j, $v) { |
||
110 | |||
111 | /** |
||
112 | * Handler of unset($job[$name]) |
||
113 | * @param string $j Job name |
||
114 | * @return void |
||
115 | */ |
||
116 | public function offsetUnset($j) { |
||
119 | |||
120 | /** |
||
121 | * Returns associative array of results |
||
122 | * @return array |
||
123 | */ |
||
124 | public function getResults() { |
||
127 | /** |
||
128 | * Keep |
||
129 | * @param boolean $keep Keep? |
||
130 | * @return void |
||
131 | */ |
||
132 | public function keep($keep = true) { |
||
135 | |||
136 | /** |
||
137 | * Has completed? |
||
138 | * @return boolean |
||
139 | */ |
||
140 | public function hasCompleted() { |
||
143 | |||
144 | /** |
||
145 | * Sets a limit of simultaneously executing tasks |
||
146 | * @param integer $n Natural number or -1 (no limit) |
||
147 | * @return this |
||
148 | */ |
||
149 | public function maxConcurrency($n = -1) { |
||
153 | |||
154 | /** |
||
155 | * Set result |
||
156 | * @param string $jobname Job name |
||
157 | * @param mixed $result Result |
||
158 | * @return boolean |
||
159 | */ |
||
160 | public function setResult($jobname, $result = null) { |
||
169 | |||
170 | /** |
||
171 | * Get result |
||
172 | * @param string $jobname Job name |
||
173 | * @return mixed Result or null |
||
174 | */ |
||
175 | public function getResult($jobname) { |
||
178 | |||
179 | /** |
||
180 | * Checks if all jobs are ready |
||
181 | * @return void |
||
182 | */ |
||
183 | protected function checkIfAllReady() { |
||
196 | |||
197 | /** |
||
198 | * Called automatically. Checks whether if the queue is full. If not, tries to pull more jobs from backlog and 'more' |
||
199 | * @return void |
||
200 | */ |
||
201 | public function checkQueue() { |
||
215 | |||
216 | /** |
||
217 | * Sets a callback which is going to be fired always when we have a room for more jobs |
||
218 | * @param callable $cb Callback |
||
219 | * @return this |
||
220 | */ |
||
221 | public function more($cb = null) { |
||
251 | |||
252 | /** |
||
253 | * Returns whether or not the queue is full (maxConcurrency option exceed) |
||
254 | * @return boolean |
||
255 | */ |
||
256 | public function isQueueFull() { |
||
259 | |||
260 | /** |
||
261 | * Adds job |
||
262 | * @param string $name Job name |
||
263 | * @param callable $cb Callback |
||
264 | * @return boolean Success |
||
265 | */ |
||
266 | public function addJob($name, $cb) { |
||
286 | |||
287 | /** |
||
288 | * Clean up |
||
289 | * @return void |
||
290 | */ |
||
291 | public function cleanup() { |
||
297 | |||
298 | /** |
||
299 | * Adds listener |
||
300 | * @param callable $cb Callback |
||
301 | * @return void |
||
302 | */ |
||
303 | public function addListener($cb) { |
||
310 | |||
311 | /** |
||
312 | * Runs the job |
||
313 | * @return void |
||
314 | */ |
||
315 | public function execute() { |
||
325 | |||
326 | /** |
||
327 | * Adds new job or calls execute() method |
||
328 | * @param mixed $name |
||
329 | * @param callable $cb |
||
330 | * @return void |
||
331 | */ |
||
332 | public function __invoke($name = null, $cb = null) { |
||
339 | } |
||
340 |
This check looks for
@param
annotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.