Completed
Push — work-fleets ( d7065d...1ee481 )
by SuperNova.WS
08:22
created

DBStaticUnit::dbUpdateOrInsertUnit()   B

Complexity

Conditions 6
Paths 24

Size

Total Lines 37
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 27
c 1
b 0
f 0
nc 24
nop 4
dl 0
loc 37
ccs 0
cts 33
cp 0
crap 42
rs 8.439
1
<?php
2
3
namespace DBStatic;
4
5
use classSupernova;
6
use mysqli_result;
7
8
class DBStaticUnit {
9
10
  public static function db_unit_time_restrictions($date = SN_TIME_NOW) {
11
    $date = is_numeric($date) ? "FROM_UNIXTIME({$date})" : "'{$date}'";
12
13
    return
14
      "(unit_time_start IS NULL OR unit_time_start <= {$date}) AND
15
    (unit_time_finish IS NULL OR unit_time_finish = '1970-01-01 03:00:00' OR unit_time_finish >= {$date})";
16
  }
17
18
19
  /**
20
   * @param int $user_id
21
   * @param int $location_type
22
   * @param int $location_id
23
   *
24
   * @return array|bool
25
   */
26
  public static function db_get_unit_list_by_location($user_id = 0, $location_type, $location_id) {
0 ignored issues
show
Unused Code introduced by
The parameter $user_id is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
27
    if (!($location_type = idval($location_type)) || !($location_id = idval($location_id))) {
28
      return false;
29
    }
30
31
    if (classSupernova::$gc->snCache->isUnitLocatorNotSet($location_type, $location_id)) {
0 ignored issues
show
Bug introduced by
The method isUnitLocatorNotSet does only exist in SnCache, but not in Closure.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
32
      $got_data = classSupernova::$gc->cacheOperator->db_get_record_list(LOC_UNIT, "unit_location_type = {$location_type} AND unit_location_id = {$location_id} AND " . DBStaticUnit::db_unit_time_restrictions());
0 ignored issues
show
Bug introduced by
The method db_get_record_list does only exist in SnDbCachedOperator, but not in Closure.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
33
      if (!empty($got_data) && is_array($got_data)) {
34
        foreach ($got_data as $unit_id => $unit_data) {
35
          classSupernova::$gc->snCache->setUnitLocatorByLocationAndIDs($location_type, $location_id, $unit_data);
0 ignored issues
show
Bug introduced by
The method setUnitLocatorByLocationAndIDs does only exist in SnCache, but not in Closure.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
36
        }
37
      }
38
    }
39
40
    $result = false;
41
    foreach (classSupernova::$gc->snCache->getUnitLocatorByFullLocation($location_type, $location_id) as $key => $value) {
0 ignored issues
show
Bug introduced by
The method getUnitLocatorByFullLocation does only exist in SnCache, but not in Closure.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
42
      $result[$key] = $value;
43
    }
44
45
    return $result;
46
  }
47
48
49
  /**
50
   * @param int    $user_id
51
   * @param        $location_type
52
   * @param        $location_id
53
   * @param int    $unit_snid
54
   * @param bool   $for_update
55
   * @param string $fields
56
   *
57
   * @return mixed
58
   */
59
  public static function db_get_unit_by_location($user_id = 0, $location_type, $location_id, $unit_snid = 0, $for_update = false, $fields = '*') {
0 ignored issues
show
Unused Code introduced by
The parameter $for_update is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $fields is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
60
    DBStaticUnit::db_get_unit_list_by_location($user_id, $location_type, $location_id);
61
62
    return classSupernova::$gc->snCache->getUnitLocator($location_type, $location_id, $unit_snid);
0 ignored issues
show
Bug introduced by
The method getUnitLocator does only exist in SnCache, but not in Closure.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
63
  }
64
65
  public static function db_unit_count_by_user_and_type_and_snid($user_id, $unit_type = 0, $unit_snid = 0) {
66
    $query = classSupernova::$db->doSelect(
67
      "SELECT unit_snid, sum(unit_level) as `qty`  FROM {{unit}} WHERE `unit_player_id` = {$user_id} " .
68
      ($unit_type ? "AND `unit_type` = {$unit_type} " : '') .
69
      ($unit_snid ? "AND `unit_snid` = {$unit_snid} " : '') .
70
      'GROUP BY `unit_snid`'
71
    );
72
    $result = array();
73
    while ($row = db_fetch($query)) {
74
      $result[$row['unit_snid']] = $row;
75
    }
76
77
    return $result;
78
  }
79
80
// Used by UNIT_CAPTAIN module TODO
81
  public static function db_unit_in_fleet_by_user($user_id, $location_id, $unit_snid, $for_update) {
82
    return classSupernova::$db->doSelectFetch(
83
      "SELECT *
84
    FROM {{fleets}} AS f
85
      JOIN {{unit}} AS u ON u.`unit_location_id` = f.fleet_id
86
    WHERE
87
      f.fleet_owner = {$user_id} AND
88
      (f.fleet_start_planet_id = {$location_id} OR f.fleet_end_planet_id = {$location_id})
89
      AND u.unit_snid = {$unit_snid} AND u.`unit_location_type` = " . LOC_FLEET . " AND " . self::db_unit_time_restrictions() .
90
      " LIMIT 1" .
91
      ($for_update ? ' FOR UPDATE' : ''));
92
  }
93
94
95
  public static function db_unit_list_laboratories($user_id) {
96
    return classSupernova::$db->doSelect("SELECT DISTINCT unit_location_id AS `id`
97
    FROM {{unit}}
98
    WHERE unit_player_id = {$user_id} AND unit_location_type = " . LOC_PLANET . " AND unit_level > 0 AND unit_snid IN (" . STRUC_LABORATORY . ", " . STRUC_LABORATORY_NANO . ");");
99
  }
100
101
  /**
102
   * @param       $unit_id
103
   * @param array $set - SET fields
104
   * @param array $adjust - ADJUST fields
105
   *
106
   * @return array|bool|mysqli_result|null
107
   */
108
  public static function db_unit_set_by_id($unit_id, $set, $adjust) {
109
    return classSupernova::$gc->cacheOperator->db_upd_record_by_id(
0 ignored issues
show
Bug introduced by
The method db_upd_record_by_id does only exist in SnDbCachedOperator, but not in Closure.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
110
      LOC_UNIT,
111
      $unit_id,
112
      $set,
113
      $adjust
114
    );
115
  }
116
117
  /**
118
   * @param array $set
119
   *
120
   * @return array|bool|false|mysqli_result|null
121
   */
122
  public static function db_unit_set_insert($set) {
123
    return classSupernova::$gc->cacheOperator->db_ins_record(LOC_UNIT, $set);
0 ignored issues
show
Bug introduced by
The method db_ins_record does only exist in SnDbCachedOperator, but not in Closure.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
124
  }
125
126
  public static function db_unit_list_delete($user_id = 0, $unit_location_type, $unit_location_id = 0, $unit_snid = 0) {
127
    $where = array('unit_location_type' => $unit_location_type);
128
    ($unit_location_id = idval($unit_location_id)) ? $where['unit_location_id'] = $unit_location_id : false;
129
    ($user_id = idval($user_id)) ? $where['unit_player_id'] = $user_id : false;
130
    ($unit_snid = idval($unit_snid)) ? $where['unit_snid'] = $unit_snid : false;
131
132
    return classSupernova::$gc->cacheOperator->db_del_record_list(LOC_UNIT, $where);
0 ignored issues
show
Bug introduced by
The method db_del_record_list does only exist in SnDbCachedOperator, but not in Closure.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
133
  }
134
135
  public static function db_unit_list_stat_calculate() {
136
    return classSupernova::$db->doSelect(
137
      "SELECT unit_player_id, unit_type, unit_snid, unit_level, count(*) AS unit_amount
138
    FROM `{{unit}}`
139
    WHERE unit_level > 0 AND " . self::db_unit_time_restrictions() .
140
      " GROUP BY unit_player_id, unit_type, unit_snid, unit_level"
141
    );
142
  }
143
144
145
  public static function db_unit_change_owner($location_type, $location_id, $new_owner_id) {
146
    classSupernova::$db->doUpdateTableSet(
147
      TABLE_UNIT,
148
      array(
149
        'unit_player_id' => $new_owner_id,
150
      ),
151
      array(
152
        'unit_location_type' => $location_type,
153
        'unit_location_id'   => $location_id,
154
      )
155
    );
156
  }
157
158
159
  public static function db_unit_list_admin_delete_mercenaries_finished() {
160
    classSupernova::$db->doDeleteDanger(
0 ignored issues
show
Deprecated Code introduced by
The method db_mysql::doDeleteDanger() has been deprecated.

This method has been deprecated.

Loading history...
161
      TABLE_UNIT,
162
      array(
163
        'unit_type' => UNIT_MERCENARIES,
164
      ),
165
      array(
166
        'unit_time_finish IS NOT NULL',
167
        "unit_time_finish < FROM_UNIXTIME(" . SN_TIME_NOW . ")",
168
      )
169
    );
170
  }
171
172
  public static function db_unit_list_admin_set_mercenaries_expire_time($default_length) {
173
    return
174
      classSupernova::$db->doUpdateTableSet(
175
        TABLE_UNIT,
176
        array(
177
          'unit_time_start'  => date(FMT_DATE_TIME_SQL, SN_TIME_NOW),
178
          'unit_time_finish' => date(FMT_DATE_TIME_SQL, SN_TIME_NOW + $default_length),
179
        ),
180
        array(
181
          'unit_type' => UNIT_MERCENARIES,
182
        )
183
      );
184
  }
185
186
187
  /**
188
   * @param      $unit_id
189
   * @param      $unit_value
190
   * @param      $user
191
   * @param null $planet_id
192
   *
193
   * @return bool
194
   */
195
  public static function dbUpdateOrInsertUnit($unit_id, $unit_value, $user, $planet_id = null) {
196
    DBStaticUser::validateUserRecord($user);
197
198
    $planet_id = !empty($planet_id['id']) ? $planet_id['id'] : $planet_id;
199
200
    $unit_location = sys_get_unit_location($user, array(), $unit_id);
201
    $location_id = $unit_location == LOC_USER ? $user['id'] : $planet_id;
202
    $location_id = $location_id ? $location_id : 'NULL';
203
204
    $temp = DBStaticUnit::db_get_unit_by_location($user['id'], $unit_location, $location_id, $unit_id, true, 'unit_id');
205
    if (!empty($temp['unit_id'])) {
206
      $result = (bool)classSupernova::$gc->cacheOperator->db_upd_record_by_id(
0 ignored issues
show
Bug introduced by
The method db_upd_record_by_id does only exist in SnDbCachedOperator, but not in Closure.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
207
        LOC_UNIT,
208
        $temp['unit_id'],
209
        array(),
210
        array(
211
          'unit_level' => +$unit_value
212
        )
213
      );
214
    } else {
215
      $locationIdRendered = $unit_location == LOC_USER ? $user['id'] : $planet_id;
216
      $unitType = get_unit_param($unit_id, P_UNIT_TYPE);
217
      $result = (bool)classSupernova::$gc->cacheOperator->db_ins_record(
0 ignored issues
show
Bug introduced by
The method db_ins_record does only exist in SnDbCachedOperator, but not in Closure.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
218
        LOC_UNIT,
219
        array(
220
          'unit_player_id'     => $user['id'],
221
          'unit_location_type' => (int)$unit_location,
222
          'unit_location_id'   => $locationIdRendered,
223
          'unit_type'          => (int)$unitType,
224
          'unit_snid'          => (int)$unit_id,
225
          'unit_level'         => (float)$unit_value,
226
        )
227
      );
228
    }
229
230
    return $result;
231
  }
232
233
}
234