Completed
Push — work-fleets ( 1c4183...d64c53 )
by SuperNova.WS
05:36
created

DBStaticUser::db_user_set_by_id()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 3
rs 10
c 1
b 1
f 0
1
<?php
2
3
class DBStaticUser extends DBStaticRecord {
4
5
  public static $_table = 'users';
6
  public static $_idField = 'id';
7
8
9
  /**
10
   * @param int $user_id
11
   *
12
   * @return string[]
13
   */
14
  public static function getOnlineTime($user_id) {
15
    $user_record = static::getRecordById($user_id, array('username', 'onlinetime'));
16
17
    return !empty($user_record) ? $user_record : array();
18
  }
19
20
  // TODO - это вообще-то надо хранить в конфигурации
21
  /**
22
   * @return string
23
   * @throws ExceptionDbSqlWhereNotAnArray
24
   */
25
  public static function getLastRegisteredUserName() {
26
    $result = static::$dbStatic->fetchOne(
27
      static::buildSelect()
28
        ->fields('username')
29
        ->where(array('`user_as_ally` IS NULL'))
30
        ->order(array('`id` DESC'))
31
    );
32
33
    return isset($result['username']) ? $result['username'] : '';
34
  }
35
36
  public static function db_player_list_export_blitz_info() {
37
    return static::$dbStatic->execute(
38
      static::buildSelect()
39
        ->fields(array('id', 'username', 'total_rank', 'total_points', 'onlinetime',))
40
        ->where(array('`user_as_ally` IS NULL'))
41
        ->order(array('`id`'))
42
    );
43
  }
44
45
  public static function db_player_list_blitz_delete_players() {
46
    doquery("DELETE FROM `{{users}}` WHERE username LIKE 'Игрок%';");
47
  }
48
49
  public static function db_player_list_blitz_set_50k_dm() {
50
    doquery('UPDATE `{{users}}` SET dark_matter = 50000, dark_matter_total = 50000;');
51
  }
52
53
  public static function db_user_by_username($username_unsafe, $for_update = false, $fields = '*', $player = null, $like = false) {
54
    return classSupernova::db_get_user_by_username($username_unsafe, $for_update, $fields, $player, $like);
55
  }
56
57
  public static function db_user_list($user_filter = '', $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...
58
    return classSupernova::db_get_record_list(LOC_USER, $user_filter);
59
  }
60
61
  public static function db_user_set_by_id($user_id, $set) {
62
    return classSupernova::db_upd_record_by_id(LOC_USER, $user_id, $set);
63
  }
64
65
  /**
66
   * @return array|bool|mysqli_result|null
67
   */
68
  public static function db_user_list_non_bots() {
69
    $query = doquery("SELECT `id` FROM {{users}} WHERE `user_as_ally` IS NULL AND `user_bot` = " . USER_BOT_PLAYER . " FOR UPDATE;");
70
71
    return $query;
72
  }
73
74
  public static function db_user_lock_with_target_owner_and_acs($user, $planet = array()) {
75
    static::$dbStatic->execute(
76
      static::buildSelectLock()
77
        ->where(array("`id` = " . idval($user['id']) .
78
          (isset($planet['id_owner']) ? ' OR `id` = ' . idval($planet['id_owner']) : '')))
79
    );
80
81
//    doquery("SELECT 1 FROM {{users}} WHERE `id` = " . idval($user['id']) .
0 ignored issues
show
Unused Code Comprehensibility introduced by
56% 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...
82
//      (isset($planet['id_owner']) ? ' OR `id` = ' . idval($planet['id_owner']) : '') .
83
//      " FOR UPDATE;"
84
//    );
85
  }
86
87
  /**
88
   * Выбирает записи игроков по списку их ID
89
   *
90
   * @param $user_id_list
91
   *
92
   * @return array
93
   */
94
  public static function db_user_list_by_id($user_id_list) {
95
    !is_array($user_id_list) ? $user_id_list = array($user_id_list) : false;
96
97
    $user_list = array();
98
    foreach ($user_id_list as $user_id_unsafe) {
99
      $user = DBStaticUser::db_user_by_id($user_id_unsafe);
100
      !empty($user) ? $user_list[$user_id_unsafe] = $user : false;
101
    }
102
103
    return $user_list;
104
  }
105
106
107
  public static function db_user_by_id($user_id_unsafe, $for_update = false, $fields = '*', $player = null) {
108
    return classSupernova::db_get_user_by_id($user_id_unsafe, $for_update, $fields, $player);
109
  }
110
111
  public static function db_user_list_set_mass_mail(&$owners_list, $set) {
112
    return classSupernova::db_upd_record_list(LOC_USER, !empty($owners_list) ? '`id` IN (' . implode(',', $owners_list) . ');' : '', $set);
113
  }
114
115
  public static function db_user_list_set_by_ally_and_rank($ally_id, $ally_rank_id, $set) {
116
    return classSupernova::db_upd_record_list(LOC_USER, "`ally_id`={$ally_id} AND `ally_rank_id` >= {$ally_rank_id}", $set);
117
  }
118
119
  public static function db_user_list_set_ally_deprecated_convert_ranks($ally_id, $i, $rank_id) {
120
    return classSupernova::db_upd_record_list(LOC_USER, "`ally_id` = {$ally_id} AND `ally_rank_id`={$rank_id}", "`ally_rank_id` = {$i}");
121
  }
122
123
124
//  public static function db_user_change_active_planet_to_capital($user_id, $captured_planet) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
54% 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...
125
//    return doquery("UPDATE {{users}} SET `current_planet` = `id_planet` WHERE `id` = {$user_id} AND `current_planet` = {$captured_planet};");
126
//  }
127
128
129
  public static function db_user_count($online = false) {
130
    $result = doquery('SELECT COUNT(id) AS user_count FROM `{{users}}` WHERE user_as_ally IS NULL' . ($online ? ' AND onlinetime > ' . (SN_TIME_NOW - classSupernova::$config->game_users_online_timeout) : ''), true);
0 ignored issues
show
Documentation introduced by
The property game_users_online_timeout does not exist on object<classConfig>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
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...
131
132
    return isset($result['user_count']) ? $result['user_count'] : 0;
133
  }
134
135
136
  public static function db_user_list_to_celebrate($config_user_birthday_range) {
137
    return doquery(
138
      "SELECT
139
      `id`, `username`, `user_birthday`, `user_birthday_celebrated`
140
      , CONCAT(YEAR(CURRENT_DATE), DATE_FORMAT(`user_birthday`, '-%m-%d')) AS `current_birthday`
141
      , DATEDIFF(CURRENT_DATE, CONCAT(YEAR(CURRENT_DATE), DATE_FORMAT(`user_birthday`, '-%m-%d'))) AS `days_after_birthday`
142
    FROM
143
      `{{users}}`
144
    WHERE
145
      `user_birthday` IS NOT NULL
146
      AND (`user_birthday_celebrated` IS NULL OR DATE_ADD(`user_birthday_celebrated`, INTERVAL 1 YEAR) < CURRENT_DATE)
147
      AND `user_as_ally` IS NULL
148
    HAVING
149
      `days_after_birthday` >= 0 AND `days_after_birthday` < {$config_user_birthday_range} FOR UPDATE;");
150
  }
151
152
//  public static function db_user_list_online_sorted($TypeSort) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% 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...
153
//    return doquery(
154
//      "SELECT `id` AS `ID`, `username` AS `NAME`, `ally_name` AS `ALLY`, `total_points` AS `STAT_POINTS`,
155
//      `onlinetime` AS `ACTIVITY`
156
//    FROM `{{users}}`
157
//    WHERE `onlinetime` >= " . (SN_TIME_NOW - classSupernova::$config->game_users_online_timeout) . " ORDER BY user_as_ally, `" . $TypeSort . "` ASC;");
158
//  }
159
160
161
  public static function db_user_list_admin_multiaccounts() {
162
    return doquery("SELECT COUNT(*) AS ip_count, user_lastip FROM `{{users}}` WHERE user_as_ally IS NULL GROUP BY user_lastip HAVING COUNT(*) > 1;");
163
  }
164
165
166
  public static function db_user_list_admin_sorted($sort, $online = false) {
167
    return doquery("SELECT u.*, COUNT(r.id) AS referral_count, SUM(r.dark_matter) AS referral_dm FROM {{users}} as u
168
    LEFT JOIN {{referrals}} as r on r.id_partner = u.id
169
    WHERE" .
170
      ($online ? " `onlinetime` >= " . (SN_TIME_NOW - classSupernova::$config->game_users_online_timeout) : ' user_as_ally IS NULL') .
0 ignored issues
show
Documentation introduced by
The property game_users_online_timeout does not exist on object<classConfig>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
171
      " GROUP BY u.id
172
    ORDER BY user_as_ally, {$sort} ASC");
173
  }
174
175
}
176