settings::comment_settings()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
1
<?php
2
require_once(dirname(__FILE__).'/../require/settings.php');
3
require_once(dirname(__FILE__).'/../require/class.Common.php');
4
5
class settings {
6
7
	/*
8
	* This function is used to modify a setting in settings.php file
9
	* @param Array list of settings and their values
10
	*/
11
	public static function modify_settings($settings) {
12
		$Common = new Common();
13
		$settings_filename = '../require/settings.php';
14
		$content = file_get_contents($settings_filename);
15
		$fh = fopen($settings_filename,'w');
16
		foreach ($settings as $settingname => $value) {
17
			if ($value == 'TRUE' || $value == 'FALSE') {
18
				$pattern = '/\R\$'.$settingname." = ".'(TRUE|FALSE)'."/";
19
				$replace = "\n".'\$'.$settingname." = ".$value."";
20
			} elseif (is_array($value)) {
21
				$pattern = '/\R\$'.$settingname." = array\(".'(.*)'."\)/";
22
				if ($Common->isAssoc($value)) {
23
					foreach ($value as $key => $data) {
24
						if (!isset($array_value)) {
25
							if (is_array($data)) {
26
								foreach ($data as $keya => $dataa) {
27
									if (is_array($dataa) && !empty($dataa)) {
28
										foreach ($dataa as $dataaa) {
29
											if (!isset($dataarraya)) $dataarraya = $dataaa;
30
											else $dataarraya .= "','".$dataaa;
31
										}
32
										$dataarray = "array('".$keya."' => array('".$dataarraya."'))";
0 ignored issues
show
Bug introduced by
The variable $dataarraya 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...
33
										unset($dataarraya);
34
									} else {
35
										if (!isset($dataarray)) $dataarray = "'".$dataa."'";
36
										else $dataarray .= ",'".$dataa."'";
37
									}
38
								}
39
								$array_value = "'".$key."' => ".$dataarray;
0 ignored issues
show
Bug introduced by
The variable $dataarray 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...
40
								unset($dataarray);
41
							} else {
42
								if ($data == 'TRUE' || $data == 'FALSE') {
43
									$array_value = "'".$key."' => ".$data."";
44
								} else {
45
									$array_value = "'".$key."' => '".$data."'";
46
								}
47
							}
48
						} else {
49
							if (is_array($data)) {
50
								foreach ($data as $keya => $dataa) {
51
									if (is_array($dataa) && !empty($dataa)) {
52
										foreach ($dataa as $dataaa) {
53
											if (!isset($dataarraya)) $dataarraya = $dataaa;
54
											else $dataarraya .= "','".$dataaa;
55
										}
56
										$dataarray = "array('".$keya."' => array('".$dataarraya."'))";
57
										unset($dataarraya);
58
									} else {
59
										if (!isset($dataarray)) $dataarray = "'".$dataa."'";
60
										else $dataarray .= "','".$dataa."'";
61
									}
62
								}
63
								$array_value .= ",'".$key."' => ".$dataarray;
64
								unset($dataarray);
65
							} else {
66
								if ($data == 'TRUE' || $data == 'FALSE') {
67
									$array_value .= ",'".$key."' => ".$data."";
68
								} else {
69
									$array_value .= ",'".$key."' => '".$data."'";
70
								}
71
							}
72
						}
73
					}
74
				} else {
75
					foreach ($value as $key => $data) {
76
						if (is_array($data) && $Common->isAssoc($data)) {
77
							foreach ($data as $keyd => $datad) {
78
								if (!isset($arrayd_value)) {
79
									if ($datad == 'TRUE' || $datad == 'FALSE') {
80
										$arrayd_value = "'".$keyd."' => ".$datad."";
81
									} else {
82
										$arrayd_value = "'".$keyd."' => '".$datad."'";
83
									}
84
								} else {
85
									if ($datad == 'TRUE' || $datad == 'FALSE') {
86
										$arrayd_value .= ",'".$keyd."' => ".$datad."";
87
									} else {
88
										$arrayd_value .= ",'".$keyd."' => '".$datad."'";
89
									}
90
								}
91
							}
92
							if (!isset($array_value)) {
93
								if (!isset($arrayd_value)) $arrayd_value = '';
94
								//$array_value = "'".$key."' => array(".$arrayd_value.")";
95
								$array_value = "array(".$arrayd_value.")";
96
							} elseif (isset($arrayd_value)) {
97
								//$array_value .= ",'".$key."' => array(".$arrayd_value.")";
98
								$array_value .= ",array(".$arrayd_value.")";
99
							}
100
							unset($arrayd_value);
101
						} else {
102
							if (!isset($array_value)) {
103
								$array_value = "'".$data."'";
104
							} else {
105
								$array_value .= ",'".$data."'";
106
							}
107
						}
108
					}
109
				}
110
				if (!isset($array_value)) $array_value = '';
111
				$replace = "\n".'\$'.$settingname." = array(".$array_value.")";
112
				unset($array_value);
113
			} else {
114
				$pattern = '/\R\$'.$settingname." = '".'(.*)'."'/";
115
				$replace = "\n".'\$'.$settingname." = '".$value."'";
116
			}
117
			$rep_cnt = 0;
118
			$content = preg_replace($pattern,$replace,$content,1,$rep_cnt);
119
			
120
			/// If setting was a string and is now an array
121
			if ($rep_cnt === 0 && is_array($value)) {
122
				$pattern = '/\R\$'.$settingname." = '".'(.*)'."'/";
123
				$content = preg_replace($pattern,$replace,$content,1,$rep_cnt);
124
			}
125
			
126
			// If setting is not in settings.php (for update)
127
			if ($rep_cnt === 0) {
128
				$content = preg_replace('/\?>/',$replace.";\n?>",$content,1,$rep_cnt);
129
			}
130
131
		}
132
		fwrite($fh,$content);
133
		fclose($fh);
134
	}
135
136
	/*
137
	* This function is used to comment a setting in settings.php file
138
	* @param Array list of settings to comment
139
	*/
140
	public static function comment_settings($settings) {
141
		$Common = new Common();
0 ignored issues
show
Unused Code introduced by
$Common is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
142
		$settings_filename = '../require/settings.php';
143
		$content = file_get_contents($settings_filename);
144
		$fh = fopen($settings_filename,'w');
145
		foreach ($settings as $settingname) {
146
			$pattern = '/\R\$'.$settingname." = /";
147
			$replace = '//$'.$settingname." = ";
148
			$content = preg_replace($pattern,$replace,$content);
149
		}
150
		fwrite($fh,$content);
151
		fclose($fh);
152
	}
153
}
154
155
//settings::comment_settings(array('globalSBS1Hosts'));
156
?>