Completed
Push — work-fleets ( f6000a...17041b )
by SuperNova.WS
05:43
created

DBStaticUnit   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 106
Duplicated Lines 6.6 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 7
loc 106
rs 10
wmc 20
lcom 0
cbo 1

13 Methods

Rating   Name   Duplication   Size   Complexity  
A db_unit_time_restrictions() 7 7 2
A db_unit_by_id() 0 3 1
A db_unit_by_location() 0 4 1
A db_unit_count_by_user_and_type_and_snid() 0 14 4
A db_unit_in_fleet_by_user() 0 13 2
A db_unit_list_laboratories() 0 5 1
A db_unit_set_by_id() 0 3 1
A db_unit_set_insert() 0 3 1
A db_unit_list_delete() 0 6 3
A db_unit_list_stat_calculate() 0 8 1
A db_unit_change_owner() 0 3 1
A db_unit_list_admin_delete_mercenaries_finished() 0 3 1
A db_unit_list_admin_set_mercenaries_expire_time() 0 9 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
class DBStaticUnit {
4
5 View Code Duplication
  public static function db_unit_time_restrictions($date = SN_TIME_NOW) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
6
    $date = is_numeric($date) ? "FROM_UNIXTIME({$date})" : "'{$date}'";
7
8
    return
9
      "(unit_time_start IS NULL OR unit_time_start <= {$date}) AND
10
    (unit_time_finish IS NULL OR unit_time_finish = '1970-01-01 03:00:00' OR unit_time_finish >= {$date})";
11
  }
12
13
  public static function db_unit_by_id($unit_id, $for_update = false, $fields = '*') {
14
    return classSupernova::db_get_unit_by_id($unit_id, $for_update, $fields);
15
  }
16
17
  public static function db_unit_by_location($user_id = 0, $location_type, $location_id, $unit_snid = 0, $for_update = false, $fields = '*') {
18
    // apply time restrictions ????
0 ignored issues
show
Unused Code Comprehensibility introduced by
37% 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...
19
    return classSupernova::db_get_unit_by_location($user_id, $location_type, $location_id, $unit_snid, $for_update, $fields);
20
  }
21
22
  public static function db_unit_count_by_user_and_type_and_snid($user_id, $unit_type = 0, $unit_snid = 0) {
23
    $query = doquery(
24
      "SELECT unit_snid, sum(unit_level) as `qty`  FROM {{unit}} WHERE `unit_player_id` = {$user_id} " .
25
      ($unit_type ? "AND `unit_type` = {$unit_type} " : '') .
26
      ($unit_snid ? "AND `unit_snid` = {$unit_snid} " : '') .
27
      'GROUP BY `unit_snid`'
28
    );
29
    $result = array();
30
    while($row = db_fetch($query)) {
31
      $result[$row['unit_snid']] = $row;
32
    }
33
34
    return $result;
35
  }
36
37
// Used by UNIT_CAPTAIN module TODO
38
  public static function db_unit_in_fleet_by_user($user_id, $location_id, $unit_snid, $for_update) {
39
    return doquery(
40
      "SELECT *
41
    FROM {{fleets}} AS f
42
      JOIN {{unit}} AS u ON u.`unit_location_id` = f.fleet_id
43
    WHERE
44
      f.fleet_owner = {$user_id} AND
45
      (f.fleet_start_planet_id = {$location_id} OR f.fleet_end_planet_id = {$location_id})
46
      AND u.unit_snid = {$unit_snid} AND u.`unit_location_type` = " . LOC_FLEET . " AND " . self::db_unit_time_restrictions() .
47
      " LIMIT 1" .
48
      ($for_update ? ' FOR UPDATE' : '')
49
      , true);
0 ignored issues
show
Documentation introduced by
true is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
50
  }
51
52
53
  public static function db_unit_list_laboratories($user_id) {
54
    return doquery("SELECT DISTINCT unit_location_id AS `id`
55
    FROM {{unit}}
56
    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 . ");");
57
  }
58
59
  public static function db_unit_set_by_id($unit_id, $set) {
60
    return classSupernova::db_upd_record_by_id(LOC_UNIT, $unit_id, $set);
61
  }
62
63
  /**
64
   * @param string $set
65
   *
66
   * @return array|bool|false|mysqli_result|null
67
   */
68
  public static function db_unit_set_insert($set) {
69
    return classSupernova::db_ins_record(LOC_UNIT, $set);
70
  }
71
72
  public static function db_unit_list_delete($user_id = 0, $unit_location_type, $unit_location_id, $unit_snid = 0) {
73
    return classSupernova::db_del_record_list(LOC_UNIT,
74
      "`unit_location_type` = {$unit_location_type} AND `unit_location_id` = {$unit_location_id}" .
75
      ($user_id = idval($user_id) ? " AND `unit_player_id` = {$user_id}" : '') .
76
      ($unit_snid = idval($unit_snid) ? " AND `unit_snid` = {$unit_snid}" : ''));
77
  }
78
79
  public static function db_unit_list_stat_calculate() {
80
    return doquery(
81
      "SELECT unit_player_id, unit_type, unit_snid, unit_level, count(*) AS unit_amount
82
    FROM `{{unit}}`
83
    WHERE unit_level > 0 AND " . self::db_unit_time_restrictions() .
84
      " GROUP BY unit_player_id, unit_type, unit_snid, unit_level"
85
    );
86
  }
87
88
89
  public static function db_unit_change_owner($location_type, $location_id, $new_owner_id) {
90
    doquery("UPDATE {{unit}} SET `unit_player_id` = {$new_owner_id} WHERE `unit_location_type` = {$location_type} AND `unit_location_id` = {$location_id}");
91
  }
92
93
94
  public static function db_unit_list_admin_delete_mercenaries_finished() {
95
    return doquery("DELETE FROM {{unit}} WHERE unit_time_finish IS NOT NULL AND unit_time_finish < FROM_UNIXTIME(" . SN_TIME_NOW . ") AND unit_type = " . UNIT_MERCENARIES);
96
  }
97
98
  public static function db_unit_list_admin_set_mercenaries_expire_time($default_length) {
99
    return doquery(
100
      "UPDATE {{unit}}
101
    SET
102
      unit_time_start = FROM_UNIXTIME(" . SN_TIME_NOW . "),
103
      unit_time_finish = FROM_UNIXTIME(" . (SN_TIME_NOW + $default_length) . ")
104
    WHERE unit_type = " . UNIT_MERCENARIES
105
    );
106
  }
107
108
}
109