Passed
Push — master ( 60257d...58a993 )
by Warwick
02:42 queued 13s
created

LSX_Optimisation   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 57
rs 10
c 0
b 0
f 0
wmc 8
lcom 1
cbo 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A get_instance() 0 7 2
A preload_css() 0 6 3
A defer_parsing_of_js() 0 6 2
1
<?php
2
3
if ( ! defined( 'ABSPATH' ) ) {
4
	exit;
5
}
6
7
/**
8
 * Holds the speed optimization functions for LSX.
9
 *
10
 * @author   LightSpeed
11
 * @category Widgets
12
 * @package  LSX
13
 * @return   LSX_Optimisation
14
 */
15
class LSX_Optimisation {
16
17
	/**
18
	 * Holds class instance
19
	 *
20
	 * @since 1.0.0
21
	 * @var      object
22
	 */
23
	protected static $instance = null;
24
25
	/**
26
	 * Constructor.
27
	 */
28
	public function __construct() {
29
		add_filter( 'style_loader_tag', array( $this, 'preload_css' ), 100, 4 );
30
		add_filter( 'script_loader_tag', array( $this, 'defer_parsing_of_js' ), 100, 3 );
31
	}
32
	/**
33
	 * Return an instance of this class.
34
	 *
35
	 * @since 1.0.0
36
	 * @return    object    A single instance of this class.
37
	 */
38
	public static function get_instance() {
39
		// If the single instance hasn't been set, set it now.
40
		if ( null === self::$instance ) {
41
			self::$instance = new self;
42
		}
43
		return self::$instance;
44
	}
45
46
	/**
47
	 * Defers the JS loading till Last
48
	 *
49
	 * @param  string $url The url to check and defer.
0 ignored issues
show
Bug introduced by
There is no parameter named $url. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
50
	 * @return string
51
	 */
52
	public function preload_css( $tag, $handle, $href, $media ) {
0 ignored issues
show
Unused Code introduced by
The parameter $href is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $media is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
53
		if ( 'lsx_fonts' === $handle || 'fontawesome' === $handle ) {
54
			$tag = str_replace( 'href', ' preload href', $tag );
55
		}
56
		return $tag;
57
	}
58
59
	/**
60
	 * Defers the JS loading till Last
61
	 *
62
	 * @param  string $url The url to check and defer.
0 ignored issues
show
Bug introduced by
There is no parameter named $url. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
63
	 * @return string
64
	 */
65
	public function defer_parsing_of_js( $tag, $handle, $href ) {
66
		if ( false !== stripos( $href, '.js' ) ) {
67
			$tag = str_replace( 'src=', ' defer src=', $tag );
68
		}
69
		return $tag;
70
	}
71
}
72
LSX_Optimisation::get_instance();
73