Conditions | 53 |
Paths | > 20000 |
Total Lines | 336 |
Lines | 18 |
Ratio | 5.36 % |
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_rlimit; |
||
87 | |||
88 | // Initialize job counters. |
||
89 | $jc = 1; |
||
90 | $jr = 1; |
||
91 | $jt = count( $ao_ccss_queue ); |
||
92 | |||
93 | // Sort queue by ascending job status (e.g. ERROR, JOB_ONGOING, JOB_QUEUED, NEW...). |
||
94 | array_multisort( array_column( $ao_ccss_queue, 'jqstat' ), $ao_ccss_queue ); // @codingStandardsIgnoreLine |
||
95 | |||
96 | // Iterates over the entire queue. |
||
97 | foreach ( $ao_ccss_queue as $path => $jprops ) { |
||
98 | // Prepare flags and target rule. |
||
99 | $update = false; |
||
100 | $deljob = false; |
||
101 | $rule_update = false; |
||
102 | $oldccssfile = false; |
||
103 | $trule = explode( '|', $jprops['rtarget'] ); |
||
104 | |||
105 | // Log job count. |
||
106 | autoptimizeCriticalCSSCore::ao_ccss_log( 'Processing job ' . $jc . ' of ' . $jt . ' with id <' . $jprops['ljid'] . '> and status <' . $jprops['jqstat'] . '>', 3 ); |
||
107 | |||
108 | // Process NEW jobs. |
||
109 | if ( 'NEW' == $jprops['jqstat'] ) { |
||
110 | |||
111 | // Log the new job. |
||
112 | autoptimizeCriticalCSSCore::ao_ccss_log( 'Found NEW job with local ID <' . $jprops['ljid'] . '>, starting its queue processing', 3 ); |
||
113 | |||
114 | // Compare job and rule hashes (if any). |
||
115 | $hash = $this->ao_ccss_diff_hashes( $jprops['ljid'], $jprops['hash'], $jprops['hashes'], $jprops['rtarget'] ); |
||
116 | |||
117 | // If job hash is new or different of a previous one. |
||
118 | if ( $hash ) { |
||
119 | // Set job hash. |
||
120 | $jprops['hash'] = $hash; |
||
121 | |||
122 | // If this is not the first job, wait 15 seconds before process next job due criticalcss.com API limits. |
||
123 | if ( $jr > 1 ) { |
||
124 | autoptimizeCriticalCSSCore::ao_ccss_log( 'Waiting 15 seconds due to criticalcss.com API limits', 3 ); |
||
125 | sleep( 15 ); |
||
126 | } |
||
127 | |||
128 | // Dispatch the job generate request and increment request count. |
||
129 | $apireq = $this->ao_ccss_api_generate( $path, $queue_debug, $qdobj['htcode'] ); |
||
130 | $jr++; |
||
131 | |||
132 | // NOTE: All the following conditions maps to the ones in admin_settings_queue.js.php. |
||
133 | if ( 'JOB_QUEUED' == $apireq['job']['status'] || 'JOB_ONGOING' == $apireq['job']['status'] ) { |
||
134 | // SUCCESS: request has a valid result. |
||
135 | // Update job properties. |
||
136 | $jprops['jid'] = $apireq['job']['id']; |
||
137 | $jprops['jqstat'] = $apireq['job']['status']; |
||
138 | autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $jprops['ljid'] . '> generate request successful, remote id <' . $jprops['jid'] . '>, status now is <' . $jprops['jqstat'] . '>', 3 ); |
||
139 | } elseif ( 'STATUS_JOB_BAD' == $apireq['job']['status'] ) { |
||
140 | // ERROR: concurrent requests |
||
141 | // Update job properties. |
||
142 | $jprops['jid'] = $apireq['job']['id']; |
||
143 | $jprops['jqstat'] = $apireq['job']['status']; |
||
144 | $jprops['jrstat'] = $apireq['error']; |
||
145 | $jprops['jvstat'] = 'NONE'; |
||
146 | $jprops['jftime'] = microtime( true ); |
||
147 | autoptimizeCriticalCSSCore::ao_ccss_log( 'Concurrent requests when processing job id <' . $jprops['ljid'] . '>, job status is now <' . $jprops['jqstat'] . '>', 3 ); |
||
148 | View Code Duplication | } elseif ( 'INVALID_JWT_TOKEN' == $apireq['errorCode'] ) { |
|
149 | // ERROR: key validation |
||
150 | // Update job properties. |
||
151 | $jprops['jqstat'] = $apireq['errorCode']; |
||
152 | $jprops['jrstat'] = $apireq['error']; |
||
153 | $jprops['jvstat'] = 'NONE'; |
||
154 | $jprops['jftime'] = microtime( true ); |
||
155 | autoptimizeCriticalCSSCore::ao_ccss_log( 'API key validation error when processing job id <' . $jprops['ljid'] . '>, job status is now <' . $jprops['jqstat'] . '>', 3 ); |
||
156 | } elseif ( empty( $apireq ) ) { |
||
157 | // ERROR: no response |
||
158 | // Update job properties. |
||
159 | $jprops['jqstat'] = 'NO_RESPONSE'; |
||
160 | $jprops['jrstat'] = 'NONE'; |
||
161 | $jprops['jvstat'] = 'NONE'; |
||
162 | $jprops['jftime'] = microtime( true ); |
||
163 | autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $jprops['ljid'] . '> request has no response, status now is <' . $jprops['jqstat'] . '>', 3 ); |
||
164 | } else { |
||
165 | // UNKNOWN: unhandled generate exception |
||
166 | // Update job properties. |
||
167 | $jprops['jqstat'] = 'JOB_UNKNOWN'; |
||
168 | $jprops['jrstat'] = 'NONE'; |
||
169 | $jprops['jvstat'] = 'NONE'; |
||
170 | $jprops['jftime'] = microtime( true ); |
||
171 | 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 ); |
||
172 | autoptimizeCriticalCSSCore::ao_ccss_log( 'Job response was: ' . json_encode( $apireq ), 3 ); |
||
173 | } |
||
174 | } else { |
||
175 | // SUCCESS: Job hash is equal to a previous one, so it's done |
||
176 | // Update job status and finish time. |
||
177 | $jprops['jqstat'] = 'JOB_DONE'; |
||
178 | $jprops['jftime'] = microtime( true ); |
||
179 | autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $jprops['ljid'] . '> requires no further processing, status now is <' . $jprops['jqstat'] . '>', 3 ); |
||
180 | } |
||
181 | |||
182 | // Set queue update flag. |
||
183 | $update = true; |
||
184 | |||
185 | } elseif ( 'JOB_QUEUED' == $jprops['jqstat'] || 'JOB_ONGOING' == $jprops['jqstat'] ) { |
||
186 | // Process QUEUED and ONGOING jobs |
||
187 | // Log the pending job. |
||
188 | autoptimizeCriticalCSSCore::ao_ccss_log( 'Found PENDING job with local ID <' . $jprops['ljid'] . '>, continuing its queue processing', 3 ); |
||
189 | |||
190 | // If this is not the first job, wait 15 seconds before process next job due criticalcss.com API limits. |
||
191 | if ( $jr > 1 ) { |
||
192 | autoptimizeCriticalCSSCore::ao_ccss_log( 'Waiting 15 seconds due to criticalcss.com API limits', 3 ); |
||
193 | sleep( 15 ); |
||
194 | } |
||
195 | |||
196 | // Dispatch the job result request and increment request count. |
||
197 | $apireq = $this->ao_ccss_api_results( $jprops['jid'], $queue_debug, $qdobj['htcode'] ); |
||
198 | $jr++; |
||
199 | |||
200 | // NOTE: All the following condigitons maps to the ones in admin_settings_queue.js.php |
||
201 | // Replace API response values if queue debugging is enabled and some value is set. |
||
202 | if ( $queue_debug ) { |
||
203 | if ( $qdobj['status'] ) { |
||
204 | $apireq['status'] = $qdobj['status']; |
||
205 | } |
||
206 | if ( $qdobj['resultStatus'] ) { |
||
207 | $apireq['resultStatus'] = $qdobj['resultStatus']; |
||
208 | } |
||
209 | if ( $qdobj['validationStatus'] ) { |
||
210 | $apireq['validationStatus'] = $qdobj['validationStatus']; |
||
211 | } |
||
212 | } |
||
213 | |||
214 | if ( 'JOB_QUEUED' == $apireq['status'] || 'JOB_ONGOING' == $apireq['status'] ) { |
||
215 | // SUCCESS: request has a valid result |
||
216 | // Process a PENDING job |
||
217 | // Update job properties. |
||
218 | $jprops['jqstat'] = $apireq['status']; |
||
219 | autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $jprops['ljid'] . '> result request successful, remote id <' . $jprops['jid'] . '>, status <' . $jprops['jqstat'] . '> unchanged', 3 ); |
||
220 | } elseif ( 'JOB_DONE' == $apireq['status'] ) { |
||
221 | // Process a DONE job |
||
222 | // New resultStatus from ccss.com "HTML_404", consider as "GOOD" for now. |
||
223 | if ( 'HTML_404' == $apireq['resultStatus'] ) { |
||
224 | $apireq['resultStatus'] = 'GOOD'; |
||
225 | } |
||
226 | |||
227 | if ( 'GOOD' == $apireq['resultStatus'] && 'GOOD' == $apireq['validationStatus'] ) { |
||
228 | // SUCCESS: GOOD job with GOOD validation |
||
229 | // Update job properties. |
||
230 | $jprops['file'] = $this->ao_ccss_save_file( $apireq['css'], $trule, false ); |
||
231 | $jprops['jqstat'] = $apireq['status']; |
||
232 | $jprops['jrstat'] = $apireq['resultStatus']; |
||
233 | $jprops['jvstat'] = $apireq['validationStatus']; |
||
234 | $jprops['jftime'] = microtime( true ); |
||
235 | $rule_update = true; |
||
236 | autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $jprops['ljid'] . '> result request successful, remote id <' . $jprops['jid'] . '>, status <' . $jprops['jqstat'] . '>, file saved <' . $jprops['file'] . '>', 3 ); |
||
237 | } elseif ( 'GOOD' == $apireq['resultStatus'] && ( 'WARN' == $apireq['validationStatus'] || 'BAD' == $apireq['validationStatus'] || 'SCREENSHOT_WARN_BLANK' == $apireq['validationStatus'] ) ) { |
||
238 | // SUCCESS: GOOD job with WARN or BAD validation |
||
239 | // Update job properties. |
||
240 | $jprops['file'] = $this->ao_ccss_save_file( $apireq['css'], $trule, true ); |
||
241 | $jprops['jqstat'] = $apireq['status']; |
||
242 | $jprops['jrstat'] = $apireq['resultStatus']; |
||
243 | $jprops['jvstat'] = $apireq['validationStatus']; |
||
244 | $jprops['jftime'] = microtime( true ); |
||
245 | $rule_update = true; |
||
246 | 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 ); |
||
247 | } elseif ( 'GOOD' != $apireq['resultStatus'] && ( 'GOOD' != $apireq['validationStatus'] || 'WARN' != $apireq['validationStatus'] || 'BAD' != $apireq['validationStatus'] || 'SCREENSHOT_WARN_BLANK' != $apireq['validationStatus'] ) ) { |
||
248 | // ERROR: no GOOD, WARN or BAD results |
||
249 | // Update job properties. |
||
250 | $jprops['jqstat'] = $apireq['status']; |
||
251 | $jprops['jrstat'] = $apireq['resultStatus']; |
||
252 | $jprops['jvstat'] = $apireq['validationStatus']; |
||
253 | $jprops['jftime'] = microtime( true ); |
||
254 | autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $jprops['ljid'] . '> result request successful but job FAILED, status now is <' . $jprops['jqstat'] . '>', 3 ); |
||
255 | $apireq['css'] = '/* critical css removed for DEBUG logging purposes */'; |
||
256 | autoptimizeCriticalCSSCore::ao_ccss_log( 'Job response was: ' . json_encode( $apireq ), 3 ); |
||
257 | } else { |
||
258 | // UNKNOWN: unhandled JOB_DONE exception |
||
259 | // Update job properties. |
||
260 | $jprops['jqstat'] = 'JOB_UNKNOWN'; |
||
261 | $jprops['jrstat'] = $apireq['resultStatus']; |
||
262 | $jprops['jvstat'] = $apireq['validationStatus']; |
||
263 | $jprops['jftime'] = microtime( true ); |
||
264 | autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $jprops['ljid'] . '> result request successful but job is UNKNOWN, status now is <' . $jprops['jqstat'] . '>', 2 ); |
||
265 | $apireq['css'] = '/* critical css removed for DEBUG logging purposes */'; |
||
266 | autoptimizeCriticalCSSCore::ao_ccss_log( 'Job response was: ' . json_encode( $apireq ), 3 ); |
||
267 | } |
||
268 | } elseif ( 'JOB_FAILED' == $apireq['job']['status'] || 'STATUS_JOB_BAD' == $apireq['job']['status'] ) { |
||
269 | // ERROR: failed job |
||
270 | // Update job properties. |
||
271 | $jprops['jqstat'] = $apireq['job']['status']; |
||
272 | if ( $apireq['error'] ) { |
||
273 | $jprops['jrstat'] = $apireq['job']['error']; |
||
274 | } else { |
||
275 | } |
||
276 | $jprops['jvstat'] = 'NONE'; |
||
277 | $jprops['jftime'] = microtime( true ); |
||
278 | autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $jprops['ljid'] . '> result request successful but job FAILED, status now is <' . $jprops['jqstat'] . '>', 3 ); |
||
279 | View Code Duplication | } elseif ( 'This css no longer exists. Please re-generate it.' == $apireq['error'] ) { |
|
280 | // ERROR: CSS doesn't exist |
||
281 | // Update job properties. |
||
282 | $jprops['jqstat'] = 'NO_CSS'; |
||
283 | $jprops['jrstat'] = $apireq['error']; |
||
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 ( empty( $apireq ) ) { |
||
288 | // ERROR: no response |
||
289 | // Update job properties. |
||
290 | $jprops['jqstat'] = 'NO_RESPONSE'; |
||
291 | $jprops['jrstat'] = 'NONE'; |
||
292 | $jprops['jvstat'] = 'NONE'; |
||
293 | $jprops['jftime'] = microtime( true ); |
||
294 | autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $jprops['ljid'] . '> request has no response, status now is <' . $jprops['jqstat'] . '>', 3 ); |
||
295 | } else { |
||
296 | // UNKNOWN: unhandled results exception |
||
297 | // Update job properties. |
||
298 | $jprops['jqstat'] = 'JOB_UNKNOWN'; |
||
299 | $jprops['jrstat'] = 'NONE'; |
||
300 | $jprops['jvstat'] = 'NONE'; |
||
301 | $jprops['jftime'] = microtime( true ); |
||
302 | 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 ); |
||
303 | } |
||
304 | |||
305 | // Set queue update flag. |
||
306 | $update = true; |
||
307 | } |
||
308 | |||
309 | // Mark DONE jobs for removal. |
||
310 | if ( 'JOB_DONE' == $jprops['jqstat'] ) { |
||
311 | $update = true; |
||
312 | $deljob = true; |
||
313 | } |
||
314 | |||
315 | // Persist updated queue object. |
||
316 | if ( $update ) { |
||
317 | if ( ! $deljob ) { |
||
318 | // Update properties of a NEW or PENDING job... |
||
319 | $ao_ccss_queue[ $path ] = $jprops; |
||
320 | } else { |
||
321 | // ...or remove the DONE job. |
||
322 | unset( $ao_ccss_queue[ $path ] ); |
||
323 | autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $jprops['ljid'] . '> is DONE and was removed from the queue', 3 ); |
||
324 | } |
||
325 | |||
326 | // Update queue object. |
||
327 | $ao_ccss_queue_raw = json_encode( $ao_ccss_queue ); |
||
328 | update_option( 'autoptimize_ccss_queue', $ao_ccss_queue_raw, false ); |
||
329 | autoptimizeCriticalCSSCore::ao_ccss_log( 'Queue updated by job id <' . $jprops['ljid'] . '>', 3 ); |
||
330 | |||
331 | // Update target rule. |
||
332 | if ( $rule_update ) { |
||
333 | $this->ao_ccss_rule_update( $jprops['ljid'], $jprops['rtarget'], $jprops['file'], $jprops['hash'] ); |
||
334 | autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $jprops['ljid'] . '> updated the target rule <' . $jprops['rtarget'] . '>', 3 ); |
||
335 | } |
||
336 | } else { |
||
337 | // Or log no queue action. |
||
338 | autoptimizeCriticalCSSCore::ao_ccss_log( 'Nothing to do on this job', 3 ); |
||
339 | } |
||
340 | |||
341 | // Break the loop if request limit is set and was reached. |
||
342 | if ( $ao_ccss_rlimit && $ao_ccss_rlimit == $jr ) { |
||
343 | autoptimizeCriticalCSSCore::ao_ccss_log( 'The limit of ' . $ao_ccss_rlimit . ' request(s) to criticalcss.com was reached, queue control must finish now', 3 ); |
||
344 | break; |
||
345 | } |
||
346 | |||
347 | // Increment job counter. |
||
348 | $jc++; |
||
349 | } |
||
350 | |||
351 | // Remove the lock file and log the queue end. |
||
352 | if ( file_exists( AO_CCSS_LOCK ) ) { |
||
353 | unlink( AO_CCSS_LOCK ); |
||
354 | autoptimizeCriticalCSSCore::ao_ccss_log( 'Queue control unlocked', 3 ); |
||
355 | } |
||
356 | autoptimizeCriticalCSSCore::ao_ccss_log( 'Queue control finished', 3 ); |
||
357 | |||
358 | // Log that queue is locked. |
||
359 | } else { |
||
360 | autoptimizeCriticalCSSCore::ao_ccss_log( 'Queue is already running, skipping the attempt to run it again', 3 ); |
||
361 | } |
||
362 | } |
||
363 | |||
830 |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: