Completed
Push — trunk ( 5a98ee...c2d255 )
by SuperNova.WS
04:09
created

art_use()   D

Complexity

Conditions 35
Paths 140

Size

Total Lines 168
Code Lines 109

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 35
eloc 109
nc 140
nop 3
dl 0
loc 168
rs 4.0963
c 0
b 0
f 0

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
function art_use(&$user, &$planetrow, $unit_id)
4
{
5
  global $lang;
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...
6
7
  if(!in_array($unit_id, sn_get_groups('artifacts')))
8
  {
9
    return;
10
  }
11
12
  sn_db_transaction_start();
13
  $user = db_user_by_id($user['id'], true);
0 ignored issues
show
Deprecated Code introduced by
The function db_user_by_id() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

13
  $user = /** @scrutinizer ignore-deprecated */ db_user_by_id($user['id'], true);
Loading history...
14
15
  $unit_level = $artifact_level_old = mrc_get_level($user, array(), $unit_id, true);
16
  if($unit_level > 0)
17
  {
18
    $db_changeset = array();
19
    switch($unit_id)
20
    {
21
      case ART_LHC:
22
      case ART_HOOK_SMALL:
23
      case ART_HOOK_MEDIUM:
24
      case ART_HOOK_LARGE:
25
        $has_moon = DBStaticPlanet::db_planet_by_parent($planetrow['id'], true, '`id`');
26
        if($planetrow['planet_type'] == PT_PLANET && !$has_moon['id'])
27
        {
28
          $unit_level--;
29
          switch ($unit_id) {
30
            case ART_HOOK_SMALL:
31
              $moonSize = \Universe::MOON_MIN_SIZE;
32
            break;
33
            case ART_HOOK_MEDIUM:
34
              $moonSize = Universe::moonSizeRandom();
35
            break;
36
            case ART_HOOK_LARGE:
37
              $moonSize = \Universe::MOON_MAX_SIZE;
38
            break;
39
            case ART_LHC:
40
            default:
41
              $moonSize = Universe::moonRollSize($planetrow['debris_metal'] + $planetrow['debris_crystal']);
42
            break;
43
          }
44
45
          if($moonSize)
46
          {
47
            $new_moon_row = uni_create_moon($planetrow['galaxy'], $planetrow['system'], $planetrow['planet'], $user['id'], $moonSize);
48
            $message = sprintf($lang['art_moon_create'][$unit_id], $new_moon_row['name'], uni_render_coordinates($planetrow), HelperString::numberFloorAndFormat($moonSize));
49
          }
50
          else
51
          {
52
            $message = $lang['art_lhc_moon_fail'];
53
          }
54
          msg_send_simple_message($user['id'], 0, 0, MSG_TYPE_ADMIN, $lang['art_lhc_from'], $lang['art_lhc_subj'], $message);
55
        }
56
        else
57
        {
58
          $message = $lang['art_moon_exists'];
59
        }
60
      break;
61
62
      case ART_RCD_SMALL:
63
      case ART_RCD_MEDIUM:
64
      case ART_RCD_LARGE:
65
        $planetrow = DBStaticPlanet::db_planet_by_id($planetrow['id'], true);
66
        if($planetrow['planet_type'] != PT_PLANET)
67
        {
68
          $message = $lang['art_rcd_err_moon'];
69
          break;
70
        }
71
72
        $que = que_get($user['id'], $planetrow['id'], QUE_STRUCTURES, false);
73
        if(!empty($que['items']))
74
        {
75
          $message = $lang['art_rcd_err_que'];
76
          break;
77
        }
78
79
        $artifact_deploy = get_unit_param($unit_id, P_DEPLOY);
80
81
        $sectors_used = 0;
82
        foreach($artifact_deploy as $deploy_unit_id => $deploy_unit_level)
83
        {
84
          if(!($levels_deployed = max(0, $deploy_unit_level - mrc_get_level($user, $planetrow, $deploy_unit_id, true, true))))
85
            continue;
86
          $sectors_used += $levels_deployed;
87
          $db_changeset['unit'][] = OldDbChangeSet::db_changeset_prepare_unit($deploy_unit_id, $levels_deployed, $user, $planetrow['id']);
88
        }
89
90
        if($sectors_used == 0)
91
        {
92
          $message = $lang['art_rcd_err_no_sense'];
93
          break;
94
        }
95
        $unit_level--;
96
        DBStaticPlanet::db_planet_set_by_id($planetrow['id'], "`field_current` = `field_current` + {$sectors_used}");
97
        $message = sprintf($lang['art_rcd_ok'], $lang['tech'][$unit_id], $planetrow['name'], uni_render_coordinates($planetrow));
98
        msg_send_simple_message($user['id'], 0, 0, MSG_TYPE_QUE, $lang['art_rcd_subj'], $lang['art_rcd_subj'], $message);
99
      break;
100
101
      case ART_HEURISTIC_CHIP:
102
        $que_item = null;
103
        $que = que_get($user['id'], $planetrow['id'], QUE_RESEARCH, true);
104
        $current_que = &$que['ques'][QUE_RESEARCH][$user['id']][0];
105
        if(!empty($current_que))
106
        {
107
          reset($current_que);
108
          $que_item = &$que['ques'][QUE_RESEARCH][$user['id']][0][key($current_que)];
109
        }
110
111
        if(!empty($que_item) && $que_item['que_time_left'] > 60)
112
        {
113
          $unit_level--;
114
          $old_time = $que_item['que_time_left'];
115
          $que_item['que_time_left'] = $que_item['que_time_left'] > PERIOD_HOUR ? ceil($que_item['que_time_left'] / 2) : 0;
116
          DBStaticQue::db_que_set_time_left_by_id($que_item['que_id'], $que_item['que_time_left']);
117
          $message = sprintf($lang['art_heurestic_chip_ok'], $lang['tech'][$que_item['que_unit_id']], $que_item['que_unit_level'], sys_time_human($old_time - $que_item['que_time_left']));
118
          msg_send_simple_message($user['id'], 0, 0, MSG_TYPE_QUE, $lang['art_heurestic_chip_subj'], $lang['art_heurestic_chip_subj'], $message);
119
        }
120
        else
121
        {
122
          $message = $lang['art_heurestic_chip_no_research'];
123
        }
124
      break;
125
126
      case ART_NANO_BUILDER:
127
        $planetrow = DBStaticPlanet::db_planet_by_id($planetrow['id'], true);
128
        $que_item = null;
129
        $que = que_get($user['id'], $planetrow['id'], QUE_STRUCTURES, true);
130
        $current_que = &$que['ques'][QUE_STRUCTURES][$user['id']][$planetrow['id']];
131
        // $que_item = &$que['que'][QUE_STRUCTURES][0];
0 ignored issues
show
Unused Code Comprehensibility introduced by
71% 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...
132
        if(!empty($current_que))
133
        {
134
          reset($current_que);
135
          $que_item = &$que['ques'][QUE_STRUCTURES][$user['id']][$planetrow['id']][key($current_que)];
136
        }
137
138
        if(isset($que_item) && $que_item['que_time_left'] > 60)
139
        {
140
          $unit_level--;
141
          $old_time = $que_item['que_time_left'];
142
          $que_item['que_time_left'] = $que_item['que_time_left'] > PERIOD_HOUR ? ceil($que_item['que_time_left'] / 2) : 0;
143
          DBStaticQue::db_que_set_time_left_by_id($que_item['que_id'], $que_item['que_time_left']);
144
          $message = sprintf($lang['art_nano_builder_ok'], $que_item['que_unit_mode'] == BUILD_CREATE ? $lang['art_nano_builder_build'] : $lang['art_nano_builder_destroy'],
145
            $lang['tech'][$que_item['que_unit_id']], $que_item['que_unit_level'], $planetrow['name'], uni_render_coordinates($planetrow), sys_time_human($old_time - $que_item['que_time_left'])
146
          );
147
          msg_send_simple_message($user['id'], 0, 0, MSG_TYPE_QUE, $lang['art_nano_builder_subj'], $lang['art_nano_builder_subj'], $message);
148
        }
149
        else
150
        {
151
          $message = $lang['art_nano_builder_no_que'];
152
        }
153
      break;
154
155
    }
156
    if($unit_level != $artifact_level_old)
157
    {
158
      $db_changeset['unit'][] = OldDbChangeSet::db_changeset_prepare_unit($unit_id, $unit_level - $artifact_level_old, $user);
159
      OldDbChangeSet::db_changeset_apply($db_changeset);
160
    }
161
  }
162
  else
163
  {
164
    $message = $lang['art_err_no_artifact'];
165
  }
166
167
  sn_db_transaction_commit();
168
  messageBox($message, "{$lang['tech'][UNIT_ARTIFACTS]} - {$lang['tech'][$unit_id]}",
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $message does not seem to be defined for all execution paths leading up to this point.
Loading history...
169
    ($request_uri = sys_get_param_str_unsafe('REQUEST_URI')) ? $request_uri : ('artifacts' . DOT_PHP_EX . '#' . $unit_id),
170
  5);
171
}
172