Completed
Push — trunk ( a060bf...055e93 )
by SuperNova.WS
04:11
created

player_nick_uncompact()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 6
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 10
rs 8.8571
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 76 and the first side effect is on line 6.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/**
3
 * Created by Gorlum 04.12.2017 4:20
4
 */
5
6
global $nickSorting;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
7
$nickSorting = [
8
  NICK_HTML,
9
10
  NICK_FIRST,
11
12
  NICK_RANK,
13
14
  NICK_BIRTHDAY,
15
  NICK_VACATION,
16
  NICK_PREMIUM,
17
  NICK_AUTH_LEVEL,
18
19
  NICK_HIGHLIGHT,
20
  NICK_CLASS,
21
  NICK_NICK_CLASS,
22
  NICK_NICK,
23
  NICK_NICK_CLASS_END,
24
  NICK_ALLY_CLASS,
25
  NICK_ALLY,
26
  NICK_ALLY_CLASS_END,
27
  NICK_CLASS_END,
28
  NICK_HIGHLIGHT_END,
29
30
  NICK_GENDER,
31
  NICK_RACE,
32
  NICK_AWARD,
33
34
  NICK_LAST,
35
];
36
/*
37
 // Old nick sorting
38
 $nickSorting = [
39
  NICK_HTML,
40
41
  NICK_FIRST,
42
43
  NICK_RACE,
44
  NICK_GENDER,
45
  NICK_AWARD,
46
  NICK_VACATION,
47
  NICK_BIRTHDAY,
48
  NICK_PREMIUM,
49
  NICK_AUTH_LEVEL,
50
51
  NICK_RANK,
52
  NICK_HIGHLIGHT,
53
  NICK_CLASS,
54
  NICK_NICK_CLASS,
55
  NICK_NICK,
56
  NICK_NICK_CLASS_END,
57
  NICK_ALLY_CLASS,
58
  NICK_ALLY,
59
  NICK_ALLY_CLASS_END,
60
  NICK_CLASS_END,
61
  NICK_HIGHLIGHT_END,
62
63
  NICK_LAST,
64
];
65
*/
66
67
68
/**
69
 * Ordering nick parts according to predefined part order
70
 *
71
 * @param array      $array
72
 * @param null|array $options
73
 *
74
 * @return array
75
 */
76
function playerNickOrder($array, $options = null) {
77
  global $nickSorting;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
78
79
  $currentSort = is_array($options[NICK_SORT]) ? $options[NICK_SORT] : $nickSorting;
80
81
  $result = [];
82
  // Rearranging nick parts according to sort array
83
  foreach ($currentSort as $nickPartId) {
84
    if (array_key_exists($nickPartId, $array)) {
85
      $result[$nickPartId] = $array[$nickPartId];
86
      unset($array[$nickPartId]);
87
    }
88
  }
89
90
  // Adding what left of nick parts to resulting array
91
  return array_merge_recursive_numeric($result, $array);
92
}
93
94
// Может принимать: (array)$user, $nick_render_array, $nick_render_array_html, $nick_render_string_compact
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% 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...
95
function player_nick_render_to_html($result, $options = false) {
96
  $result = player_nick_uncompact($result);
97
98
  if (is_array($result)) {
99
    if (isset($result['id'])) {
100
      $result = player_nick_render_current_to_array($result, $options);
101
    }
102
    if (!isset($result[NICK_HTML])) {
103
      $result = player_nick_render_array_to_html($result);
104
    }
105
    unset($result[NICK_HTML]);
106
    $result = implode('', playerNickOrder($result, $options));
107
  }
108
109
  return $result;
110
}
111
112
/**
113
 * Pack nick parts to string
114
 *
115
 * @param array $nick_array
116
 *
117
 * @return string
118
 */
119
function player_nick_compact($nick_array) {
120
  return json_encode(playerNickOrder($nick_array));
121
}
122
123
/**
124
 * Unpacks nick parts from string - if necessary
125
 *
126
 * @param array|string $nick_string
127
 *
128
 * @return array|string
129
 */
130
function player_nick_uncompact($nick_string) {
131
  $result = $nick_string;
132
  if (is_string($nick_string) && (strpos($nick_string, '{"') === 0 || strpos($nick_string, '["') === 0)) {
133
    $result = json_decode($nick_string, true);
134
    if (json_last_error() !== JSON_ERROR_NONE) {
135
      $result = $nick_string;
136
    }
137
  }
138
139
  return $result;
140
}
141
142
function player_nick_render_array_to_html($nick_array) {
143
  $result = null;
144
145
  return sn_function_call('player_nick_render_array_to_html', array($nick_array, &$result));
146
}
147
148
/**
149
 * @param array $nick_array
150
 * @param array $result
151
 *
152
 * @return array
153
 */
154
function sn_player_nick_render_array_to_html($nick_array, &$result) {
155
  static $iconCache = array();
156
157
  if (empty($iconCache['gender_' . $nick_array[NICK_GENDER]])) {
158
    $iconCache['gender_' . $nick_array[NICK_GENDER]] = classSupernova::$gc->skinModel->getImageCurrent("gender_{$nick_array[NICK_GENDER]}|html");
159
    $iconCache['icon_vacation'] = classSupernova::$gc->skinModel->getImageCurrent('icon_vacation|html');
160
    $iconCache['icon_birthday'] = classSupernova::$gc->skinModel->getImageCurrent('icon_birthday|html');
161
  }
162
163
  // ALL STRING ARE UNSAFE!!!
164
  if (isset($nick_array[NICK_BIRTHDAY])) {
165
    $result[NICK_BIRTHDAY] = $iconCache['icon_birthday'];
166
  }
167
168
  if (isset($nick_array[NICK_VACATION])) {
169
    $result[NICK_VACATION] = $iconCache['icon_vacation'];
170
  }
171
172
  if (isset($nick_array[NICK_RANK])) {
173
    $result[NICK_RANK] = "<div class='rank rank-{$nick_array[NICK_RANK]} rank-nick'></div>";
174
  }
175
176
  if (isset($nick_array[NICK_GENDER])) {
177
    $result[NICK_GENDER] = $iconCache['gender_' . $nick_array[NICK_GENDER]];
178
  }
179
180
  $highlight = null;
181
  if (isset($nick_array[NICK_PREMIUM])) {
182
    $highlight = classSupernova::$config->chat_highlight_premium;
0 ignored issues
show
Bug Best Practice introduced by
The property chat_highlight_premium does not exist on classConfig. Since you implemented __get, consider adding a @property annotation.
Loading history...
183
  }
184
185
  if (isset($nick_array[NICK_AUTH_LEVEL])) {
186
    switch ($nick_array[NICK_AUTH_LEVEL]) {
187
      case 4:
188
        $highlight = classSupernova::$config->chat_highlight_developer;
0 ignored issues
show
Bug Best Practice introduced by
The property chat_highlight_developer does not exist on classConfig. Since you implemented __get, consider adding a @property annotation.
Loading history...
189
      break;
190
191
      case 3:
192
        $highlight = classSupernova::$config->chat_highlight_admin;
0 ignored issues
show
Bug Best Practice introduced by
The property chat_highlight_admin does not exist on classConfig. Since you implemented __get, consider adding a @property annotation.
Loading history...
193
      break;
194
195
      case 2:
196
        $highlight = classSupernova::$config->chat_highlight_operator;
0 ignored issues
show
Bug Best Practice introduced by
The property chat_highlight_operator does not exist on classConfig. Since you implemented __get, consider adding a @property annotation.
Loading history...
197
      break;
198
199
      case 1:
200
        $highlight = classSupernova::$config->chat_highlight_moderator;
0 ignored issues
show
Bug Best Practice introduced by
The property chat_highlight_moderator does not exist on classConfig. Since you implemented __get, consider adding a @property annotation.
Loading history...
201
      break;
202
    }
203
204
  }
205
206
  if ($highlight) {
207
    list($result[NICK_HIGHLIGHT], $result[NICK_HIGHLIGHT_END]) = explode('$1', $highlight);
208
  }
209
210
  if (isset($nick_array[NICK_CLASS])) {
211
    $result[NICK_CLASS] = '<span ' . $nick_array[NICK_CLASS] . '>';
212
    $result[NICK_CLASS_END] = '</span>';
213
  }
214
215
  $result[NICK_NICK] = sys_safe_output($nick_array[NICK_NICK]);
216
217
  if (isset($nick_array[NICK_ALLY])) {
218
    $result[NICK_ALLY] = '[' . sys_safe_output($nick_array[NICK_ALLY]) . ']';
219
  }
220
221
  $result[NICK_HTML] = true;
222
223
  return $result;
224
}
225
226
/**
227
 * @param array      $render_user
228
 * @param array|bool $options - [
229
 *                            'color' => true,
230
 *                            'icons' => true,
231
 *                            'gender' => true,
232
 *                            'birthday' => true,
233
 *                            'ally' => true,
234
 *                            ]
235
 *
236
 * @return mixed
237
 */
238
function player_nick_render_current_to_array($render_user, $options = false) {
239
  $result = null;
240
241
  return sn_function_call('player_nick_render_current_to_array', array($render_user, $options, &$result));
242
}
243
244
/**
245
 * @param array      $render_user
246
 * @param array|bool $options - [
247
 *                            'color' => true,
248
 *                            'icons' => true,
249
 *                            'gender' => true,
250
 *                            'birthday' => true,
251
 *                            'ally' => true,
252
 *                            ]
253
 * @param array      $result
254
 *
255
 * @return mixed
256
 */
257
function sn_player_nick_render_current_to_array($render_user, $options = false, &$result) {
258
  /*
0 ignored issues
show
Unused Code Comprehensibility introduced by
45% 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...
259
  $options = $options !== true ? $options :
260
    array(
261
      'color' => true,
262
      'icons' => true,
263
      'gender' => true,
264
      'birthday' => true,
265
      'ally' => true,
266
    );
267
  */
268
269
270
  if ($render_user['user_birthday'] && ($options === true || isset($options['icons']) || isset($options['birthday'])) && (date('Y', SN_TIME_NOW) . date('-m-d', strtotime($render_user['user_birthday'])) == date('Y-m-d', SN_TIME_NOW))) {
271
    $result[NICK_BIRTHDAY] = '';
272
  }
273
274
  if ($options === true || (isset($options['icons']) && $options['icons']) || (isset($options['gender']) && $options['gender'])) {
275
    $result[NICK_GENDER] = $render_user['gender'] == GENDER_UNKNOWN ? 'unknown' : ($render_user['gender'] == GENDER_FEMALE ? 'female' : 'male');
276
  }
277
278
  if (($options === true || (isset($options['icons']) && $options['icons']) || (isset($options['vacancy']) && $options['vacancy'])) && $render_user['vacation']) {
0 ignored issues
show
introduced by
Consider adding parentheses for clarity. Current Interpretation: {currentAssign}, Probably Intended Meaning: {alternativeAssign}
Loading history...
279
    $result[NICK_VACATION] = $render_user['vacation'];
280
  }
281
282
  if ($options === true || !empty($options['icons']) || !empty($options['player_rank'])) {
283
    $result[NICK_RANK] = classSupernova::$gc->playerLevelHelper->getPointLevel($render_user['total_points'], $render_user['authlevel']);
284
  }
285
286
  if ($options === true || (isset($options['color']) && $options['color'])) {
287
    if ($user_auth_level = $render_user['authlevel']) {
288
      $result[NICK_AUTH_LEVEL] = $user_auth_level;
289
    }
290
    if ($user_premium = mrc_get_level($render_user, false, UNIT_PREMIUM)) {
0 ignored issues
show
Bug introduced by
false of type false is incompatible with the type array expected by parameter $planet of mrc_get_level(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

290
    if ($user_premium = mrc_get_level($render_user, /** @scrutinizer ignore-type */ false, UNIT_PREMIUM)) {
Loading history...
291
      $result[NICK_PREMIUM] = $user_premium;
292
    }
293
  }
294
295
  if ((isset($options['class']) && $options['class'])) {
296
    $result[NICK_CLASS] = (isset($result_options[NICK_CLASS]) ? ' ' . $result_options[NICK_CLASS] : '') . $options['class'];
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $result_options does not exist. Did you maybe mean $result?
Loading history...
297
  }
298
299
  if ($render_user['ally_tag'] && ($options === true || (isset($options['ally']) && $options['ally']))) {
300
    $result[NICK_ALLY] = $render_user['ally_tag'];
301
  }
302
303
  $result[NICK_NICK] = $render_user['username'];
304
305
  return $result;
306
}
307
308