Conditions | 17 |
Paths | 160 |
Total Lines | 68 |
Lines | 0 |
Ratio | 0 % |
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 |
||
13 | function wgWizKeepVars($array, $prefix="") { |
||
14 | // this function translates the given array to a list |
||
15 | // of hidden input types. When $ARCurrent->override is on |
||
16 | // there is no check to see wether a given variable |
||
17 | // has already been seen and $ARCurrent->seenit is set. |
||
18 | // In the case of an array, seenit is set to the name of |
||
19 | // the array (for each element). In the case of a normal |
||
20 | // variable, seenit is set to that variable's name. |
||
21 | // When $ARCurrent->override is not set, each variable is |
||
22 | // first checked against the 'seenit' array. If it is an |
||
23 | // element of an array, seenit is checked against the name |
||
24 | // of the array, else it is checked against the name of |
||
25 | // the variable itself. |
||
26 | // Only 'leaf' node or array elements are checked. |
||
27 | |||
28 | global $ARCurrent, $AR; |
||
29 | if (!$prefix) { |
||
30 | $prefix="arStoreVars"; |
||
31 | } |
||
32 | $prefix.="["; |
||
33 | if ($prefix==="arStoreVars[") { |
||
34 | $toplevel=true; |
||
35 | } |
||
36 | if (!($regexp=$ARCurrent->regexp)) { |
||
37 | $regexp='/^arStoreVars\[('; |
||
38 | reset($AR->nls->list); |
||
39 | foreach( $AR->nls->list as $key => $value ) { |
||
40 | $regexp.=$key.'|'; |
||
41 | } |
||
42 | $regexp=substr($regexp,0,-1).')\]\[$/'; |
||
43 | $ARCurrent->regexp=$regexp; |
||
44 | } |
||
45 | if (preg_match($ARCurrent->regexp, $prefix)) { |
||
46 | $toplevel=true; |
||
47 | } |
||
48 | $postfix="]"; |
||
49 | |||
50 | $ignoreVars = array( |
||
51 | "wgWizAction" => true, |
||
52 | ); |
||
53 | |||
54 | if( is_array($array ) ) { |
||
55 | reset($array); |
||
56 | foreach( $array as $key => $value ) { |
||
57 | if( !$ignoreVars[$key] ) { |
||
58 | if (is_array($value)) { |
||
59 | if ($key!=="arStoreVars") { |
||
60 | wgWizKeepVars($value, $prefix.$key.$postfix); |
||
61 | } |
||
62 | } elseif ($ARCurrent->override) { // don't check $ARCurrent->seenit, do set it. |
||
63 | if ($toplevel) { // this is a normal name-value pair |
||
|
|||
64 | $ARCurrent->seenit[$prefix.$key.$postfix]=true; |
||
65 | } else { // it's part of an array |
||
66 | $ARCurrent->seenit[$prefix]=true; |
||
67 | } |
||
68 | $value = htmlentities($value, ENT_QUOTES, 'UTF-8'); |
||
69 | echo "<input type=\"hidden\" name=\"".$prefix.$key.$postfix."\" value=\"".$value."\">\n"; |
||
70 | } elseif (!$ARCurrent->seenit[$prefix] && !$toplevel) { // value part of array |
||
71 | $value = htmlentities($value, ENT_QUOTES, 'UTF-8'); |
||
72 | echo "<input type=\"hidden\" name=\"".$prefix.$key.$postfix."\" value=\"".$value."\">\n"; |
||
73 | } elseif (!$ARCurrent->seenit[$prefix.$key.$postfix] && $toplevel) { // value not in array |
||
74 | $value = htmlentities($value, ENT_QUOTES, 'UTF-8'); |
||
75 | echo "<input type=\"hidden\" name=\"".$prefix.$key.$postfix."\" value=\"".$value."\">\n"; |
||
76 | } |
||
77 | } |
||
78 | } |
||
79 | } |
||
80 | } |
||
81 | } |
||
110 |
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:
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
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: