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

DBStaticChat   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 262
Duplicated Lines 13.74 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 36
loc 262
ccs 0
cts 152
cp 0
rs 10
wmc 17
lcom 0
cbo 3

17 Methods

Rating   Name   Duplication   Size   Complexity  
A db_chat_player_list_online() 0 13 1
A db_chat_message_insert() 12 12 1
A db_chat_message_count_by_ally() 0 5 1
A db_chat_message_get_page() 0 10 1
A db_chat_player_update_invisibility() 0 11 1
A db_chat_player_update_unmute() 12 12 1
A db_chat_player_update_mute() 12 12 1
A db_chat_message_delete() 0 8 1
A db_chat_message_purge() 0 3 1
A db_chat_message_get_last_25() 0 5 1
A db_chat_player_get() 0 3 1
A db_chat_player_insert() 0 5 1
A db_chat_player_update() 0 11 1
A db_chat_list_select_advanced() 0 14 1
A db_chat_list_get_with_users() 0 19 1
A db_chat_player_select_id() 0 5 1
A db_chat_player_update_activity() 0 11 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
namespace DBStatic;
4
use classSupernova;
5
use mysqli_result;
6
7
class DBStaticChat {
8
9
  // Chat *************************************************************************************************************
10
  public static function db_chat_player_list_online($chat_refresh_rate, $ally_add) {
11
    $sql_date = SN_TIME_NOW - $chat_refresh_rate * 2;
12
13
    return classSupernova::$db->doSelect(
14
      "SELECT u.*, cp.*
15
    FROM {{chat_player}} AS cp
16
      JOIN {{users}} AS u ON u.id = cp.chat_player_player_id
17
    WHERE
18
      `chat_player_refresh_last` >= '{$sql_date}'
19
      AND (`banaday` IS NULL OR `banaday` <= " . SN_TIME_NOW . ")
20
      {$ally_add}
21
    ORDER BY authlevel DESC, `username`");
22
  }
23
24
  /**
25
   * @param $user_id
26
   * @param $nickUnsafe
27
   * @param $ally_id
28
   * @param $message_unsafe
29
   * @param $chat_message_sender_name_unsafe
30
   * @param $chat_message_recipient_id
31
   * @param $chat_message_recipient_name_unsafe
32
   */
33 View Code Duplication
  public static function db_chat_message_insert($user_id, $nickUnsafe, $ally_id, $message_unsafe, $chat_message_sender_name_unsafe = '', $chat_message_recipient_id = 0, $chat_message_recipient_name_unsafe = '') {
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...
34
    classSupernova::$db->doInsertSet(TABLE_CHAT, array(
35
      'chat_message_sender_id'      => $user_id,
36
      'user'                        => $nickUnsafe,
37
      'ally_id'                     => $ally_id,
38
      'message'                     => $message_unsafe,
39
      'timestamp'                   => SN_TIME_NOW,
40
      'chat_message_sender_name'    => $chat_message_sender_name_unsafe,
41
      'chat_message_recipient_id'   => $chat_message_recipient_id,
42
      'chat_message_recipient_name' => $chat_message_recipient_name_unsafe,
43
    ));
44
  }
45
46
  /**
47
   * @param $alliance
48
   *
49
   * @return array|bool|mysqli_result|null
50
   */
51
  public static function db_chat_message_count_by_ally($alliance) {
52
    $rows = classSupernova::$db->doSelectFetch("SELECT count(1) AS CNT FROM {{chat}} WHERE ally_id = '{$alliance}';");
53
54
    return $rows;
55
  }
56
57
  /**
58
   * @param $alliance
59
   * @param $where_add
60
   * @param $start_row
61
   * @param $page_limit
62
   *
63
   * @return array|bool|mysqli_result|null
64
   */
65
  public static function db_chat_message_get_page($alliance, $where_add, $start_row, $page_limit) {
66
    $query = classSupernova::$db->doSelect(
67
      "SELECT c.*, u.authlevel
68
      FROM
69
        {{chat}} AS c
70
        LEFT JOIN {{users}} AS u ON u.id = c.chat_message_sender_id
71
      WHERE c.chat_message_recipient_id IS NULL AND c.ally_id = '{$alliance}' {$where_add} ORDER BY messageid DESC LIMIT {$start_row}, {$page_limit};");
72
73
    return $query;
74
  }
75
76
  /**
77
   * @param $chat_directive
78
   * @param $userId
79
   */
80
  public static function db_chat_player_update_invisibility($chat_directive, $userId) {
81
    classSupernova::$db->doUpdateRowSet(
82
      TABLE_CHAT_PLAYER,
83
      array(
84
        'chat_player_invisible' => $chat_directive,
85
      ),
86
      array(
87
        'chat_player_player_id' => $userId,
88
      )
89
    );
90
  }
91
92
  /**
93
   * @param $reasonUnsafe
94
   * @param $chat_player_subject_id
95
   */
96 View Code Duplication
  public static function db_chat_player_update_unmute($reasonUnsafe, $chat_player_subject_id) {
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...
97
    classSupernova::$db->doUpdateRowSet(
98
      TABLE_CHAT_PLAYER,
99
      array(
100
        'chat_player_muted'       => 0,
101
        'chat_player_mute_reason' => $reasonUnsafe,
102
      ),
103
      array(
104
        'chat_player_player_id' => $chat_player_subject_id,
105
      )
106
    );
107
  }
108
109
  /**
110
   * @param $date_compiled
111
   * @param $reasonUnsafe
112
   * @param $chat_player_subject_id
113
   */
114 View Code Duplication
  public static function db_chat_player_update_mute($date_compiled, $reasonUnsafe, $chat_player_subject_id) {
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...
115
    classSupernova::$db->doUpdateRowSet(
116
      TABLE_CHAT_PLAYER,
117
      array(
118
        'chat_player_muted'       => $date_compiled,
119
        'chat_player_mute_reason' => $reasonUnsafe,
120
      ),
121
      array(
122
        'chat_player_player_id' => $chat_player_subject_id,
123
      )
124
    );
125
  }
126
127
  /**
128
   * @param $delete
129
   */
130
  public static function db_chat_message_delete($delete) {
131
    classSupernova::$gc->db->doDeleteRow(
0 ignored issues
show
Bug introduced by
The method doDeleteRow does only exist in db_mysql, 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...
132
      TABLE_CHAT,
133
      array(
134
        'messageid' => $delete,
135
      )
136
    );
137
  }
138
139
  public static function db_chat_message_purge() {
140
    classSupernova::$db->doDeleteSql('DELETE FROM `{{chat}}`;');
141
  }
142
143
  /**
144
   * @return array|bool|mysqli_result|null
145
   */
146
  public static function db_chat_message_get_last_25() {
147
    $query = classSupernova::$db->doSelect("SELECT * FROM `{{chat}}` ORDER BY messageid DESC LIMIT 25;");
148
149
    return $query;
150
  }
151
152
153
  /**
154
   * @param $player_id
155
   * @param $fields
156
   *
157
   * @return array|bool|mysqli_result|null
158
   */
159
  public static function db_chat_player_get($player_id, $fields) {
160
    return classSupernova::$db->doSelectFetch("SELECT {$fields} FROM {{chat_player}} WHERE `chat_player_player_id` = {$player_id} LIMIT 1");
161
  }
162
163
164
  /**
165
   * @param $player_id
166
   */
167
  public static function db_chat_player_insert($player_id) {
168
    classSupernova::$db->doInsertSet(TABLE_CHAT_PLAYER, array(
169
      'chat_player_player_id' => $player_id,
170
    ));
171
  }
172
173
174
  /**
175
   * @param $userId
176
   */
177
  public static function db_chat_player_update($userId) {
178
    classSupernova::$db->doUpdateRowSet(
179
      TABLE_CHAT_PLAYER,
180
      array(
181
        'chat_player_refresh_last' => SN_TIME_NOW,
182
      ),
183
      array(
184
        'chat_player_player_id' => $userId,
185
      )
186
    );
187
  }
188
189
190
  /**
191
   * @param $alliance
192
   * @param $user
193
   *
194
   * @return array|bool|mysqli_result|null
195
   */
196
  public static function db_chat_list_select_advanced($alliance, $user) {
197
    $rows = classSupernova::$db->doSelectFetch("SELECT count(1) AS CNT
198
          FROM {{chat}}
199
          WHERE
200
          (
201
            (ally_id = '{$alliance}' AND `chat_message_recipient_id` IS NULL) OR
202
            (ally_id = 0 AND `chat_message_recipient_id` = {$user['id']}) OR
203
            (ally_id = 0 AND `chat_message_sender_id` = {$user['id']} AND `chat_message_recipient_id` IS NOT NULL) OR
204
            (ally_id = 0 AND `chat_message_sender_id` IS NULL AND `chat_message_recipient_id` IS NULL)
205
          )
206
        ");
207
208
    return $rows;
209
  }
210
211
212
  /**
213
   * @param $alliance
214
   * @param $user
215
   * @param $where_add
216
   * @param $start_row
217
   * @param $page_limit
218
   *
219
   * @return array|bool|mysqli_result|null
220
   */
221
  public static function db_chat_list_get_with_users($alliance, $user, $where_add, $start_row, $page_limit) {
222
    $query = classSupernova::$db->doSelect(
223
      "SELECT c.*, u.authlevel
224
        FROM
225
          {{chat}} AS c
226
          LEFT JOIN {{users}} AS u ON u.id = c.chat_message_sender_id
227
        WHERE
228
          (
229
            (c.ally_id = '{$alliance}' AND `chat_message_recipient_id` IS NULL) OR
230
            (c.ally_id = 0 AND `chat_message_recipient_id` = {$user['id']}) OR
231
            (c.ally_id = 0 AND `chat_message_sender_id` = {$user['id']} AND `chat_message_recipient_id` IS NOT NULL) OR
232
            (c.ally_id = 0 AND `chat_message_sender_id` IS NULL AND `chat_message_recipient_id` IS NULL)
233
          )
234
          {$where_add}
235
        ORDER BY messageid DESC
236
        LIMIT {$start_row}, {$page_limit}");
237
238
    return $query;
239
  }
240
241
  /**
242
   * @param $user
243
   *
244
   * @return array|bool|mysqli_result|null
245
   */
246
  public static function db_chat_player_select_id($user) {
247
    $activity_row = classSupernova::$db->doSelectFetch("SELECT `chat_player_id` FROM {{chat_player}} WHERE `chat_player_player_id` = {$user['id']} LIMIT 1");
248
249
    return $activity_row;
250
  }
251
252
253
  /**
254
   * @param $userId
255
   */
256
  public static function db_chat_player_update_activity($userId) {
257
    classSupernova::$db->doUpdateRowSet(
258
      TABLE_CHAT_PLAYER,
259
      array(
260
        'chat_player_activity' => SN_TIME_SQL,
261
      ),
262
      array(
263
        'chat_player_player_id' => $userId,
264
      )
265
    );
266
  }
267
268
}