Conditions | 59 |
Paths | > 20000 |
Total Lines | 229 |
Lines | 42 |
Ratio | 18.34 % |
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 |
||
211 | public function read( $options ) |
||
212 | { |
||
213 | $noptimize_js = apply_filters( 'autoptimize_filter_js_noptimize', false, $this->content ); |
||
214 | if ( $noptimize_js ) { |
||
215 | return false; |
||
216 | } |
||
217 | |||
218 | // only optimize known good JS? |
||
219 | $allowlist_js = apply_filters( 'autoptimize_filter_js_allowlist', '', $this->content ); |
||
220 | $allowlist_js = apply_filters( 'autoptimize_filter_js_whitelist', $allowlist_js, $this->content ); // fixme: to be removed in next version. |
||
221 | View Code Duplication | if ( ! empty( $allowlist_js ) ) { |
|
|
|||
222 | $this->allowlist = array_filter( array_map( 'trim', explode( ',', $allowlist_js ) ) ); |
||
223 | } |
||
224 | |||
225 | // is there JS we should simply remove? |
||
226 | $removable_js = apply_filters( 'autoptimize_filter_js_removables', '', $this->content ); |
||
227 | View Code Duplication | if ( ! empty( $removable_js ) ) { |
|
228 | $this->jsremovables = array_filter( array_map( 'trim', explode( ',', $removable_js ) ) ); |
||
229 | } |
||
230 | |||
231 | // only header? |
||
232 | View Code Duplication | if ( apply_filters( 'autoptimize_filter_js_justhead', $options['justhead'] ) ) { |
|
233 | $content = explode( '</head>', $this->content, 2 ); |
||
234 | $this->content = $content[0] . '</head>'; |
||
235 | $this->restofcontent = $content[1]; |
||
236 | } |
||
237 | |||
238 | // Determine whether we're doing JS-files aggregation or not. |
||
239 | if ( ! $options['aggregate'] ) { |
||
240 | $this->aggregate = false; |
||
241 | } |
||
242 | // Returning true for "dontaggregate" turns off aggregation. |
||
243 | if ( $this->aggregate && apply_filters( 'autoptimize_filter_js_dontaggregate', false ) ) { |
||
244 | $this->aggregate = false; |
||
245 | } |
||
246 | |||
247 | // Defer when not aggregating. |
||
248 | if ( false === $this->aggregate && apply_filters( 'autoptimize_js_filter_defer_not_aggregate', $options['defer_not_aggregate'] ) ) { |
||
249 | $this->defer_not_aggregate = true; |
||
250 | } |
||
251 | |||
252 | // include inline? |
||
253 | if ( apply_filters( 'autoptimize_js_include_inline', $options['include_inline'] ) ) { |
||
254 | $this->include_inline = true; |
||
255 | } |
||
256 | |||
257 | // filter to "late inject minified JS", default to true for now (it is faster). |
||
258 | $this->inject_min_late = apply_filters( 'autoptimize_filter_js_inject_min_late', true ); |
||
259 | |||
260 | // filters to override hardcoded do(nt)move(last) array contents (array in, array out!). |
||
261 | $this->dontmove = apply_filters( 'autoptimize_filter_js_dontmove', $this->dontmove ); |
||
262 | $this->domovelast = apply_filters( 'autoptimize_filter_js_movelast', $this->domovelast ); |
||
263 | $this->domove = apply_filters( 'autoptimize_filter_js_domove', $this->domove ); |
||
264 | |||
265 | // Determine whether excluded files should be minified if not yet so. |
||
266 | if ( ! $options['minify_excluded'] && $options['aggregate'] ) { |
||
267 | $this->minify_excluded = false; |
||
268 | } |
||
269 | $this->minify_excluded = apply_filters( 'autoptimize_filter_js_minify_excluded', $this->minify_excluded, '' ); |
||
270 | |||
271 | // get extra exclusions settings or filter. |
||
272 | $exclude_js = $options['js_exclude']; |
||
273 | $exclude_js = apply_filters( 'autoptimize_filter_js_exclude', $exclude_js, $this->content ); |
||
274 | |||
275 | if ( '' !== $exclude_js ) { |
||
276 | if ( is_array( $exclude_js ) ) { |
||
277 | $remove_keys = array_keys( $exclude_js, 'remove' ); |
||
278 | if ( false !== $remove_keys ) { |
||
279 | foreach ( $remove_keys as $remove_key ) { |
||
280 | unset( $exclude_js[ $remove_key ] ); |
||
281 | $this->jsremovables[] = $remove_key; |
||
282 | } |
||
283 | } |
||
284 | $excl_js_arr = array_keys( $exclude_js ); |
||
285 | } else { |
||
286 | $excl_js_arr = array_filter( array_map( 'trim', explode( ',', $exclude_js ) ) ); |
||
287 | } |
||
288 | $this->dontmove = array_merge( $excl_js_arr, $this->dontmove ); |
||
289 | } |
||
290 | |||
291 | // Should we add try-catch? |
||
292 | if ( $options['trycatch'] ) { |
||
293 | $this->trycatch = true; |
||
294 | } |
||
295 | |||
296 | // force js in head? |
||
297 | if ( $options['forcehead'] ) { |
||
298 | $this->forcehead = true; |
||
299 | } else { |
||
300 | $this->forcehead = false; |
||
301 | } |
||
302 | |||
303 | $this->forcehead = apply_filters( 'autoptimize_filter_js_forcehead', $this->forcehead ); |
||
304 | |||
305 | // get cdn url. |
||
306 | $this->cdn_url = $options['cdn_url']; |
||
307 | |||
308 | // noptimize me. |
||
309 | $this->content = $this->hide_noptimize( $this->content ); |
||
310 | |||
311 | // Save IE hacks. |
||
312 | $this->content = $this->hide_iehacks( $this->content ); |
||
313 | |||
314 | // comments. |
||
315 | $this->content = $this->hide_comments( $this->content ); |
||
316 | |||
317 | // Get script files. |
||
318 | if ( preg_match_all( '#<script.*</script>#Usmi', $this->content, $matches ) ) { |
||
319 | foreach ( $matches[0] as $tag ) { |
||
320 | // only consider script aggregation for types allowlisted in should_aggregate-function. |
||
321 | $should_aggregate = $this->should_aggregate( $tag ); |
||
322 | if ( ! $should_aggregate ) { |
||
323 | $tag = ''; |
||
324 | continue; |
||
325 | } |
||
326 | |||
327 | if ( preg_match( '#<script[^>]*src=("|\')([^>]*)("|\')#Usmi', $tag, $source ) ) { |
||
328 | // non-inline script. |
||
329 | View Code Duplication | if ( $this->isremovable( $tag, $this->jsremovables ) ) { |
|
330 | $this->content = str_replace( $tag, '', $this->content ); |
||
331 | continue; |
||
332 | } |
||
333 | |||
334 | $orig_tag = null; |
||
335 | $url = current( explode( '?', $source[2], 2 ) ); |
||
336 | $path = $this->getpath( $url ); |
||
337 | if ( false !== $path && preg_match( '#\.js$#', $path ) && $this->ismergeable( $tag ) ) { |
||
338 | // ok to optimize, add to array. |
||
339 | $this->scripts[] = $path; |
||
340 | } else { |
||
341 | $orig_tag = $tag; |
||
342 | $new_tag = $tag; |
||
343 | |||
344 | // non-mergeable script (excluded or dynamic or external). |
||
345 | if ( is_array( $exclude_js ) ) { |
||
346 | // should we add flags? |
||
347 | foreach ( $exclude_js as $excl_tag => $excl_flags ) { |
||
348 | if ( false !== strpos( $orig_tag, $excl_tag ) && in_array( $excl_flags, array( 'async', 'defer' ) ) ) { |
||
349 | $new_tag = str_replace( '<script ', '<script ' . $excl_flags . ' ', $new_tag ); |
||
350 | } |
||
351 | } |
||
352 | } |
||
353 | |||
354 | // not aggregating but deferring? |
||
355 | if ( $this->defer_not_aggregate && false === $this->aggregate && str_replace( $this->dontmove, '', $path ) === $path && strpos( $new_tag, ' defer' ) === false ) { |
||
356 | $new_tag = str_replace( '<script ', '<script defer ', $new_tag ); |
||
357 | // and remove async as async+defer=async while we explicitly want defer. |
||
358 | if ( strpos( $new_tag, ' async' ) !== false && apply_filters( 'autoptimize_filter_js_defer_remove_async', true ) ) { |
||
359 | $new_tag = str_replace( array( ' async', ' async="async"', " async='async'" ), '', $new_tag ); |
||
360 | } |
||
361 | } |
||
362 | |||
363 | // Should we minify the non-aggregated script? |
||
364 | // -> if aggregate is on and exclude minify is on |
||
365 | // -> if aggregate is off and the file is not in dontmove. |
||
366 | View Code Duplication | if ( $path && $this->minify_excluded ) { |
|
367 | $consider_minified_array = apply_filters( 'autoptimize_filter_js_consider_minified', false ); |
||
368 | if ( ( false === $this->aggregate && str_replace( $this->dontmove, '', $path ) === $path ) || ( true === $this->aggregate && ( false === $consider_minified_array || str_replace( $consider_minified_array, '', $path ) === $path ) ) ) { |
||
369 | $minified_url = $this->minify_single( $path ); |
||
370 | if ( ! empty( $minified_url ) ) { |
||
371 | // Replace original URL with minified URL from cache. |
||
372 | $new_tag = str_replace( $url, $minified_url, $new_tag ); |
||
373 | } elseif ( apply_filters( 'autoptimize_filter_ccsjs_remove_empty_minified_url', false ) ) { |
||
374 | // Remove the original script tag, because cache content is empty but only if filter |
||
375 | // is trued because $minified_url is also false if original JS is minified already. |
||
376 | $new_tag = ''; |
||
377 | } |
||
378 | } |
||
379 | } |
||
380 | |||
381 | if ( $this->ismovable( $new_tag ) ) { |
||
382 | // can be moved, flags and all. |
||
383 | if ( $this->movetolast( $new_tag ) ) { |
||
384 | $this->move['last'][] = $new_tag; |
||
385 | } else { |
||
386 | $this->move['first'][] = $new_tag; |
||
387 | } |
||
388 | View Code Duplication | } else { |
|
389 | // cannot be moved, so if flag was added re-inject altered tag immediately. |
||
390 | if ( ( '' !== $new_tag && $orig_tag !== $new_tag ) || ( '' === $new_tag && apply_filters( 'autoptimize_filter_js_remove_empty_files', false ) ) ) { |
||
391 | $this->content = str_replace( $orig_tag, $new_tag, $this->content ); |
||
392 | $orig_tag = ''; |
||
393 | } |
||
394 | // and forget about the $tag (not to be touched any more). |
||
395 | $tag = ''; |
||
396 | } |
||
397 | } |
||
398 | } else { |
||
399 | // Inline script. |
||
400 | View Code Duplication | if ( $this->isremovable( $tag, $this->jsremovables ) ) { |
|
401 | $this->content = str_replace( $tag, '', $this->content ); |
||
402 | continue; |
||
403 | } |
||
404 | |||
405 | // unhide comments, as javascript may be wrapped in comment-tags for old times' sake. |
||
406 | $tag = $this->restore_comments( $tag ); |
||
407 | if ( $this->ismergeable( $tag ) && $this->include_inline ) { |
||
408 | preg_match( '#<script.*>(.*)</script>#Usmi', $tag, $code ); |
||
409 | $code = preg_replace( '#.*<!\[CDATA\[(?:\s*\*/)?(.*)(?://|/\*)\s*?\]\]>.*#sm', '$1', $code[1] ); |
||
410 | $code = preg_replace( '/(?:^\\s*<!--\\s*|\\s*(?:\\/\\/)?\\s*-->\\s*$)/', '', $code ); |
||
411 | $this->scripts[] = 'INLINE;' . $code; |
||
412 | } else { |
||
413 | // Can we move this? |
||
414 | $autoptimize_js_moveable = apply_filters( 'autoptimize_js_moveable', '', $tag ); |
||
415 | if ( $this->ismovable( $tag ) || '' !== $autoptimize_js_moveable ) { |
||
416 | if ( $this->movetolast( $tag ) || 'last' === $autoptimize_js_moveable ) { |
||
417 | $this->move['last'][] = $tag; |
||
418 | } else { |
||
419 | $this->move['first'][] = $tag; |
||
420 | } |
||
421 | } else { |
||
422 | // We shouldn't touch this. |
||
423 | $tag = ''; |
||
424 | } |
||
425 | } |
||
426 | // Re-hide comments to be able to do the removal based on tag from $this->content. |
||
427 | $tag = $this->hide_comments( $tag ); |
||
428 | } |
||
429 | |||
430 | // Remove the original script tag. |
||
431 | $this->content = str_replace( $tag, '', $this->content ); |
||
432 | } |
||
433 | |||
434 | return true; |
||
435 | } |
||
436 | |||
437 | // No script files, great ;-) . |
||
438 | return false; |
||
439 | } |
||
440 | |||
797 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.