Conditions | 54 |
Paths | > 20000 |
Total Lines | 344 |
Lines | 27 |
Ratio | 7.85 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
27 | public function ao_ccss_queue_control() { |
||
28 | // The queue execution backend. |
||
29 | global $ao_ccss_key; |
||
30 | if ( ! isset( $ao_ccss_key ) || empty( $ao_ccss_key ) ) { |
||
31 | // no key set, not processing the queue! |
||
32 | autoptimizeCriticalCSSCore::ao_ccss_log( 'No key set, so not processing queue.', 3 ); |
||
33 | return; |
||
34 | } |
||
35 | |||
36 | /** |
||
37 | * Provide a debug facility for the queue |
||
38 | * This debug facility provides a way to easily force some queue behaviors useful for development and testing. |
||
39 | * To enable this feature, create the file AO_CCSS_DIR . 'queue.json' with a JSON object like the one bellow: |
||
40 | * |
||
41 | * {"enable":bool,"htcode":int,"status":0|"str","resultStatus ":0|"str","validationStatus":0|"str"} |
||
42 | * |
||
43 | * Where values are: |
||
44 | * - enable : 0 or 1, enable or disable this debug facility straight from the file |
||
45 | * - htcode : 0 or any HTTP reponse code (e.g. 2xx, 4xx, 5xx) to force API responses |
||
46 | * - status : 0 or a valid value for 'status' (see 'Generating critical css - Job Status Types' in spec docs) |
||
47 | * - resultStatus : 0 or a valid value for 'resultStatus' (see 'Appendix - Result status types' in the spec docs) |
||
48 | * - validationStatus: 0 or a valid value for 'validationStatus' (see 'Appendix - Validation status types' in the spec docs) |
||
49 | * |
||
50 | * When properly set, queue will always finish a job with the declared settings above regardless of the real API responses. |
||
51 | */ |
||
52 | $queue_debug = false; |
||
53 | if ( file_exists( AO_CCSS_DEBUG ) ) { |
||
54 | $qdobj_raw = file_get_contents( AO_CCSS_DEBUG ); |
||
55 | $qdobj = json_decode( $qdobj_raw, true ); |
||
56 | if ( $qdobj ) { |
||
57 | if ( 1 === $qdobj['enable'] ) { |
||
58 | $queue_debug = true; |
||
59 | autoptimizeCriticalCSSCore::ao_ccss_log( 'Queue operating in debug mode with the following settings: <' . $qdobj_raw . '>', 3 ); |
||
60 | } |
||
61 | } |
||
62 | } |
||
63 | |||
64 | // Set some default values for $qdobj to avoid function call warnings. |
||
65 | if ( ! $queue_debug ) { |
||
66 | $qdobj['htcode'] = false; |
||
67 | } |
||
68 | |||
69 | // Check if queue is already running. |
||
70 | $queue_lock = false; |
||
71 | if ( file_exists( AO_CCSS_LOCK ) ) { |
||
72 | $queue_lock = true; |
||
73 | } |
||
74 | |||
75 | // Proceed with the queue if it's not already running. |
||
76 | if ( ! $queue_lock ) { |
||
77 | |||
78 | // Log queue start and create the lock file. |
||
79 | autoptimizeCriticalCSSCore::ao_ccss_log( 'Queue control started', 3 ); |
||
80 | if ( touch( AO_CCSS_LOCK ) ) { |
||
81 | autoptimizeCriticalCSSCore::ao_ccss_log( 'Queue control locked', 3 ); |
||
82 | } |
||
83 | |||
84 | // Attach required variables. |
||
85 | global $ao_ccss_queue; |
||
86 | global $ao_ccss_rtimelimit; |
||
87 | |||
88 | // Initialize counters. |
||
89 | if ( $ao_ccss_rtimelimit == 0 ) { |
||
90 | // no time limit set, let's go with 1000 seconds. |
||
91 | $ao_ccss_rtimelimit = 1000; |
||
92 | } |
||
93 | $mt = time() + $ao_ccss_rtimelimit; // maxtime queue processing can run. |
||
94 | $jc = 1; // job count number. |
||
95 | $jr = 1; // jobs requests number. |
||
96 | $jt = count( $ao_ccss_queue ); // number of jobs in queue. |
||
97 | |||
98 | // Sort queue by ascending job status (e.g. ERROR, JOB_ONGOING, JOB_QUEUED, NEW...). |
||
99 | array_multisort( array_column( $ao_ccss_queue, 'jqstat' ), $ao_ccss_queue ); // @codingStandardsIgnoreLine |
||
100 | |||
101 | // Iterates over the entire queue. |
||
102 | foreach ( $ao_ccss_queue as $path => $jprops ) { |
||
103 | // Prepare flags and target rule. |
||
104 | $update = false; |
||
105 | $deljob = false; |
||
106 | $rule_update = false; |
||
107 | $oldccssfile = false; |
||
108 | $trule = explode( '|', $jprops['rtarget'] ); |
||
109 | |||
110 | // Log job count. |
||
111 | autoptimizeCriticalCSSCore::ao_ccss_log( 'Processing job ' . $jc . ' of ' . $jt . ' with id <' . $jprops['ljid'] . '> and status <' . $jprops['jqstat'] . '>', 3 ); |
||
112 | |||
113 | // Process NEW jobs. |
||
114 | if ( 'NEW' == $jprops['jqstat'] ) { |
||
115 | |||
116 | // Log the new job. |
||
117 | autoptimizeCriticalCSSCore::ao_ccss_log( 'Found NEW job with local ID <' . $jprops['ljid'] . '>, starting its queue processing', 3 ); |
||
118 | |||
119 | // Compare job and rule hashes (if any). |
||
120 | $hash = $this->ao_ccss_diff_hashes( $jprops['ljid'], $jprops['hash'], $jprops['hashes'], $jprops['rtarget'] ); |
||
121 | |||
122 | // If job hash is new or different of a previous one. |
||
123 | if ( $hash ) { |
||
124 | if ( $jr > 2 ) { |
||
125 | // we already posted 2 jobs to criticalcss.com, don't post more this run |
||
126 | // but we can keep on processing the queue to keep it tidy. |
||
127 | autoptimizeCriticalCSSCore::ao_ccss_log( 'Holding off on generating request for job with local ID <' . $jprops['ljid'] . '>, maximum number of POSTS reached.', 3 ); |
||
128 | continue; |
||
129 | } |
||
130 | |||
131 | // Set job hash. |
||
132 | $jprops['hash'] = $hash; |
||
133 | |||
134 | // Dispatch the job generate request and increment request count. |
||
135 | $apireq = $this->ao_ccss_api_generate( $path, $queue_debug, $qdobj['htcode'] ); |
||
136 | $jr++; |
||
137 | |||
138 | // NOTE: All the following conditions maps to the ones in admin_settings_queue.js.php. |
||
139 | if ( 'JOB_QUEUED' == $apireq['job']['status'] || 'JOB_ONGOING' == $apireq['job']['status'] ) { |
||
140 | // SUCCESS: request has a valid result. |
||
141 | // Update job properties. |
||
142 | $jprops['jid'] = $apireq['job']['id']; |
||
143 | $jprops['jqstat'] = $apireq['job']['status']; |
||
144 | autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $jprops['ljid'] . '> generate request successful, remote id <' . $jprops['jid'] . '>, status now is <' . $jprops['jqstat'] . '>', 3 ); |
||
145 | View Code Duplication | } elseif ( 'STATUS_JOB_BAD' == $apireq['job']['status'] ) { |
|
146 | // ERROR: concurrent requests |
||
147 | // Update job properties. |
||
148 | $jprops['jid'] = $apireq['job']['id']; |
||
149 | $jprops['jqstat'] = $apireq['job']['status']; |
||
150 | if ( $apireq['job']['error'] ) { |
||
151 | $jprops['jrstat'] = $apireq['job']['error']; |
||
152 | } else { |
||
153 | $jprops['jrstat'] = 'Baby did a bad bad thing'; |
||
154 | } |
||
155 | $jprops['jvstat'] = 'NONE'; |
||
156 | $jprops['jftime'] = microtime( true ); |
||
157 | autoptimizeCriticalCSSCore::ao_ccss_log( 'Concurrent requests when processing job id <' . $jprops['ljid'] . '>, job status is now <' . $jprops['jqstat'] . '>', 3 ); |
||
158 | } elseif ( 'INVALID_JWT_TOKEN' == $apireq['errorCode'] ) { |
||
159 | // ERROR: key validation |
||
160 | // Update job properties. |
||
161 | $jprops['jqstat'] = $apireq['errorCode']; |
||
162 | $jprops['jrstat'] = $apireq['error']; |
||
163 | $jprops['jvstat'] = 'NONE'; |
||
164 | $jprops['jftime'] = microtime( true ); |
||
165 | autoptimizeCriticalCSSCore::ao_ccss_log( 'API key validation error when processing job id <' . $jprops['ljid'] . '>, job status is now <' . $jprops['jqstat'] . '>', 3 ); |
||
166 | } elseif ( empty( $apireq ) ) { |
||
167 | // ERROR: no response |
||
168 | // Update job properties. |
||
169 | $jprops['jqstat'] = 'NO_RESPONSE'; |
||
170 | $jprops['jrstat'] = 'NONE'; |
||
171 | $jprops['jvstat'] = 'NONE'; |
||
172 | $jprops['jftime'] = microtime( true ); |
||
173 | autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $jprops['ljid'] . '> request has no response, status now is <' . $jprops['jqstat'] . '>', 3 ); |
||
174 | } else { |
||
175 | // UNKNOWN: unhandled generate exception |
||
176 | // Update job properties. |
||
177 | $jprops['jqstat'] = 'JOB_UNKNOWN'; |
||
178 | $jprops['jrstat'] = 'NONE'; |
||
179 | $jprops['jvstat'] = 'NONE'; |
||
180 | $jprops['jftime'] = microtime( true ); |
||
181 | autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $jprops['ljid'] . '> generate request has an UNKNOWN condition, status now is <' . $jprops['jqstat'] . '>, check log messages above for more information', 2 ); |
||
182 | autoptimizeCriticalCSSCore::ao_ccss_log( 'Job response was: ' . json_encode( $apireq ), 3 ); |
||
183 | } |
||
184 | } else { |
||
185 | // SUCCESS: Job hash is equal to a previous one, so it's done |
||
186 | // Update job status and finish time. |
||
187 | $jprops['jqstat'] = 'JOB_DONE'; |
||
188 | $jprops['jftime'] = microtime( true ); |
||
189 | autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $jprops['ljid'] . '> requires no further processing, status now is <' . $jprops['jqstat'] . '>', 3 ); |
||
190 | } |
||
191 | |||
192 | // Set queue update flag. |
||
193 | $update = true; |
||
194 | |||
195 | } elseif ( 'JOB_QUEUED' == $jprops['jqstat'] || 'JOB_ONGOING' == $jprops['jqstat'] ) { |
||
196 | // Process QUEUED and ONGOING jobs |
||
197 | // Log the pending job. |
||
198 | autoptimizeCriticalCSSCore::ao_ccss_log( 'Found PENDING job with local ID <' . $jprops['ljid'] . '>, continuing its queue processing', 3 ); |
||
199 | |||
200 | // Dispatch the job result request and increment request count. |
||
201 | $apireq = $this->ao_ccss_api_results( $jprops['jid'], $queue_debug, $qdobj['htcode'] ); |
||
202 | |||
203 | // NOTE: All the following condigitons maps to the ones in admin_settings_queue.js.php |
||
204 | // Replace API response values if queue debugging is enabled and some value is set. |
||
205 | if ( $queue_debug ) { |
||
206 | if ( $qdobj['status'] ) { |
||
207 | $apireq['status'] = $qdobj['status']; |
||
208 | } |
||
209 | if ( $qdobj['resultStatus'] ) { |
||
210 | $apireq['resultStatus'] = $qdobj['resultStatus']; |
||
211 | } |
||
212 | if ( $qdobj['validationStatus'] ) { |
||
213 | $apireq['validationStatus'] = $qdobj['validationStatus']; |
||
214 | } |
||
215 | } |
||
216 | |||
217 | if ( 'JOB_QUEUED' == $apireq['status'] || 'JOB_ONGOING' == $apireq['status'] ) { |
||
218 | // SUCCESS: request has a valid result |
||
219 | // Process a PENDING job |
||
220 | // Update job properties. |
||
221 | $jprops['jqstat'] = $apireq['status']; |
||
222 | autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $jprops['ljid'] . '> result request successful, remote id <' . $jprops['jid'] . '>, status <' . $jprops['jqstat'] . '> unchanged', 3 ); |
||
223 | } elseif ( 'JOB_DONE' == $apireq['status'] ) { |
||
224 | // Process a DONE job |
||
225 | // New resultStatus from ccss.com "HTML_404", consider as "GOOD" for now. |
||
226 | if ( 'HTML_404' == $apireq['resultStatus'] ) { |
||
227 | $apireq['resultStatus'] = 'GOOD'; |
||
228 | } |
||
229 | |||
230 | if ( 'GOOD' == $apireq['resultStatus'] && ( 'GOOD' == $apireq['validationStatus'] || 'WARN' == $apireq['validationStatus'] ) ) { |
||
231 | // SUCCESS: GOOD job with GOOD or WARN validation |
||
232 | // Update job properties. |
||
233 | $jprops['file'] = $this->ao_ccss_save_file( $apireq['css'], $trule, false ); |
||
234 | $jprops['jqstat'] = $apireq['status']; |
||
235 | $jprops['jrstat'] = $apireq['resultStatus']; |
||
236 | $jprops['jvstat'] = $apireq['validationStatus']; |
||
237 | $jprops['jftime'] = microtime( true ); |
||
238 | $rule_update = true; |
||
239 | autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $jprops['ljid'] . '> result request successful, remote id <' . $jprops['jid'] . '>, status <' . $jprops['jqstat'] . '>, file saved <' . $jprops['file'] . '>', 3 ); |
||
240 | } elseif ( 'GOOD' == $apireq['resultStatus'] && ( 'BAD' == $apireq['validationStatus'] || 'SCREENSHOT_WARN_BLANK' == $apireq['validationStatus'] ) ) { |
||
241 | // SUCCESS: GOOD job with BAD or SCREENSHOT_WARN_BLANK validation |
||
242 | // Update job properties. |
||
243 | $jprops['jqstat'] = $apireq['status']; |
||
244 | $jprops['jrstat'] = $apireq['resultStatus']; |
||
245 | $jprops['jvstat'] = $apireq['validationStatus']; |
||
246 | $jprops['jftime'] = microtime( true ); |
||
247 | if ( apply_filters( 'autoptimize_filter_ccss_save_review_rules', true ) ) { |
||
248 | $jprops['file'] = $this->ao_ccss_save_file( $apireq['css'], $trule, true ); |
||
249 | $rule_update = true; |
||
250 | autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $jprops['ljid'] . '> result request successful, remote id <' . $jprops['jid'] . '>, status <' . $jprops['jqstat'] . ', file saved <' . $jprops['file'] . '> but requires REVIEW', 3 ); |
||
251 | } else { |
||
252 | autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $jprops['ljid'] . '> result request successful, remote id <' . $jprops['jid'] . '>, status <' . $jprops['jqstat'] . ', file not saved because it required REVIEW.', 3 ); |
||
253 | } |
||
254 | } elseif ( 'GOOD' != $apireq['resultStatus'] && ( 'GOOD' != $apireq['validationStatus'] || 'WARN' != $apireq['validationStatus'] || 'BAD' != $apireq['validationStatus'] || 'SCREENSHOT_WARN_BLANK' != $apireq['validationStatus'] ) ) { |
||
255 | // ERROR: no GOOD, WARN or BAD results |
||
256 | // Update job properties. |
||
257 | $jprops['jqstat'] = $apireq['status']; |
||
258 | $jprops['jrstat'] = $apireq['resultStatus']; |
||
259 | $jprops['jvstat'] = $apireq['validationStatus']; |
||
260 | $jprops['jftime'] = microtime( true ); |
||
261 | autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $jprops['ljid'] . '> result request successful but job FAILED, status now is <' . $jprops['jqstat'] . '>', 3 ); |
||
262 | $apireq['css'] = '/* critical css removed for DEBUG logging purposes */'; |
||
263 | autoptimizeCriticalCSSCore::ao_ccss_log( 'Job response was: ' . json_encode( $apireq ), 3 ); |
||
264 | } else { |
||
265 | // UNKNOWN: unhandled JOB_DONE exception |
||
266 | // Update job properties. |
||
267 | $jprops['jqstat'] = 'JOB_UNKNOWN'; |
||
268 | $jprops['jrstat'] = $apireq['resultStatus']; |
||
269 | $jprops['jvstat'] = $apireq['validationStatus']; |
||
270 | $jprops['jftime'] = microtime( true ); |
||
271 | autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $jprops['ljid'] . '> result request successful but job is UNKNOWN, status now is <' . $jprops['jqstat'] . '>', 2 ); |
||
272 | $apireq['css'] = '/* critical css removed for DEBUG logging purposes */'; |
||
273 | autoptimizeCriticalCSSCore::ao_ccss_log( 'Job response was: ' . json_encode( $apireq ), 3 ); |
||
274 | } |
||
275 | View Code Duplication | } elseif ( 'JOB_FAILED' == $apireq['job']['status'] || 'STATUS_JOB_BAD' == $apireq['job']['status'] ) { |
|
276 | // ERROR: failed job |
||
277 | // Update job properties. |
||
278 | $jprops['jqstat'] = $apireq['job']['status']; |
||
279 | if ( $apireq['job']['error'] ) { |
||
280 | $jprops['jrstat'] = $apireq['job']['error']; |
||
281 | } else { |
||
282 | $jprops['jrstat'] = 'Baby did a bad bad thing'; |
||
283 | } |
||
284 | $jprops['jvstat'] = 'NONE'; |
||
285 | $jprops['jftime'] = microtime( true ); |
||
286 | autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $jprops['ljid'] . '> result request successful but job FAILED, status now is <' . $jprops['jqstat'] . '>', 3 ); |
||
287 | } elseif ( 'This css no longer exists. Please re-generate it.' == $apireq['error'] ) { |
||
288 | // ERROR: CSS doesn't exist |
||
289 | // Update job properties. |
||
290 | $jprops['jqstat'] = 'NO_CSS'; |
||
291 | $jprops['jrstat'] = $apireq['error']; |
||
292 | $jprops['jvstat'] = 'NONE'; |
||
293 | $jprops['jftime'] = microtime( true ); |
||
294 | autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $jprops['ljid'] . '> result request successful but job FAILED, status now is <' . $jprops['jqstat'] . '>', 3 ); |
||
295 | } elseif ( empty( $apireq ) ) { |
||
296 | // ERROR: no response |
||
297 | // Update job properties. |
||
298 | $jprops['jqstat'] = 'NO_RESPONSE'; |
||
299 | $jprops['jrstat'] = 'NONE'; |
||
300 | $jprops['jvstat'] = 'NONE'; |
||
301 | $jprops['jftime'] = microtime( true ); |
||
302 | autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $jprops['ljid'] . '> request has no response, status now is <' . $jprops['jqstat'] . '>', 3 ); |
||
303 | } else { |
||
304 | // UNKNOWN: unhandled results exception |
||
305 | // Update job properties. |
||
306 | $jprops['jqstat'] = 'JOB_UNKNOWN'; |
||
307 | $jprops['jrstat'] = 'NONE'; |
||
308 | $jprops['jvstat'] = 'NONE'; |
||
309 | $jprops['jftime'] = microtime( true ); |
||
310 | autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $jprops['ljid'] . '> result request has an UNKNOWN condition, status now is <' . $jprops['jqstat'] . '>, check log messages above for more information', 2 ); |
||
311 | } |
||
312 | |||
313 | // Set queue update flag. |
||
314 | $update = true; |
||
315 | } |
||
316 | |||
317 | // Mark DONE jobs for removal. |
||
318 | if ( 'JOB_DONE' == $jprops['jqstat'] ) { |
||
319 | $update = true; |
||
320 | $deljob = true; |
||
321 | } |
||
322 | |||
323 | // Persist updated queue object. |
||
324 | if ( $update ) { |
||
325 | if ( ! $deljob ) { |
||
326 | // Update properties of a NEW or PENDING job... |
||
327 | $ao_ccss_queue[ $path ] = $jprops; |
||
328 | } else { |
||
329 | // ...or remove the DONE job. |
||
330 | unset( $ao_ccss_queue[ $path ] ); |
||
331 | autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $jprops['ljid'] . '> is DONE and was removed from the queue', 3 ); |
||
332 | } |
||
333 | |||
334 | // Update queue object. |
||
335 | $ao_ccss_queue_raw = json_encode( $ao_ccss_queue ); |
||
336 | update_option( 'autoptimize_ccss_queue', $ao_ccss_queue_raw, false ); |
||
337 | autoptimizeCriticalCSSCore::ao_ccss_log( 'Queue updated by job id <' . $jprops['ljid'] . '>', 3 ); |
||
338 | |||
339 | // Update target rule. |
||
340 | if ( $rule_update ) { |
||
341 | $this->ao_ccss_rule_update( $jprops['ljid'], $jprops['rtarget'], $jprops['file'], $jprops['hash'] ); |
||
342 | autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $jprops['ljid'] . '> updated the target rule <' . $jprops['rtarget'] . '>', 3 ); |
||
343 | } |
||
344 | } else { |
||
345 | // Or log no queue action. |
||
346 | autoptimizeCriticalCSSCore::ao_ccss_log( 'Nothing to do on this job', 3 ); |
||
347 | } |
||
348 | |||
349 | // Break the loop if request time limit is (almost exceeded). |
||
350 | if ( time() > $mt ) { |
||
351 | autoptimizeCriticalCSSCore::ao_ccss_log( 'The time limit of ' . $ao_ccss_rtimelimit . ' seconds was exceeded, queue control must finish now', 3 ); |
||
352 | break; |
||
353 | } |
||
354 | |||
355 | // Increment job counter. |
||
356 | $jc++; |
||
357 | } |
||
358 | |||
359 | // Remove the lock file and log the queue end. |
||
360 | if ( file_exists( AO_CCSS_LOCK ) ) { |
||
361 | unlink( AO_CCSS_LOCK ); |
||
362 | autoptimizeCriticalCSSCore::ao_ccss_log( 'Queue control unlocked', 3 ); |
||
363 | } |
||
364 | autoptimizeCriticalCSSCore::ao_ccss_log( 'Queue control finished', 3 ); |
||
365 | |||
366 | // Log that queue is locked. |
||
367 | } else { |
||
368 | autoptimizeCriticalCSSCore::ao_ccss_log( 'Queue is already running, skipping the attempt to run it again', 3 ); |
||
369 | } |
||
370 | } |
||
371 | |||
841 |
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()
can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.