| Conditions | 22 |
| Paths | 768 |
| Total Lines | 143 |
| Code Lines | 98 |
| Lines | 5 |
| Ratio | 3.5 % |
| 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 |
||
| 43 | function b_donations_donatometer_show($options) |
||
| 44 | { |
||
| 45 | global $xoopsDB; |
||
| 46 | $xdBlockDir = basename(dirname(__DIR__)); |
||
| 47 | |||
| 48 | $tr_config = configInfo(); |
||
| 49 | //determine the currency |
||
| 50 | $PP_CURR_CODE = explode('|', $tr_config['pp_curr_code']); // [USD,GBP,JPY,CAD,EUR] |
||
| 51 | $PP_CURR_CODE = $PP_CURR_CODE[0]; |
||
| 52 | $currencySign = defineCurrency($PP_CURR_CODE); |
||
| 53 | |||
| 54 | $block = array(); |
||
| 55 | |||
| 56 | $swingd = $tr_config['swing_day']; |
||
| 57 | if (($swingd < 0) or ($swingd > 31)) { |
||
| 58 | $swingd = 6; |
||
| 59 | } |
||
| 60 | $dmshowdate = $options[1]; |
||
| 61 | $dmshowamt = $options[2]; |
||
| 62 | |||
| 63 | if (is_numeric($options[0]) && $options[0] > 0) { |
||
| 64 | $dmlen = $options[0]; |
||
| 65 | } elseif (is_numeric($dmlen) && $dmlen == 0) { |
||
| 66 | $dmlen = -1; |
||
| 67 | } else { |
||
| 68 | $dmlen = 10; |
||
| 69 | } |
||
| 70 | |||
| 71 | // Check the current day against the swing day to execute the proper query |
||
| 72 | if (date('d') >= $swingd) { |
||
| 73 | $query_Recordset1 = 'SELECT count(mc_gross) AS count, sum(mc_gross) AS gross, sum(' . "mc_gross-mc_fee) AS net, date_format( now(),'%M') AS mon, date_format( subdate( date_format( adddate(" . "now(), INTERVAL 1 MONTH),'%Y-%c-1'), INTERVAL 1 DAY), '%b %e') AS due_by, date_format(now(),'%b') AS " . 'mon_short FROM ' . $xoopsDB->prefix('donations_transactions') . ' WHERE (payment_date >= date_format(' . "now(),'%Y-%m-" . $swingd . "'))"; |
||
| 74 | |||
| 75 | $query_Recordset3 = 'select custom as muser_id, option_selection1 as showname, ' . "date_format( payment_date, '%b-%e') as date, concat('" . $currencySign . "',sum(mc_gross)) as amt " . 'from ' . $xoopsDB->prefix('donations_transactions') . ' where (payment_date >= date_format( ' . "now(), '%Y-%m-" . $swingd . "')) group by txn_id order by payment_date desc"; |
||
| 76 | } else { |
||
| 77 | $query_Recordset1 = 'select count(mc_gross) as count, sum(mc_gross) as gross, sum(mc_gross -' . ' mc_fee) as net, date_format( subdate( now(), interval ' . $swingd . " day), '%M') as mon," . " 'Now!' as due_by, date_format( subdate( now(), interval " . $swingd . " day), '%b') as mon_short" . ' from ' . $xoopsDB->prefix('donations_transactions') . " where (payment_date < date_format( now(), '%Y-%m-" . $swingd . "')" . ') and payment_date > date_format( subdate( now(), interval ' . $swingd . " day), '%Y-%m-" . $swingd . "')"; |
||
| 78 | |||
| 79 | $query_Recordset3 = 'select custom as muser_id, option_selection1 as showname, ' . "date_format( payment_date, '%b-%e') as date, concat('" . $currencySign . "', sum(mc_gross)) as amt " . 'from ' . $xoopsDB->prefix('donations_transactions') . ' where (payment_date < date_format(now(),' . " '%Y-%m-" . $swingd . "')) and payment_date > date_format( subdate( now(),interval " . $swingd . ' ' . "day), '%Y-%m-" . $swingd . "') group by txn_id order by payment_date desc"; |
||
| 80 | } |
||
| 81 | |||
| 82 | // Get the donation totals |
||
| 83 | $Recordset1 = $xoopsDB->query($query_Recordset1); |
||
| 84 | $row_Recordset1 = $xoopsDB->fetchArray($Recordset1); |
||
| 85 | //If there are not records, then get "null" data |
||
| 86 | if (!$row_Recordset1) { |
||
| 87 | $query_Recordset1 = "select '0' as count, '0' as gross, '0' as net, date_format( now()," . "'%M') as mon, date_format( subdate( date_format( adddate( now(), interval 1 month), '%Y-%c-1')," . " interval 1 day), '%b %e') as due_by, date_format( now(), '%b') as mon_short from " . ' ' . $xoopsDB->prefix('donations_transactions') . ''; |
||
| 88 | $Recordset1 = $xoopsDB->query($query_Recordset1); |
||
| 89 | $row_Recordset1 = $xoopsDB->fetchArray($Recordset1); |
||
| 90 | } |
||
| 91 | // Get the goal |
||
| 92 | $query_Recordset2 = 'SELECT * FROM ' . $xoopsDB->prefix('donations_config') . " WHERE name='month_goal' AND subtype='" . $row_Recordset1['mon_short'] . "'"; |
||
| 93 | $Recordset2 = $xoopsDB->query($query_Recordset2); |
||
| 94 | $row_Recordset2 = $xoopsDB->fetchArray($Recordset2); |
||
| 95 | |||
| 96 | // Set our remaining template vars |
||
| 97 | if (!$row_Recordset1['mon']) { |
||
| 98 | $DM_MON = date('F'); |
||
| 99 | } else { |
||
| 100 | $DM_MON = $row_Recordset1['mon']; |
||
| 101 | } |
||
| 102 | $difference = $row_Recordset1['net'] - $row_Recordset2['value']; |
||
| 103 | $DM_GOAL = sprintf($currencySign . '%.02f', $row_Recordset2['value']); |
||
| 104 | $DM_DUE = $row_Recordset1['due_by']; |
||
| 105 | $DM_NUM = $row_Recordset1['count']; |
||
| 106 | $DM_GROSS = sprintf($currencySign . '%.02f', $row_Recordset1['gross']); |
||
| 107 | $DM_NET = sprintf($currencySign . '%.02f', $row_Recordset1['net']); |
||
| 108 | $DM_LEFT = sprintf($currencySign . '%.02f', $row_Recordset2['value'] - $row_Recordset1['net']); |
||
| 109 | $DM_BUTTON = $options[3]; |
||
| 110 | $DM_BUTT_DIMS = ''; |
||
| 111 | if (is_numeric($options[4])) { |
||
| 112 | $DM_BUTT_DIMS .= 'width=' . $options[4] . ' '; |
||
| 113 | } |
||
| 114 | if (is_numeric($options[5])) { |
||
| 115 | $DM_BUTT_DIMS .= 'height=' . $options[5] . ' '; |
||
| 116 | } |
||
| 117 | |||
| 118 | // Load the template |
||
| 119 | $block['DM_BUTTON'] = $DM_BUTTON; |
||
| 120 | $block['DM_BUTT_DIMS'] = $DM_BUTT_DIMS; |
||
| 121 | $block['DM_MON'] = $DM_MON; |
||
| 122 | $block['DM_GOAL'] = $DM_GOAL; |
||
| 123 | $block['DM_DUE'] = $DM_DUE; |
||
| 124 | $block['DM_GROSS'] = $DM_GROSS; |
||
| 125 | $block['DM_NET'] = $DM_NET; |
||
| 126 | $block['DON_URL'] = XOOPS_URL . '/modules/' . $xdBlockDir . '/index.php'; |
||
| 127 | $show_don = 0; |
||
| 128 | // Do we want to display the donators? |
||
| 129 | if (is_numeric($dmlen) && $dmlen >= 0) { |
||
| 130 | $show_don = 1; |
||
| 131 | // Get the list of donators |
||
| 132 | $Recordset3 = $xoopsDB->query($query_Recordset3); |
||
| 133 | |||
| 134 | // List all the donators |
||
| 135 | $i = 0; |
||
| 136 | $var = ''; |
||
| 137 | while (($row_Recordset3 = $xoopsDB->fetchArray($Recordset3)) && ($i != $options[0])) { |
||
| 138 | // Refunded transactions will show up with $0 amount |
||
| 139 | if ($row_Recordset3['amt'] > "$0") { |
||
| 140 | $dmalign = 'center'; |
||
| 141 | $var .= "<tr><td style=\"width: 100%; text-align: {$dmalign};\" colspan=\"2\">\n"; |
||
| 142 | // Observe the user's wish regarding revealing their name |
||
| 143 | $muser_id = $row_Recordset3['muser_id']; |
||
| 144 | View Code Duplication | if (strcmp($row_Recordset3['showname'], 'Yes') == 0 && ($userfoin = mgetUserInfo($muser_id))) { |
|
| 145 | $var .= "<a href='" . XOOPS_URL . '/userinfo.php?uid=' . $userfoin->getVar('uid') . "'>" . $userfoin->getVar('uname') . "</a>\n"; |
||
| 146 | } else { |
||
| 147 | $var .= _MB_DON_ANONYMOUS_SHORT; |
||
| 148 | } |
||
| 149 | $var .= ' '; |
||
| 150 | if ($dmshowamt) { |
||
| 151 | $var .= '(' . $row_Recordset3['amt'] . ')'; |
||
| 152 | } |
||
| 153 | if ($dmshowdate) { |
||
| 154 | $var .= $row_Recordset3['date']; |
||
| 155 | } |
||
| 156 | $var .= "</td></tr>\n"; |
||
| 157 | } |
||
| 158 | ++$i; |
||
| 159 | } |
||
| 160 | } |
||
| 161 | |||
| 162 | if ($difference >= 0) { |
||
| 163 | $DM_OVERAGE = sprintf($currencySign . '%.02f', $difference); |
||
| 164 | $block['DM_REMAIN'] = _MB_DON_SURPLUS; |
||
| 165 | $block['DM_BALANCE'] = $DM_OVERAGE; |
||
| 166 | } else { |
||
| 167 | $block['DM_REMAIN'] = _MB_DON_LEFT2GO; |
||
| 168 | $block['DM_BALANCE'] = "<span style=\"color: #CC0000;\">{$DM_LEFT}</span>"; |
||
| 169 | } |
||
| 170 | |||
| 171 | // Define language constants |
||
| 172 | $block['DM_STAT'] = _MB_DON_STAT; |
||
| 173 | $block['DM_MONGOAL'] = _MB_DON_MONGOAL; |
||
| 174 | $block['DM_DUEDATE'] = _MB_DON_DUEDATE; |
||
| 175 | $block['DM_GROSSAMT'] = _MB_DON_GROSSAMT; |
||
| 176 | $block['DM_NETBAL'] = _MB_DON_NETBAL; |
||
| 177 | $block['DM_DONATIONS'] = _MB_DON_DONATIONS; |
||
| 178 | $block['DM_MAKEDON'] = _MB_DON_MAKEDON; |
||
| 179 | |||
| 180 | // Display block |
||
| 181 | $block['show_don'] = $show_don; |
||
| 182 | $block['content'] = $var; |
||
| 183 | |||
| 184 | return $block; |
||
| 185 | } |
||
| 186 | |||
| 220 |
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.