Conditions | 20 |
Paths | 2892 |
Total Lines | 201 |
Code Lines | 109 |
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 |
||
142 | function xml2html ($xml, $title = NULL, $xsl = 'engine.xsl') |
||
143 | { |
||
144 | debug_write_log(DEBUG_TRACE, '[xml2html]'); |
||
145 | debug_write_log(DEBUG_DUMP, '[xml2html] $title = ' . $title); |
||
146 | |||
147 | // a page is being generated |
||
148 | if (!is_null($title)) |
||
149 | { |
||
150 | // generate page parameters |
||
151 | |||
152 | $params = array('title' => $title, |
||
153 | 'version' => ustrprocess(get_html_resource(RES_VERSION_X_ID), VERSION), |
||
154 | 'username' => isset($_SESSION[VAR_FULLNAME]) ? ustr2html($_SESSION[VAR_FULLNAME]) : get_html_resource(RES_GUEST_ID), |
||
155 | 'logout' => get_html_resource(get_user_level() == USER_LEVEL_GUEST ? RES_LOGIN_ID : RES_LOGOUT_ID), |
||
156 | 'search' => get_html_resource(RES_SEARCH_ID)); |
||
157 | |||
158 | $script = '<script>' |
||
159 | . 'function onLogoutButton()' |
||
160 | . '{' |
||
161 | . (get_user_level() == USER_LEVEL_GUEST |
||
162 | ? 'window.open("../logon/index.php", "_parent");' |
||
163 | : sprintf('jqConfirm("%s","%s","%s","%s","logout()")', |
||
164 | get_html_resource(RES_QUESTION_ID), |
||
165 | get_html_resource(RES_CONFIRM_LOGOUT_ID), |
||
166 | get_html_resource(RES_OK_ID), |
||
167 | get_html_resource(RES_CANCEL_ID))) |
||
168 | . '}' |
||
169 | . '</script>'; |
||
170 | |||
171 | if (MAINTENANCE_BANNER) |
||
172 | { |
||
173 | list($year, $month, $day) = explode('-', MAINTENANCE_START_DATE); |
||
174 | list($hour, $minute) = explode(':', MAINTENANCE_START_TIME); |
||
175 | |||
176 | $date1 = mktime($hour, $minute, 0, $month, $day, $year); |
||
177 | |||
178 | list($year, $month, $day) = explode('-', MAINTENANCE_FINISH_DATE); |
||
179 | list($hour, $minute) = explode(':', MAINTENANCE_FINISH_TIME); |
||
180 | |||
181 | $date2 = mktime($hour, $minute, 0, $month, $day, $year); |
||
182 | |||
183 | if (version_compare(PHP_VERSION, '5.1.3') >= 0) |
||
184 | { |
||
185 | $timezone = date('P'); |
||
186 | } |
||
187 | else |
||
188 | { |
||
189 | $timezone = date('O'); |
||
190 | $timezone = usubstr($timezone, 0, ustrlen($timezone) - 2) . ':' . usubstr($timezone, ustrlen($timezone) - 2); |
||
191 | } |
||
192 | |||
193 | $params['banner'] = ustrprocess(get_html_resource(RES_BANNER_ID), get_datetime($date1), get_datetime($date2), 'GMT' . $timezone); |
||
194 | } |
||
195 | |||
196 | $header = '<?xml version="1.0" encoding="UTF-8"?>' |
||
197 | . '<page'; |
||
198 | |||
199 | foreach ($params as $name => $value) |
||
200 | { |
||
201 | $header .= sprintf(' %s="%s"', $name, $value); |
||
202 | } |
||
203 | |||
204 | $header .= '>'; |
||
205 | |||
206 | $theme = isset($_SESSION[VAR_THEME_NAME]) ? $_SESSION[VAR_THEME_NAME] : THEME_DEFAULT; |
||
207 | |||
208 | $header .= '<css>../themes/css.php?name=' . $theme . '/jquery-ui.css</css>' |
||
209 | . '<css>../themes/css.php?name=' . $theme . '/jquery.jqplot.css</css>' |
||
210 | . '<css>../themes/css.php?name=' . $theme . '/etraxis.css</css>' |
||
211 | . '<css>../themes/css.php?name=' . $theme . '/list.css</css>' |
||
212 | . '<css>../themes/css.php?name=' . $theme . '/combobox.css</css>' |
||
213 | . '<css>../themes/css.php?name=' . $theme . '/buttons.css</css>'; |
||
214 | |||
215 | // generate main menu |
||
216 | |||
217 | $menu = sprintf('<mainmenu logo="../%s" site="%s">', COMPANY_LOGO, COMPANY_SITE); |
||
218 | |||
219 | if (get_user_level() == USER_LEVEL_GUEST) |
||
220 | { |
||
221 | $menu .= '<menuitem url="../records/index.php">' . get_html_resource(RES_RECORDS_ID) . '</menuitem>' |
||
222 | . '<menuitem url="../projects/index.php">' . get_html_resource(RES_PROJECTS_ID) . '</menuitem>'; |
||
223 | } |
||
224 | else |
||
225 | { |
||
226 | $menu .= '<menuitem url="../records/index.php">' . get_html_resource(RES_RECORDS_ID) . '</menuitem>' |
||
227 | . '<menuitem url="../filters/index.php">' . get_html_resource(RES_FILTERS_ID) . '</menuitem>' |
||
228 | . '<menuitem url="../views/index.php">' . get_html_resource(RES_VIEWS_ID) . '</menuitem>'; |
||
229 | |||
230 | if (EMAIL_NOTIFICATIONS_ENABLED) |
||
231 | { |
||
232 | $menu .= '<menuitem url="../subscriptions/index.php">' . get_html_resource(RES_SUBSCRIPTIONS_ID) . '</menuitem>'; |
||
233 | |||
234 | if (can_reminder_be_created()) |
||
235 | { |
||
236 | $menu .= '<menuitem url="../reminders/index.php">' . get_html_resource(RES_REMINDERS_ID) . '</menuitem>'; |
||
237 | } |
||
238 | } |
||
239 | |||
240 | $menu .= '<menuitem url="../projects/index.php">' . get_html_resource(RES_PROJECTS_ID) . '</menuitem>'; |
||
241 | |||
242 | if (get_user_level() == USER_LEVEL_ADMIN) |
||
243 | { |
||
244 | $menu .= '<menuitem url="../accounts/index.php">' . get_html_resource(RES_ACCOUNTS_ID) . '</menuitem>' |
||
245 | . '<menuitem url="../groups/index.php">' . get_html_resource(RES_GLOBAL_GROUPS_ID) . '</menuitem>'; |
||
246 | |||
247 | if (file_exists('../config/index.php')) |
||
248 | { |
||
249 | $menu .= '<menuitem url="../config/index.php">' . get_html_resource(RES_CONFIGURATION_ID) . '</menuitem>'; |
||
250 | } |
||
251 | } |
||
252 | |||
253 | $menu .= '<menuitem url="../settings/index.php">' . get_html_resource(RES_SETTINGS_ID) . '</menuitem>'; |
||
254 | } |
||
255 | |||
256 | $menu .= '</mainmenu>'; |
||
257 | |||
258 | // select requested tab, if one was specified |
||
259 | |||
260 | $tab = '<onready>' |
||
261 | . '$("#tabs").tabs("option", "selected", ' . (try_request('tab', 1) - 1) . ');' |
||
262 | . '$.blockUI.defaults.message = "' . get_html_resource(RES_PLEASE_WAIT_ID) . '";' |
||
263 | . '</onready>'; |
||
264 | |||
265 | // join all pieces together |
||
266 | |||
267 | $xml = $header |
||
268 | . $script |
||
269 | . $menu |
||
270 | . $tab |
||
271 | . $xml |
||
272 | . '</page>'; |
||
273 | } |
||
274 | // a part of page is being generated |
||
275 | else |
||
276 | { |
||
277 | // generate parameters |
||
278 | |||
279 | $params = array('msgboxTitle' => get_html_resource(RES_QUESTION_ID), |
||
280 | 'btnOk' => get_html_resource(RES_OK_ID), |
||
281 | 'btnCancel' => get_html_resource(RES_CANCEL_ID)); |
||
282 | |||
283 | $header = '<tab-content'; |
||
284 | |||
285 | foreach ($params as $name => $value) |
||
286 | { |
||
287 | $header .= sprintf(' %s="%s"', $name, $value); |
||
288 | } |
||
289 | |||
290 | $header .= '>'; |
||
291 | |||
292 | // join all pieces together |
||
293 | |||
294 | $xml = $header |
||
295 | . $xml |
||
296 | . '</tab-content>'; |
||
297 | } |
||
298 | |||
299 | // process resulted XML |
||
300 | |||
301 | $page = new DOMDocument(); |
||
302 | $xslt = new XSLTProcessor(); |
||
303 | |||
304 | $page->load(get_theme_xsl_file($xsl)); |
||
305 | $xslt->importStyleSheet($page); |
||
306 | $page->loadXML($xml); |
||
307 | |||
308 | $html = $xslt->transformToXML($page); |
||
309 | |||
310 | if (extension_loaded('libxml')) |
||
311 | { |
||
312 | $error = libxml_get_last_error(); |
||
313 | |||
314 | if ($error) |
||
315 | { |
||
316 | debug_write_log(DEBUG_ERROR, '[xml2html] XML Error = ' . $error->message); |
||
317 | debug_write_log(DEBUG_DUMP, '[xml2html] $xml = ' . $xml); |
||
318 | } |
||
319 | } |
||
320 | |||
321 | $html = str_replace('%br;', '<br/>', $html); |
||
322 | |||
323 | // built-in compressions: check whether required extensions is available and PHP compression is turned off |
||
324 | if (!extension_loaded('zlib') || ini_get('zlib.output_compression') || ini_get('output_handler')) |
||
325 | { |
||
326 | $html = str_replace('scripts/get.php?name=', 'scripts/', $html); |
||
327 | } |
||
328 | |||
329 | // workaround: some PHP configurations insert CDATA tags which break the output |
||
330 | $html = preg_replace('/<!\[cdata\[(.*?)\]\]>/isu', '$1', $html); |
||
331 | $html = preg_replace('/%3C!\[cdata\[(.*?)\]\]%3E/isu', '$1', $html); |
||
332 | |||
333 | mb_regex_encoding('UTF-8'); |
||
334 | |||
335 | $html = mb_eregi_replace('%([A-Za-z]+);', '&\1;', $html); |
||
336 | $html = mb_eregi_replace('%(#[0-9]{1,4});', '&\1;', $html); |
||
337 | $html = mb_eregi_replace('%(#x[0-9A-Fa-f]{1,4});', '&\1;', $html); |
||
338 | |||
339 | debug_write_log(DEBUG_PERFORMANCE, 'page size = ' . strlen($html)); |
||
340 | |||
341 | return $html; |
||
342 | } |
||
343 | |||
345 |
Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.
A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.