Completed
Push — master ( bcb77d...aa4fea )
by
unknown
03:19
created

LSX_Google_Font_Collection::get_location()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
if ( ! defined( 'ABSPATH' ) ) return; // Exit if accessed directly
3
/**
4
 * Google Font_Collection Class
5
**/
6
// this file controls all of the data for the custom fonts used in the theme customizer
7
8
// the Google_Font_Collection object is a wrapper that holds all of the individual custom fonts
9
class LSX_Google_Font_Collection
10
{
11
	private $fonts;
12
13
	/**
14
	 * Constructor
15
	**/
16
	public function __construct($fonts)
17
	{
18
		if(empty($fonts))
19
		{
20
			//we didn't get the required data
21
			return false;
0 ignored issues
show
Bug introduced by
Constructors do not have meaningful return values, anything that is returned from here is discarded. Are you sure this is correct?
Loading history...
22
		}
23
24
		// create fonts
25
		foreach ($fonts as $key => $value) 
26
		{	
27
			if( empty( $value["system"] ) ){
28
				$this->fonts[$value["title"]] = new LSX_Google_Font($value["title"], $value["location"], $value["cssDeclaration"], $value["cssClass"]);
29
			}
30
		}
31
	}
32
33
	/**
34
	 * get_font_family_name_array Function
35
	 * this function returns an array containing all of the font family names
36
	**/
37
	function get_font_family_name_array()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
38
	{
39
		$result;
0 ignored issues
show
Bug introduced by
The variable $result seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
40
		foreach ($this->fonts as $key => $value) 
41
		{
42
			$result[] = $value->__get("title");
0 ignored issues
show
Coding Style Comprehensibility introduced by
$result was never initialized. Although not strictly required by PHP, it is generally a good practice to add $result = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
43
		}
44
		return $result;
0 ignored issues
show
Bug introduced by
The variable $result does not seem to be defined for all execution paths leading up to this point.

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:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

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

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
45
	}
46
47
	/**
48
	 * get_title
49
	 * this function returns the font title
50
	**/
51
	function get_title($key)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
52
	{
53
		return $this->fonts[$key]->__get("title");
54
	}
55
56
	/**
57
	 * get_location
58
	 * this function returns the font location
59
	**/
60
	function get_location($key)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
61
	{
62
		return $this->fonts[$key]->__get("location");
63
	}
64
65
	/**
66
	 * get_css_declaration
67
	 * this function returns the font css declaration
68
	**/
69
	function get_css_declaration($key)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
70
	{
71
		return $this->fonts[$key]->__get("cssDeclaration");
72
	}
73
74
75
	/**
76
	 * get_css_class_array
77
	 * this function returns an array of css classes
78
	 * this function is used when displaying the fancy list of fonts in the theme customizer
79
	 * this function is used to send a JS file an array for the postMessage transport option in the theme customizer
80
	**/
81
	function get_css_class_array()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
82
	{
83
		$result;
0 ignored issues
show
Bug introduced by
The variable $result seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
84
		foreach ($this->fonts as $key => $value) 
85
		{
86
			$result[$value->__get("title")] = $value->__get("cssClass");
0 ignored issues
show
Coding Style Comprehensibility introduced by
$result was never initialized. Although not strictly required by PHP, it is generally a good practice to add $result = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
87
		}
88
		return $result;
0 ignored issues
show
Bug introduced by
The variable $result does not seem to be defined for all execution paths leading up to this point.

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:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

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

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
89
	}
90
91
	/**
92
	 * get_total_number_of_fonts
93
	 * this function returns the total number of fonts
94
	**/
95
	function get_total_number_of_fonts()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
96
	{
97
		return count($this->fonts);
98
	}
99
100
	/**
101
	 * print_theme_customizer_css_locations
102
	 * this function prints the links to the css files for the theme customizer
103
	**/
104
	function print_theme_customizer_css_locations()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
105
	{
106
		foreach ($this->fonts as $key => $value) 
107
		{
108
			$protocol = 'http';
109
			if(is_ssl()){ $protocol.='s'; }
110
			?>
111
			<?php echo wp_keses_post( '<' ); ?>link href="<?php echo esc_attr( $protocol ); ?>://fonts.googleapis.com/css?family=<?php echo esc_attr( $value->__get( "location" ) ); ?>" rel='stylesheet' type='text/css'<?php echo wp_keses_post( '>' ); ?>
112
			<?php
113
		}
114
	}
115
116
	/**
117
	 * print_theme_customizer_css_classes
118
	 * this function prints the theme customizer css classes necessary to display all of the fonts
119
	**/
120
	function print_theme_customizer_css_classes()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
121
	{
122
		?>
123
		<style type="text/css">
124
			<?php
125
			foreach ($this->fonts as $key => $value) 
126
			{
127
				?>
128
				.<?php echo esc_attr( $value->__get( "cssClass" ) ); ?>{
129
					font-family: <?php echo esc_attr( $value->__get( "cssDeclaration" ) ); ?>;
130
				}
131
				<?php
132
			}
133
			?>
134
		</style>
135
		<?php 
136
	}
137
138
} // Font_Collection