Completed
Push — master ( e5ef44...29e0f1 )
by Auke
18s
created

dialog.grants.logic.php ➔ grantsArrayToString()   C

Complexity

Conditions 11
Paths 2

Size

Total Lines 40
Code Lines 32

Duplication

Lines 26
Ratio 65 %

Code Coverage

Tests 0
CRAP Score 132
Metric Value
cc 11
eloc 32
nc 2
nop 1
dl 26
loc 40
rs 5.2653
ccs 0
cts 39
cp 0
crap 132

How to fix   Complexity   

Long Method

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:

1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 6 and the first side effect is on line 2.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
	$userConfig = $this->loadUserConfig();
3
	$authconfig = $userConfig['authentication'];
4
5
	if( !function_exists("grantsArrayToString") ) {
6
		function grantsArrayToString($grants) {
7
			$grantstring = "";
8
			if (is_array($grants)) {
9
				foreach ($grants as $grant => $granttype) {
10
					if (is_array($granttype)) {
11
						$grantstring .= " $grant ( ";
12
						reset($granttype);
13
						while (list($class, $modifierId)=each($granttype)) {
14 View Code Duplication
							if( $granttype > 0 ) {
15
								switch($modifierId) {
16
									case ARGRANTLOCAL:
17
										$modifier = "=";
18
									break;
19
									case ARGRANTCHILDREN:
20
										$modifier = ">";
21
									break;
22
									default:
23
										$modifier = "";
24
								}
25
								$grantstring .= " $modifier$class ";
26
							}
27
						}
28
						$grantstring .= " ) ";
29 View Code Duplication
					} elseif( $granttype > 0 ) {
30
						switch($granttype) {
31
							case ARGRANTLOCAL:
32
								$modifier = "=";
33
							break;
34
							case ARGRANTCHILDREN:
35
								$modifier = ">";
36
							break;
37
							default:
38
								$modifier = "";
39
						}
40
						$grantstring .= " $modifier$grant ";
41
					}
42
				}
43
			}
44
			return $grantstring;
45
		}
46
	}
47
48
	if( !function_exists("arGetGrantType") ) {
49
		function arGetGrantType($value) {
50
			if (($value & ARMASKLOCAL) && ($value & ARMASKCHILDREN)) {
51
				$result="";
52
			} else if ($value & ARMASKLOCAL) {
53
				$result="=";
54
			} else {
55
				$result=">";
56
			}
57
			return $result;
58
		}
59
	}
60
61
	if( !function_exists("array_compare") ) {
62
		function array_compare(&$ar1, &$ar2) {
63
			if (count($ar1) != count($ar2)) {
64
				return false;
65
			} else {
66
				foreach ($ar1 as $key => $value) {
67
					if (is_array($value) && is_array($ar2[$key])) {
68
						return array_compare($ar1[$key], $ar2[$key]);
69
					} else
70
					if ($value !== $ar2[$key]) {
71
						return false;
72
					}
73
					return true;
74
				}
75
			}
76
		}
77
	}
78
79
	if( !function_exists("getClass") ) {
80
		function getClass($grey=false) {
81
			global $ARCurrent;
82
			if ($ARCurrent->oddline=($ARCurrent->oddline+1)%2) {
83
				$class='odd';
84
			} else {
85
				$class='even';
86
			}
87
			if ($grey) {
88
				$class .= '-grey';
89
			}
90
			return $class;
91
		}
92
	}
93
94
	if( !function_exists("getPathByType") ) {
95
		function getPathByType($type, $id) {
96
			global $AR;
97
			switch ($type) {
98
				case 'pgroup':
99 View Code Duplication
					foreach ($authconfig['groupdirs'] as $groupdir) {
0 ignored issues
show
Bug introduced by
The variable $authconfig does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
100
						$path  = current($AR->user->find($groupdir, 'login.value=\''.$id.'\'', 'system.get.path.phtml'));
101
						if ($path) {
102
							break;
103
						}
104
					}
105
					break;
106
				case 'puser':
107 View Code Duplication
					foreach ($authconfig['userdirs'] as $userdir) {
108
						$path  = current($AR->user->find($userdir, 'login.value=\''.$id.'\'', 'system.get.path.phtml'));
109
						if ($path) {
110
							break;
111
						}
112
					}
113
					break;
114
			}
115
			return $path;
0 ignored issues
show
Bug introduced by
The variable $path 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...
116
		}
117
	}
118
119
	if( !function_exists("removeSimilarGrants") ) {
120
		function removeSimilarGrants($grants) {
121
			// first check for similar grants (same modifiers)
122
			$grants_check = $grants;
123
			foreach ($grants as $grant => $grant_ar) {
124
				// loop $grants for each entry in $grants_check (which is a copy)
125
				array_shift($grants_check);
126
				if (is_array($grant_ar)) {
127
					foreach ($grants_check as $grant_c => $grant_ar_c) {
128
						if (is_array($grant_ar_c) && array_compare($grant_ar, $grant_ar_c)) {
129
							unset($grants[$grant_c]);
130
						}
131
					}
132
				}
133
			}
134
			reset($grants);
135
			ksort($grants);
136
			return $grants;
137
		}
138
	}
139
140
	if( !function_exists("getGrantString") ) {
141
		function getGrantString($id, $type, $grants, $grey) {
142
			if ($grants && is_array($grants)) {
143
				$grants = removeSimilarGrants($grants);
144
				$grant_display = '';
145
				$grant_string = '';
146
147
				foreach ($grants as $grant => $modifiers) {
148
					if (!is_array($modifiers)) {
149
						$grant_type=arGetGrantType($modifiers);
150
						$grant_display .= htmlspecialchars($grant_type); // echo
151
					} else {
152
						$grant_type='';
153
					}
154
155
					if (!$grey) {
156
						$grant_display .= "<a href=\"javascript:selectGrant('$type', '$id', '$grant');\">";
157
					} else {
158
						$grant_display .= "<span class='grey'>";
159
					}
160
161
					$grant_display .= $grant;
162
					if (is_array($grants_eq[$grant])) {
0 ignored issues
show
Bug introduced by
The variable $grants_eq does not exist. Did you mean $grants?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
163
						foreach ($grants_eq[$grant] as $g_add) {
0 ignored issues
show
Bug introduced by
The variable $grants_eq does not exist. Did you mean $grants?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
164
							$grant .= ", ".$grant_type."$g_add";
165
						}
166
						$grant_display .= "[$grant]";
167
						$grant_string .= "[$grant]";
168
					} else {
169
						$grant_string .= "$grant_type$grant ";
170
					}
171
					if (is_array($modifiers)) {
172
						$grant_string .= "( ";
173
						$grant_display .= "( ";
174
						foreach ($modifiers as $modifier => $value) {
175
							$grant_type=arGetGrantType($value);
176
							$grant_display .= htmlspecialchars($grant_type);
177
							$grant_display .= $modifier." ";
178
							$grant_string .= $grant_type.$modifier." ";
179
						}
180
						$grant_display .= ") ";
181
						$grant_string .= ") ";
182
					}
183
					if (!$grey) {
184
						$grant_display .= "</a>";
185
					} else {
186
						$grant_display .= "</span>";
187
					}
188
				}
189
				$grant_display .= " ";
190
				$grant_string .= " ";
191
			}
192
			return array($grant_string, $grant_display);
0 ignored issues
show
Bug introduced by
The variable $grant_string 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...
Bug introduced by
The variable $grant_display 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...
193
		}
194
	}
195
196
	if( !function_exists("DisplayGrants") ) {
197
		function DisplayGrants(&$grantslist, $type, $grey=false) {
198
			global $AR, $ARCurrent;
199
			if ($grantslist[$type] && is_array($grantslist[$type])) {
200
				ksort($grantslist[$type]);
201
				while (list($id, $grants)=each($grantslist[$type])) { // path was login en is weer login
202
					$grant_string = "";
0 ignored issues
show
Unused Code introduced by
$grant_string 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...
203
					if (!$ARCurrent->donelist[$type][$id]) {
204
						$ARCurrent->donelist[$type][$id]=true;
205
						$row_id = "grants-$type-$id";
206
						$row_class = getClass($grey);
207
						$path = getPathByType($type, $id);
208
						$grant_strings = getGrantString($id, $type, $grants, $grey);
209
						$grant_string = $grant_strings[0];
210
						$grant_html = $grant_strings[1];
211
						$icon = $AR->dir->images . "icons/small/" . $type . ".png";
212
213
						?>
214
						<script type="text/javascript">
215
							id2path['<?php echo $type; ?>']['<?php echo $id; ?>'] = '<?php echo $path; ?>';
216
							grant_strings['<?php echo $type; ?>']['<?php echo $id; ?>']='<?php echo $grant_string; ?>';
217
						</script>
218
						<tr class='<?php echo $row_class; ?>' id="<?php echo $row_id; ?>">
219
							<td>
220
								<img alt="<?php echo $type; ?>" src="<?php echo $icon; ?>">
221
							</td>
222
							<td>
223
								<a href="javascript:loadUser('<?php echo $type; ?>', '<?php echo $id; ?>');"><?php echo $id; ?></a>
224
							</td>
225
							<td>
226
								<?php echo $grant_html; ?>
227
							</td>
228
						</tr>
229
						<?php
230
					}
231
				}
232
			}
233
		}
234
	}
235
?>
236