| Conditions | 24 | 
| Paths | 8736 | 
| Total Lines | 137 | 
| Code Lines | 90 | 
| Lines | 0 | 
| Ratio | 0 % | 
| Changes | 5 | ||
| Bugs | 0 | Features | 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  | 
            ||
| 3 | function qst_render_page() { | 
            ||
| 4 | global $user, $template;  | 
            ||
| 5 | |||
| 6 |   $user_id = sys_get_param_id('user_id', false); | 
            ||
| 
                                                                                                    
                        
                         | 
                |||
| 7 |   $mode = sys_get_param_str('mode'); | 
            ||
| 8 | |||
| 9 |   $quest_units_allowed = sn_get_groups(array('structures', 'tech', 'fleet', 'defense')); | 
            ||
| 10 |   $quest_reward_allowed = sn_get_groups('quest_rewards'); | 
            ||
| 11 | |||
| 12 |   $in_admin = defined('IN_ADMIN') && IN_ADMIN === true; | 
            ||
| 13 | |||
| 14 |   if ($in_admin) { | 
            ||
| 15 |     $quest_id = sys_get_param_id('id'); | 
            ||
| 16 |     $quest_name = sys_get_param_str_unsafe('QUEST_NAME'); | 
            ||
| 17 |     if (!empty($quest_name)) { | 
            ||
| 18 |       $quest_description = sys_get_param_str_unsafe('QUEST_DESCRIPTION'); | 
            ||
| 19 |       try { | 
            ||
| 20 |         $quest_rewards_list = sys_get_param('QUEST_REWARDS_LIST'); | 
            ||
| 21 | $quest_rewards = array();  | 
            ||
| 22 |         foreach ($quest_rewards_list as $quest_rewards_id => $quest_rewards_amount) { | 
            ||
| 23 |           if (!in_array($quest_rewards_id, $quest_reward_allowed)) { | 
            ||
| 24 | throw new Exception(classLocale::$lang['qst_adm_err_reward_type']);  | 
            ||
| 25 | }  | 
            ||
| 26 | |||
| 27 |           if ($quest_rewards_amount < 0) { | 
            ||
| 28 | throw new Exception(classLocale::$lang['qst_adm_err_reward_amount']);  | 
            ||
| 29 |           } elseif ($quest_rewards_amount > 0) { | 
            ||
| 30 |             $quest_rewards[] = "{$quest_rewards_id},{$quest_rewards_amount}"; | 
            ||
| 31 | }  | 
            ||
| 32 | }  | 
            ||
| 33 |         if (empty($quest_rewards)) { | 
            ||
| 34 | throw new Exception(classLocale::$lang['qst_adm_err_reward_empty']);  | 
            ||
| 35 | }  | 
            ||
| 36 | |||
| 37 |         $quest_rewards = implode(';', $quest_rewards); | 
            ||
| 38 | |||
| 39 |         $quest_unit_id = sys_get_param_int('QUEST_UNIT_ID'); | 
            ||
| 40 |         if (!in_array($quest_unit_id, $quest_units_allowed)) { | 
            ||
| 41 | throw new Exception(classLocale::$lang['qst_adm_err_unit_id']);  | 
            ||
| 42 | }  | 
            ||
| 43 | |||
| 44 |         $quest_unit_amount = sys_get_param_float('QUEST_UNIT_AMOUNT'); | 
            ||
| 45 |         if ($quest_unit_amount <= 0) { | 
            ||
| 46 | throw new Exception(classLocale::$lang['qst_adm_err_unit_amount']);  | 
            ||
| 47 | }  | 
            ||
| 48 |         $quest_conditions = "{$quest_unit_id},{$quest_unit_amount}"; | 
            ||
| 49 | |||
| 50 | // TODO: Change quest type  | 
            ||
| 51 | $quest_type = 0;  | 
            ||
| 52 | |||
| 53 |         if ($mode == 'edit') { | 
            ||
| 54 | $quest_name = db_escape($quest_name);  | 
            ||
| 55 | $quest_description = db_escape($quest_description);  | 
            ||
| 56 | db_quest_update($quest_name, $quest_type, $quest_description, $quest_conditions, $quest_rewards, $quest_id);  | 
            ||
| 57 |         } else { | 
            ||
| 58 |           sn_db_perform('{{quest}}', array( | 
            ||
| 59 | 'quest_name' => $quest_name,  | 
            ||
| 60 | 'quest_type' => $quest_type,  | 
            ||
| 61 | 'quest_description' => $quest_description,  | 
            ||
| 62 | 'quest_conditions' => $quest_conditions,  | 
            ||
| 63 | 'quest_rewards' => $quest_rewards,  | 
            ||
| 64 | ));  | 
            ||
| 65 | }  | 
            ||
| 66 | |||
| 67 | // TODO: Add mass mail for new quests  | 
            ||
| 68 |       } catch (Exception $e) { | 
            ||
| 69 | message($e->getMessage(), classLocale::$lang['sys_error']);  | 
            ||
| 70 | }  | 
            ||
| 71 | |||
| 72 | $mode = '';  | 
            ||
| 73 | };  | 
            ||
| 74 | |||
| 75 |     switch ($mode) { | 
            ||
| 76 | case 'del':  | 
            ||
| 77 | db_quest_delete($quest_id);  | 
            ||
| 78 | $mode = '';  | 
            ||
| 79 | break;  | 
            ||
| 80 | |||
| 81 | case 'edit':  | 
            ||
| 82 |         $template->assign_var('QUEST_ID', $quest_id); | 
            ||
| 83 | |||
| 84 | case 'copy':  | 
            ||
| 85 | $quest = db_quest_get($quest_id);  | 
            ||
| 86 | break;  | 
            ||
| 87 | }  | 
            ||
| 88 | $query = db_quest_count();  | 
            ||
| 89 |     classSupernova::$config->db_saveItem('quest_total', $query['count']); | 
            ||
| 90 |   } elseif (!$user_id) { | 
            ||
| 91 | $user_id = $user['id'];  | 
            ||
| 92 | }  | 
            ||
| 93 | |||
| 94 | $quest_list = qst_get_quests($user_id);  | 
            ||
| 95 | $template->assign_vars(array(  | 
            ||
| 96 | 'AUTHLEVEL' => $user['authlevel'],  | 
            ||
| 97 | 'TOTAL' => count($quest_list),  | 
            ||
| 98 | 'mode' => $mode,  | 
            ||
| 99 | 'USER_ID' => $user_id,  | 
            ||
| 100 | 'IN_ADMIN' => $in_admin,  | 
            ||
| 101 | ));  | 
            ||
| 102 | |||
| 103 |   if (!empty($quest)) { | 
            ||
| 104 | $quest_templatized = qst_templatize(qst_quest_parse($quest));  | 
            ||
| 105 |   } else { | 
            ||
| 106 | $quest_templatized['quest_rewards_list'] = array();  | 
            ||
| 107 | }  | 
            ||
| 108 | |||
| 109 |   foreach ($quest_reward_allowed as $unit_id) { | 
            ||
| 110 | $found = false;  | 
            ||
| 111 |     foreach ($quest_templatized['quest_rewards_list'] as $quest_templatized_reward) { | 
            ||
| 112 |       if ($quest_templatized_reward['ID'] == $unit_id) { | 
            ||
| 113 | $found = true;  | 
            ||
| 114 | break;  | 
            ||
| 115 | }  | 
            ||
| 116 | }  | 
            ||
| 117 | |||
| 118 |     if (!$found) { | 
            ||
| 119 | $quest_templatized['quest_rewards_list'][$unit_id] = array(  | 
            ||
| 120 | 'ID' => $unit_id,  | 
            ||
| 121 | 'NAME' => classLocale::$lang['tech'][$unit_id],  | 
            ||
| 122 | 'AMOUNT' => 0,  | 
            ||
| 123 | );  | 
            ||
| 124 | }  | 
            ||
| 125 | }  | 
            ||
| 126 | |||
| 127 | qst_assign_to_template($template, $quest_templatized);  | 
            ||
| 128 | |||
| 129 |   foreach ($quest_list as $quest_data) { | 
            ||
| 130 | qst_assign_to_template($template, qst_templatize($quest_data, true), 'quest');  | 
            ||
| 131 | }  | 
            ||
| 132 | |||
| 133 |   foreach ($quest_units_allowed as $unit_id) { | 
            ||
| 134 |     $template->assign_block_vars('allowed_unit', array( | 
            ||
| 135 | 'ID' => $unit_id,  | 
            ||
| 136 | 'NAME' => classLocale::$lang['tech'][$unit_id],  | 
            ||
| 137 | ));  | 
            ||
| 138 | }  | 
            ||
| 139 | }  | 
            ||
| 140 | |||
| 321 | 
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: