Completed
Push — work-fleets ( e04805...964a94 )
by SuperNova.WS
05:41
created

DBStaticUser::db_user_list_to_celebrate()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 35
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 6
Bugs 1 Features 0
Metric Value
cc 1
eloc 18
c 6
b 1
f 0
nc 1
nop 1
dl 0
loc 35
rs 8.8571
ccs 0
cts 20
cp 0
crap 2
1
<?php
2
3
/**
4
 * Class DBStaticUser
5
 */
6
class DBStaticUser extends DBStaticRecord {
7
8
  public static $_table = 'users';
9
  public static $_idField = 'id';
10
11
12
  /**
13
   * @param int $playerId
14
   *
15
   * @return string[]
16
   */
17
  public static function getOnlineTime($playerId) {
18
    $row = static::getRecordById($playerId, array('username', 'onlinetime'));
19
20
    return !empty($row) ? $row : array();
21
  }
22
23
  // TODO - это вообще-то надо хранить в конфигурации
24
  /**
25
   * @return string
26
   */
27
  public static function getLastRegisteredUserName() {
28
    $result = static::fetchOne(
29
      static::buildSelectAll()
30
        ->fields('username')
31
        ->where(array('`user_as_ally` IS NULL'))
32
        ->orderBy(array('`id` DESC'))
33
    );
34
35
    return isset($result['username']) ? $result['username'] : '';
36
  }
37
38
  public static function db_player_list_export_blitz_info() {
39
    return static::execute(
40
      static::buildSelectAll()
41
        ->fields(array('id', 'username', 'total_rank', 'total_points', 'onlinetime',))
42
        ->where(array('`user_as_ally` IS NULL'))
43
        ->orderBy(array('`id`'))
44
    );
45
  }
46
47
  /**
48
   * @return array|bool|mysqli_result|null
49
   */
50
  public static function db_user_list_non_bots() {
51
//    $query = doquery("SELECT `id` FROM {{users}} WHERE `user_as_ally` IS NULL AND `user_bot` = " . USER_BOT_PLAYER . " FOR UPDATE;");
52
53
    $query = static::execute(
54
      static::buildSelectAll()
55
        ->fields('id')
56
        ->where(array(
57
          "`user_as_ally` IS NULL AND `user_bot` = " . USER_BOT_PLAYER . " FOR UPDATE;"
58
        ))
59
    );
60
61
    return $query;
62
  }
63
64
  public static function db_user_lock_with_target_owner_and_acs($user, $planet = array()) {
65
//    $query = "SELECT 1 FROM `{{users}}` WHERE `id` = :userId" .
0 ignored issues
show
Unused Code Comprehensibility introduced by
59% 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...
66
//      (!empty($planet['id_owner']) ? ' OR `id` = :planetOwnerId' : '');
67
68
    $where = '`id` = :userId';
69
    if (!empty($planet['id_owner'])) {
70
      $where .= ' OR `id` = :planetOwnerId';
71
    }
72
73
    $query = static::buildSelectLock()
74
      ->where($where);
0 ignored issues
show
Documentation introduced by
$where is of type string, but the function expects a array.

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...
75
76
    // TODO - FOR UPDATE
77
    return static::prepareExecute(
78
      $query,
79
      array(
80
        ':userId'        => idval($user['id']),
81
        ':planetOwnerId' => !empty($planet['id_owner']) ? idval($planet['id_owner']) : 0,
82
      )
83
    );
84
  }
85
86
  public static function db_user_count($online = false) {
87
//    $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
Unused Code Comprehensibility introduced by
55% 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...
88
//    return isset($result['user_count']) ? $result['user_count'] : 0;
89
90
//    $query =      'SELECT COUNT(`id`) AS `user_count` FROM `{{users}}` WHERE `user_as_ally` IS NULL' .
0 ignored issues
show
Unused Code Comprehensibility introduced by
46% 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...
91
//      ($online ? ' AND `onlinetime` > :onlineTime' : '');
92
93
    $query = static::buildSelectCountId()
94
      ->where('`user_as_ally` IS NULL');
0 ignored issues
show
Documentation introduced by
'`user_as_ally` IS NULL' is of type string, but the function expects a array.

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...
95
    if ($online) {
96
      $query->where('`onlinetime` > :onlineTime');
0 ignored issues
show
Documentation introduced by
'`onlinetime` > :onlineTime' is of type string, but the function expects a array.

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...
97
    }
98
99
    return static::prepareFetchValue($query, array(
100
      ':onlineTime' => SN_TIME_NOW - classSupernova::$config->game_users_online_timeout,
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...
101
    ));
102
  }
103
104
  public static function db_user_list_admin_sorted($sort, $online = false) {
105
//    $query = "SELECT
0 ignored issues
show
Unused Code Comprehensibility introduced by
53% 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...
106
//          u.*, COUNT(r.id) AS referral_count, SUM(r.dark_matter) AS referral_dm
107
//      FROM
108
//          {{users}} as u
109
//          LEFT JOIN
110
//              {{referrals}} as r on r.id_partner = u.id
111
//      WHERE " .
112
//      ($online ? "`onlinetime` >= :onlineTime" : 'user_as_ally IS NULL') .
113
//      " GROUP BY u.id
114
//        ORDER BY user_as_ally, {$sort} ASC";
115
116
    $query = static::buildSelect()
117
      ->fromAlias('u')
118
      ->field('u.*')
119
      ->fieldCount('r.id', 'referral_count')
120
      ->fieldSingleFunction('sum', 'r.dark_matter', 'referral_dm')
121
      ->join('LEFT JOIN {{referrals}} as r on r.id_partner = u.id')
122
      ->where($online ? "`onlinetime` >= :onlineTime" : 'user_as_ally IS NULL')
0 ignored issues
show
Documentation introduced by
$online ? '`onlinetime` ... 'user_as_ally IS NULL' is of type string, but the function expects a array.

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...
123
      ->groupBy('u.id')
0 ignored issues
show
Documentation introduced by
'u.id' is of type string, but the function expects a array.

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...
124
      ->orderBy("user_as_ally, {$sort} ASC");
125
126
    return static::prepareExecute(
127
      $query,
128
      array(
129
        ':onlineTime' => SN_TIME_NOW - classSupernova::$config->game_users_online_timeout,
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...
130
      )
131
    );
132
  }
133
134
  public static function db_user_list_to_celebrate($config_user_birthday_range) {
135
//    $query = "SELECT
0 ignored issues
show
Unused Code Comprehensibility introduced by
78% 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...
136
//        `id`, `username`, `user_birthday`, `user_birthday_celebrated`,
137
//        CONCAT(YEAR(CURRENT_DATE), DATE_FORMAT(`user_birthday`, '-%m-%d')) AS `current_birthday`,
138
//        DATEDIFF(CURRENT_DATE, CONCAT(YEAR(CURRENT_DATE), DATE_FORMAT(`user_birthday`, '-%m-%d'))) AS `days_after_birthday`
139
//      FROM
140
//        `{{users}}`
141
//      WHERE
142
//        `user_birthday` IS NOT NULL
143
//        AND (`user_birthday_celebrated` IS NULL OR DATE_ADD(`user_birthday_celebrated`, INTERVAL 1 YEAR) < CURRENT_DATE)
144
//        AND `user_as_ally` IS NULL
145
//      HAVING
146
//        `days_after_birthday` >= 0 AND `days_after_birthday` < :birthdayRange FOR UPDATE";
147
148
    $query = static::buildSelect()
149
      ->field('id')
150
      ->field('username')
151
      ->field('user_birthday')
152
      ->field('user_birthday_celebrated')
153
      ->field(DbSqlLiteral::build()->literal('CONCAT(YEAR(CURRENT_DATE), DATE_FORMAT(`user_birthday`, \'-%m-%d\')) AS `current_birthday`'))
154
      ->field(DbSqlLiteral::build()->literal('DATEDIFF(CURRENT_DATE, CONCAT(YEAR(CURRENT_DATE), DATE_FORMAT(`user_birthday`, \'-%m-%d\'))) AS `days_after_birthday`'))
155
      ->where('`user_birthday` IS NOT NULL')
0 ignored issues
show
Documentation introduced by
'`user_birthday` IS NOT NULL' is of type string, but the function expects a array.

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...
156
      ->where('(`user_birthday_celebrated` IS NULL OR DATE_ADD(`user_birthday_celebrated`, INTERVAL 1 YEAR) < CURRENT_DATE)')
0 ignored issues
show
Documentation introduced by
'(`user_birthday_celebra... YEAR) < CURRENT_DATE)' is of type string, but the function expects a array.

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...
157
      ->where('`user_as_ally` IS NULL')
0 ignored issues
show
Documentation introduced by
'`user_as_ally` IS NULL' is of type string, but the function expects a array.

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...
158
      ->having('`days_after_birthday` >= 0')
159
      ->having('`days_after_birthday` < :birthdayRange')
160
      ->forUpdate();
161
162
    return static::prepareExecute(
163
      $query,
164
      array(
165
        ':birthdayRange' => $config_user_birthday_range,
166
      )
167
    );
168
  }
169
170
  public static function db_user_list_admin_multiaccounts() {
171
    $query = "SELECT COUNT(*) AS `ip_count`, `user_lastip`
172
      FROM `{{users}}`
173
      WHERE `user_as_ally` IS NULL
174
      GROUP BY `user_lastip`
175
      HAVING COUNT(*) > 1";
176
177
    return static::prepareExecute(
178
      $query
179
    );
180
  }
181
182
  public static function db_player_list_blitz_delete_players() {
183
    doquery("DELETE FROM `{{users}}` WHERE username LIKE 'Игрок%';");
184
  }
185
186
  public static function db_player_list_blitz_set_50k_dm() {
187
    doquery('UPDATE `{{users}}` SET dark_matter = 50000, dark_matter_total = 50000;');
188
  }
189
190
191
  /**
192
   * Выбирает записи игроков по списку их ID
193
   *
194
   * @param $user_id_list
195
   *
196
   * @return array
197
   */
198
  public static function db_user_list_by_id($user_id_list) {
199
    !is_array($user_id_list) ? $user_id_list = array($user_id_list) : false;
200
201
    $user_list = array();
202
    foreach ($user_id_list as $user_id_unsafe) {
203
      $user = DBStaticUser::db_user_by_id($user_id_unsafe);
204
      !empty($user) ? $user_list[$user_id_unsafe] = $user : false;
205
    }
206
207
    return $user_list;
208
  }
209
210
211
  public static function db_user_by_username($username_unsafe, $for_update = false, $fields = '*', $player = null, $like = false) {
212
    return classSupernova::db_get_user_by_username($username_unsafe, $for_update, $fields, $player, $like);
213
  }
214
215
  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...
216
    return classSupernova::db_get_record_list(LOC_USER, $user_filter);
217
  }
218
219
  public static function db_user_set_by_id($user_id, $set) {
220
    return classSupernova::db_upd_record_by_id(LOC_USER, $user_id, $set);
221
  }
222
223
  public static function db_user_by_id($user_id_unsafe, $for_update = false, $fields = '*', $player = null) {
224
    return classSupernova::db_get_user_by_id($user_id_unsafe, $for_update, $fields, $player);
225
  }
226
227
  public static function db_user_list_set_mass_mail(&$owners_list, $set) {
228
    return classSupernova::db_upd_record_list(LOC_USER, !empty($owners_list) ? '`id` IN (' . implode(',', $owners_list) . ');' : '', $set);
229
  }
230
231
  public static function db_user_list_set_by_ally_and_rank($ally_id, $ally_rank_id, $set) {
232
    return classSupernova::db_upd_record_list(LOC_USER, "`ally_id`={$ally_id} AND `ally_rank_id` >= {$ally_rank_id}", $set);
233
  }
234
235
  public static function db_user_list_set_ally_deprecated_convert_ranks($ally_id, $i, $rank_id) {
236
    return classSupernova::db_upd_record_list(LOC_USER, "`ally_id` = {$ally_id} AND `ally_rank_id`={$rank_id}", "`ally_rank_id` = {$i}");
237
  }
238
239
//  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...
240
//    return doquery("UPDATE {{users}} SET `current_planet` = `id_planet` WHERE `id` = {$user_id} AND `current_planet` = {$captured_planet};");
241
//  }
242
243
}
244