Conditions | 8 |
Paths | 128 |
Total Lines | 107 |
Code Lines | 37 |
Lines | 107 |
Ratio | 100 % |
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 |
||
60 | View Code Duplication | function b_random_partner_edit($options) |
|
61 | { |
||
62 | $form = "<table border='0'>"; |
||
63 | /*$form .= "<tr><td>"._MB_SPARTNER_PARTNERS_PSPACE."</td><td>"; |
||
64 | $chk = ""; |
||
65 | if ($options[0] == 0) { |
||
66 | $chk = " checked='checked'"; |
||
67 | } |
||
68 | $form .= "<input type='radio' name='options[0]' value='0'".$chk." />"._NO.""; |
||
69 | $chk = ""; |
||
70 | if ($options[0] == 1) { |
||
71 | $chk = " checked='checked'"; |
||
72 | } |
||
73 | $form .= "<input type='radio' name='options[0]' value='1'".$chk." />"._YES."</td></tr>";*/ |
||
74 | $form .= '<tr><td>' . _MB_SPARTNER_FADE . '</td><td>'; |
||
75 | $chk = ''; |
||
76 | if ($options[0] == 0) { |
||
77 | $chk = " checked='checked'"; |
||
78 | } |
||
79 | $form .= "<input type='radio' name='options[1]' value='0'" . $chk . ' />' . _NO . ''; |
||
80 | $chk = ''; |
||
81 | if ($options[0] == 1) { |
||
82 | $chk = " checked='checked'"; |
||
83 | } |
||
84 | $form .= "<input type='radio' name='options[1]' value='1'" . $chk . ' />' . _YES . '</td></tr>'; |
||
85 | /*$form .= "<tr><td>"._MB_SPARTNER_BRAND."</td><td>"; |
||
86 | $chk = ""; |
||
87 | if ($options[2] == 0) { |
||
88 | $chk = " checked='checked'"; |
||
89 | } |
||
90 | $form .= "<input type='radio' name='options[2]' value='0'".$chk." />"._NO.""; |
||
91 | $chk = ""; |
||
92 | if ($options[2] == 1) { |
||
93 | $chk = " checked='checked'"; |
||
94 | } |
||
95 | $form .= "<input type='radio' name='options[2]' value='1'".$chk." />"._YES."</td></tr>"; |
||
96 | $form .= "<tr><td>"._MB_SPARTNER_BLIMIT."</td><td>"; |
||
97 | $form .= "<input type='text' name='options[3]' size='16' value='".$options[3]."' /></td></tr>";*/ |
||
98 | $form .= '<tr><td>' . _MB_SPARTNER_BSHOW . '</td><td>'; |
||
99 | $form .= "<select size='1' name='options[1]'>"; |
||
100 | $sel = ''; |
||
101 | if ($options[1] == 1) { |
||
102 | $sel = " selected='selected'"; |
||
103 | } |
||
104 | $form .= "<option value='1' " . $sel . '>' . _MB_SPARTNER_IMAGES . '</option>'; |
||
105 | $sel = ''; |
||
106 | if ($options[1] == 2) { |
||
107 | $sel = " selected='selected'"; |
||
108 | } |
||
109 | $form .= "<option value='2' " . $sel . '>' . _MB_SPARTNER_TEXT . '</option>'; |
||
110 | $sel = ''; |
||
111 | if ($options[1] == 3) { |
||
112 | $sel = " selected='selected'"; |
||
113 | } |
||
114 | $form .= "<option value='3' " . $sel . '>' . _MB_SPARTNER_BOTH . '</option>'; |
||
115 | $form .= '</select></td></tr>'; |
||
116 | /*$form .= "<tr><td>"._MB_SPARTNER_BORDER."</td><td>"; |
||
117 | $form .= "<select size='1' name='options[5]'>"; |
||
118 | $sel = ""; |
||
119 | if ($options[5] == "id") { |
||
120 | $sel = " selected='selected'"; |
||
121 | } |
||
122 | $form .= "<option value='id' ".$sel.">"._MB_SPARTNER_ID."</option>"; |
||
123 | $sel = ""; |
||
124 | if ($options[5] == "hits") { |
||
125 | $sel = " selected='selected'"; |
||
126 | } |
||
127 | $form .= "<option value='hits' ".$sel.">"._MB_SPARTNER_HITS."</option>"; |
||
128 | $sel = ""; |
||
129 | if ($options[5] == "title") { |
||
130 | $sel = " selected='selected'"; |
||
131 | } |
||
132 | $form .= "<option value='title' ".$sel.">"._MB_SPARTNER_TITLE."</option>"; |
||
133 | if ($options[5] == "weight") { |
||
134 | $sel = " selected='selected'"; |
||
135 | } |
||
136 | $form .= "<option value='weight' ".$sel.">"._MB_SPARTNER_WEIGHT."</option>"; |
||
137 | $form .= "</select> "; |
||
138 | $form .= "<select size='1' name='options[6]'>"; |
||
139 | $sel = ""; |
||
140 | if ($options[6] == "ASC") { |
||
141 | $sel = " selected='selected'"; |
||
142 | } |
||
143 | $form .= "<option value='ASC' ".$sel.">"._MB_SPARTNER_ASC."</option>"; |
||
144 | $sel = ""; |
||
145 | if ($options[6] == "DESC") { |
||
146 | $sel = " selected='selected'"; |
||
147 | } |
||
148 | $form .= "<option value='DESC' ".$sel.">"._MB_SPARTNER_DESC."</option>"; |
||
149 | $form .= "</select></td></tr>"; |
||
150 | */ |
||
151 | $form .= '<tr><td>' . _MB_SPARTNER_SEE_ALL . '</td><td>'; |
||
152 | $chk = ''; |
||
153 | if ($options[2] == 0) { |
||
154 | $chk = " checked='checked'"; |
||
155 | } |
||
156 | $form .= "<input type='radio' name='options[2]' value='0'" . $chk . ' />' . _NO . ''; |
||
157 | $chk = ''; |
||
158 | if ($options[7] == 1) { |
||
159 | $chk = " checked='checked'"; |
||
160 | } |
||
161 | $form .= "<input type='radio' name='options[2]' value='1'" . $chk . ' />' . _YES . '</td></tr>'; |
||
162 | |||
163 | $form .= '</table>'; |
||
164 | |||
165 | return $form; |
||
166 | } |
||
167 |
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:
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 thebar
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.