Completed
Push — work-fleets ( 76d0ff...f3b03d )
by SuperNova.WS
05:19
created

includes/includes/market_trader.inc::eco_mrk_trader()   F

Complexity

Conditions 25
Paths 1356

Size

Total Lines 131
Code Lines 85

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
cc 25
eloc 85
c 5
b 0
f 0
nc 1356
nop 2
dl 0
loc 131
rs 2

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
3
if((!defined('SN_IN_MARKET') || SN_IN_MARKET !== true) && (!defined('SN_IN_ALLY') || SN_IN_ALLY !== true)) {
4
  classSupernova::$debug->error("Attempt to call market page mode {$mode} directly - not from market.php", 'Forbidden', 403);
5
}
6
7
function eco_mrk_trader($user, $planetrow = null) {
8
  global $mode;
9
10
  $template = gettemplate('market_trader', true);
11
12
  $intError = MARKET_DEAL;
13
  $planetrow = isset($planetrow) ? $planetrow : $user;
14
15
  global $page_title;
16
  $classLocale = classLocale::$lang;
17
18
  lng_include('market');
19
20
  $rates = array(
21
    RES_METAL => classSupernova::$config->rpg_exchange_metal,
22
    RES_CRYSTAL => classSupernova::$config->rpg_exchange_crystal,
23
    RES_DEUTERIUM => classSupernova::$config->rpg_exchange_deuterium,
24
    RES_DARK_MATTER => classSupernova::$config->rpg_exchange_darkMatter
25
  );
26
27
  // $dm_db_name = pname_resource_name(RES_DARK_MATTER);
28
  $exchangeTo = in_array($exchangeTo = sys_get_param_int('exchangeTo'), sn_get_groups('resources_trader')) ? $exchangeTo : 0;
29
  if($exchangeTo && is_array($tradeList = $_POST['spend'])) {
30
    $value = 0;
31
    $qry = array();
32
33
    sn_db_transaction_start();
34
    if($planetrow['id_owner']) {
35
      $global_data = sys_o_get_updated($user, $planetrow, SN_TIME_NOW);
36
      $planetrow = $global_data['planet'];
37
    }
38
    else {
39
      // Locking user record
40
      $user = db_user_by_id($user['id'], true);
41
    }
42
43
    foreach(sn_get_groups('resources_trader') as $resource_id) {
44
      $amount = floatval($tradeList[$resource_id]);
45
      if($amount < 0) {
46
        classSupernova::$debug->error('Trying to supply negative resource amount on Black Market Page', 'Hack Attempt', 305);
47
      }
48
49
      if($resource_id == RES_DARK_MATTER && $exchangeTo == RES_DARK_MATTER) {
50
        continue;
51
      }
52
53
      $resource_db_name = pname_resource_name($resource_id);
54
      if($exchangeTo == RES_DARK_MATTER) {
55
        $sign = '+';
56
        $amount = floor($tradeList[RES_DARK_MATTER] / 3 * $rates[RES_DARK_MATTER] / $rates[$resource_id]);
57
        $value += $amount;
58
      } else {
59
        $value += floor($amount * $rates[$resource_id] / $rates[$exchangeTo]);
60
        if($resource_id == RES_DARK_MATTER) {
61
          $amount = 0;
62
        } else {
63
          if(mrc_get_level($user, $planetrow, $resource_id, true) < $amount) {
64
            $intError = MARKET_NO_RESOURCES;
65
            break;
66
          }
67
68
          $sign = '-';
69
        }
70
      }
71
72
      if($amount) {
73
        $qry[] = "`{$resource_db_name}` = `{$resource_db_name}` {$sign} {$amount}";
0 ignored issues
show
Bug introduced by
The variable $sign 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...
74
      }
75
    }
76
77
    if($exchangeTo != RES_DARK_MATTER) {
78
      $amount = floor($value);
79
      $exchange_to_db_name = pname_resource_name($exchangeTo);
80
      $qry[] = "`{$exchange_to_db_name}` = `{$exchange_to_db_name}` + {$amount}";
81
    }
82
83
    $operation_cost = classSupernova::$config->rpg_cost_trader * ($exchangeTo == RES_DARK_MATTER ? 3 : 1) + $tradeList[RES_DARK_MATTER];
84
85
    $intError = $value <= 0 ? MARKET_ZERO_DEAL : $intError;
86
    $intError = mrc_get_level($user, null, RES_DARK_MATTER) < $operation_cost ? MARKET_NO_DM : $intError;
87
88
    if($intError == MARKET_DEAL) {
89
      $qry = implode(', ', $qry);
90
      $table = $planetrow['id_owner'] ? 'planets' : 'users';
91
92
      doquery("UPDATE {{{$table}}} SET {$qry} WHERE `id` = {$planetrow['id']} LIMIT 1;");
93
      rpg_points_change($user['id'], RPG_MARKET, -$operation_cost, "Using Black Market page {$classLocale['eco_mrk_trader']}");
94
      sn_db_transaction_commit();
95
96
      $intError = MARKET_DEAL_TRADE;
97
      $_SERVER['REQUEST_URI'] = ($has_message = strpos($_SERVER['REQUEST_URI'], '&message=')) ? substr($_SERVER['REQUEST_URI'], 0, $has_message) : $_SERVER['REQUEST_URI'];
98
      header("Location: {$_SERVER['REQUEST_URI']}&message={$intError}");
99
      ob_end_flush();
100
      die();
101
    }
102
    sn_db_transaction_rollback();
103
    $template->assign_block_vars('result', array(
104
      'STATUS' => $intError == MARKET_DEAL ? ERR_NONE : ERR_ERROR,
105
      'MESSAGE' => classLocale::$lang['eco_mrk_errors'][$intError],
106
    ));
107
  }
108
109
  $template->assign_vars(array(
110
    'EXCHANGE_TO_RESOURCE_ID' => $exchangeTo,
111
  ));
112
113
  foreach(sn_get_groups('resources_trader') as $resource_id) {
114
    if($resource_id == RES_DARK_MATTER) {
115
      $amount = floor(mrc_get_level($user, null, RES_DARK_MATTER) - classSupernova::$config->rpg_cost_trader);
116
    } else {
117
      $amount = floor(mrc_get_level($user, $planetrow, $resource_id));
118
    }
119
120
    $template->assign_block_vars('resources', array(
121
      'ID'         => $resource_id,
122
      'NAME'       => classLocale::$lang['tech'][$resource_id],
123
      'AVAIL'      => $amount,
124
      'AVAIL_TEXT' => pretty_number($amount),
125
      'SPENT'      => ($intError == MARKET_DEAL) ? 0 : max(0, $tradeList[$resource_id]),
0 ignored issues
show
Bug introduced by
The variable $tradeList 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...
126
      'RATE'       => $rates[$resource_id],
127
    ));
128
  }
129
130
  $template->assign_vars(array(
131
    'rpg_cost_trader'   => classSupernova::$config->rpg_cost_trader,
132
    // 'message' => $message,
133
    'MODE' => $mode,
134
  ));
135
136
  display($template, $page_title);
137
}
138