donat_o_meter.php ➔ b_donations_donatometer_edit()   B
last analyzed

Complexity

Conditions 5
Paths 16

Size

Total Lines 29
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 22
nc 16
nop 1
dl 0
loc 29
rs 8.439
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 43 and the first side effect is on line 34.

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.

Loading history...
2
/************************************************************************/
3
/* Donations - Paypal financial management module for Xoops 2           */
4
/* Copyright (c) 2016 XOOPS Project                                     */
5
/* http://dev.xoops.org/modules/xfmod/project/?group_id=1060            */
6
/* 
7
/************************************************************************/
8
/*                                                                      */
9
/* Based on NukeTreasury for PHP-Nuke - by Dave Lawrence AKA Thrash     */
10
/* NukeTreasury - Financial management for PHP-Nuke                     */
11
/* Copyright (c) 2004 by Dave Lawrence AKA Thrash                       */
12
/*                       [email protected]                         */
13
/*                       [email protected]                          */
14
/*                                                                      */
15
/************************************************************************/
16
/*                                                                      */
17
/* This program is free software; you can redistribute it and/or modify */
18
/* it under the terms of the GNU General Public License as published by */
19
/* the Free Software Foundation; either version 2 of the License.       */
20
/*                                                                      */
21
/* This program is distributed in the hope that it will be useful, but  */
22
/* WITHOUT ANY WARRANTY; without even the implied warranty of           */
23
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU     */
24
/* General Public License for more details.                             */
25
/*                                                                      */
26
/* You should have received a copy of the GNU General Public License    */
27
/* along with this program; if not, write to the Free Software          */
28
/* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  */
29
/* USA                                                                  */
30
/************************************************************************/
31
32
// defined('XOOPS_ROOT_PATH') || exit('XOOPS root path not defined');
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
33
34
$xdBlockDir = basename(dirname(__DIR__));
35
xoops_loadLanguage('main', $xdBlockDir);
36
37
include_once XOOPS_ROOT_PATH . "/modules/{$xdBlockDir}/include/functions.php";
38
39
/**
40
 * @param $options
41
 * @return array
42
 */
43
function b_donations_donatometer_show($options)
44
{
45
    global $xoopsDB;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
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]
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
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)) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as or instead of || is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
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) {
0 ignored issues
show
Bug introduced by
The variable $dmlen seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
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'];
0 ignored issues
show
Unused Code introduced by
$DM_NUM is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
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))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
145
                    $var .= "<a href='" . XOOPS_URL . '/userinfo.php?uid=' . $userfoin->getVar('uid') . "'>" . $userfoin->getVar('uname') . "</a>\n";
0 ignored issues
show
Bug introduced by
The method getVar cannot be called on $userfoin (of type boolean).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
146
                } else {
147
                    $var .= _MB_DON_ANONYMOUS_SHORT;
148
                }
149
                $var .= '&nbsp;';
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;
0 ignored issues
show
Bug introduced by
The variable $var does not seem to be defined for all execution paths leading up to this point.

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:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

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

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
183
184
    return $block;
185
}
186
187
/**
188
 * @param $options
189
 * @return string
190
 */
191
function b_donations_donatometer_edit($options)
192
{
193
    $form = _MB_DON_NUM_DONORS . ":&nbsp;<input type='text' name='options[0]' value='" . $options[0] . "'  size='4'/>";
194
    $form .= '<br />' . _MB_DON_REVEAL_DATES . ":&nbsp;<select size='1' name='options[1]'><option value='1'";
195
    if ($options[1] == 1) {
196
        $form .= ' selected';
197
    }
198
    $form .= ' />' . _YES . "</option><option value='0'";
199
    if ($options[1] == 0) {
200
        $form .= ' selected';
201
    }
202
    $form .= ' />' . _NO . '</option></select>';
203
    $form .= '<br />' . _MB_DON_REVEAL_AMOUNTS . ":&nbsp;<select size='1' name='options[2]'><option value='1'";
204
    if ($options[2] == 1) {
205
        $form .= ' selected';
206
    }
207
    $form .= ' />' . _YES . "</option><option value='0'";
208
    if ($options[2] == 0) {
209
        $form .= ' selected';
210
    }
211
    $form .= ' />' . _NO . '</option></select>';
212
    $form .= '<br />' . _MB_DON_BUTTON_URL . ':&nbsp;';
213
    $form .= "<input size='70' name='options[3]' type='text' value='" . $options[3] . "'>";
214
    $form .= '<br />' . _MB_DON_BUTTON_DIMS . ':&nbsp;';
215
    $form .= _MB_DON_WIDTH . "&nbsp;<input size='4' name='options[4]' type='text' value='" . $options[4] . "'>";
216
    $form .= '&nbsp;&nbsp;' . _MB_DON_WIDTH . "&nbsp;<input size='4' name='options[5]' type='text' value='" . $options[5] . "'>";
217
218
    return $form;
219
}
220