Completed
Push — trunk ( e6cc80...a060bf )
by SuperNova.WS
05:00
created

playerNickOrder()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 7
c 0
b 0
f 0
nc 3
nop 1
dl 0
loc 14
rs 9.4285
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 41 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_RACE,
13
  NICK_GENDER,
14
  NICK_AWARD,
15
  NICK_VACATION,
16
  NICK_BIRTHSDAY,
17
  NICK_PREMIUM,
18
  NICK_AUTH_LEVEL,
19
20
  NICK_HIGHLIGHT,
21
  NICK_CLASS,
22
  NICK_NICK_CLASS,
23
  NICK_NICK,
24
  NICK_NICK_CLASS_END,
25
  NICK_ALLY_CLASS,
26
  NICK_ALLY,
27
  NICK_ALLY_CLASS_END,
28
  NICK_CLASS_END,
29
  NICK_HIGHLIGHT_END,
30
31
  NICK_LAST,
32
];
33
34
/**
35
 * Ordering nick parts according to predefined part order
36
 *
37
 * @param array $array
38
 *
39
 * @return array
40
 */
41
function playerNickOrder($array) {
42
  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...
43
44
  $result = [];
45
  // Rearranging nick parts according to sort array
46
  foreach ($nickSorting as $nickPartId) {
47
    if (array_key_exists($nickPartId, $array)) {
48
      $result[$nickPartId] = $array[$nickPartId];
49
      unset($array[$nickPartId]);
50
    }
51
  }
52
53
  // Adding what left of nick parts to resulting array
54
  return array_merge($result, $array);
55
}
56
57
// Может принимать: (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...
58
function player_nick_render_to_html($result, $options = false) {
59
  $result = player_nick_uncompact($result);
60
61
  if (is_array($result)) {
62
    if (isset($result['id'])) {
63
      $result = player_nick_render_current_to_array($result, $options);
64
    }
65
    if (!isset($result[NICK_HTML])) {
66
      $result = player_nick_render_array_to_html($result);
67
    }
68
    unset($result[NICK_HTML]);
69
    $result = implode('', playerNickOrder($result));
70
  }
71
72
  return $result;
73
}
74
75
/**
76
 * Pack nick parts to string
77
 *
78
 * @param array $nick_array
79
 *
80
 * @return string
81
 */
82
function player_nick_compact($nick_array) {
83
  return json_encode(playerNickOrder($nick_array));
84
}
85
86
/**
87
 * Unpacks nick parts from string - if necessary
88
 *
89
 * @param array|string $nick_string
90
 *
91
 * @return array|string
92
 */
93
function player_nick_uncompact($nick_string) {
94
  $result = $nick_string;
95
  if (is_string($nick_string) && strpos($nick_string, '{"') === 0) {
96
    $result = json_decode($nick_string, true);
97
    if (json_last_error() !== JSON_ERROR_NONE) {
98
      $result = $nick_string;
99
    }
100
  }
101
102
  return $result;
103
}
104
105
function player_nick_render_array_to_html($nick_array) {
106
  $result = null;
107
108
  return sn_function_call('player_nick_render_array_to_html', array($nick_array, &$result));
109
}
110
111
/**
112
 * @param array $nick_array
113
 * @param array $result
114
 *
115
 * @return array
116
 */
117
function sn_player_nick_render_array_to_html($nick_array, &$result) {
118
  static $iconCache = array();
119
120
  if (empty($iconCache['gender_' . $nick_array[NICK_GENDER]])) {
121
    $iconCache['gender_' . $nick_array[NICK_GENDER]] = classSupernova::$gc->skinModel->getImageCurrent("gender_{$nick_array[NICK_GENDER]}|html");
122
    $iconCache['icon_vacation'] = classSupernova::$gc->skinModel->getImageCurrent('icon_vacation|html');
123
    $iconCache['icon_birthday'] = classSupernova::$gc->skinModel->getImageCurrent('icon_birthday|html');
124
  }
125
126
  // ALL STRING ARE UNSAFE!!!
127
  if (isset($nick_array[NICK_BIRTHSDAY])) {
128
    $result[NICK_BIRTHSDAY] = $iconCache['icon_birthday'];
129
  }
130
131
  if (isset($nick_array[NICK_VACATION])) {
132
    $result[NICK_VACATION] = $iconCache['icon_vacation'];
133
  }
134
135
  if (isset($nick_array[NICK_GENDER])) {
136
    $result[NICK_GENDER] = $iconCache['gender_' . $nick_array[NICK_GENDER]];
137
  }
138
139
  $highlight = null;
140
  if (isset($nick_array[NICK_PREMIUM])) {
141
    $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...
142
  }
143
144
  if (isset($nick_array[NICK_AUTH_LEVEL])) {
145
    switch ($nick_array[NICK_AUTH_LEVEL]) {
146
      case 4:
147
        $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...
148
      break;
149
150
      case 3:
151
        $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...
152
      break;
153
154
      case 2:
155
        $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...
156
      break;
157
158
      case 1:
159
        $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...
160
      break;
161
    }
162
163
  }
164
165
  if ($highlight) {
166
    list($result[NICK_HIGHLIGHT], $result[NICK_HIGHLIGHT_END]) = explode('$1', $highlight);
167
  }
168
169
  if (isset($nick_array[NICK_CLASS])) {
170
    $result[NICK_CLASS] = '<span ' . $nick_array[NICK_CLASS] . '>';
171
    $result[NICK_CLASS_END] = '</span>';
172
  }
173
174
  $result[NICK_NICK] = sys_safe_output($nick_array[NICK_NICK]);
175
176
  if (isset($nick_array[NICK_ALLY])) {
177
    $result[NICK_ALLY] = '[' . sys_safe_output($nick_array[NICK_ALLY]) . ']';
178
  }
179
180
  $result[NICK_HTML] = true;
181
182
  return $result;
183
}
184
185
/**
186
 * @param array      $render_user
187
 * @param array|bool $options - [
188
 *                            'color' => true,
189
 *                            'icons' => true,
190
 *                            'gender' => true,
191
 *                            'birthday' => true,
192
 *                            'ally' => true,
193
 *                            ]
194
 *
195
 * @return mixed
196
 */
197
function player_nick_render_current_to_array($render_user, $options = false) {
198
  $result = null;
199
200
  return sn_function_call('player_nick_render_current_to_array', array($render_user, $options, &$result));
201
}
202
203
/**
204
 * @param array      $render_user
205
 * @param array|bool $options - [
206
 *                            'color' => true,
207
 *                            'icons' => true,
208
 *                            'gender' => true,
209
 *                            'birthday' => true,
210
 *                            'ally' => true,
211
 *                            ]
212
 * @param array      $result
213
 *
214
 * @return mixed
215
 */
216
function sn_player_nick_render_current_to_array($render_user, $options = false, &$result) {
217
  /*
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...
218
  $options = $options !== true ? $options :
219
    array(
220
      'color' => true,
221
      'icons' => true,
222
      'gender' => true,
223
      'birthday' => true,
224
      'ally' => true,
225
    );
226
  */
227
228
229
  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))) {
230
    $result[NICK_BIRTHSDAY] = '';
231
  }
232
233
  if ($options === true || (isset($options['icons']) && $options['icons']) || (isset($options['gender']) && $options['gender'])) {
234
    $result[NICK_GENDER] = $render_user['gender'] == GENDER_UNKNOWN ? 'unknown' : ($render_user['gender'] == GENDER_FEMALE ? 'female' : 'male');
235
  }
236
237
  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...
238
    $result[NICK_VACATION] = $render_user['vacation'];
239
  }
240
241
  if ($options === true || (isset($options['color']) && $options['color'])) {
242
    if ($user_auth_level = $render_user['authlevel']) {
243
      $result[NICK_AUTH_LEVEL] = $user_auth_level;
244
    }
245
    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

245
    if ($user_premium = mrc_get_level($render_user, /** @scrutinizer ignore-type */ false, UNIT_PREMIUM)) {
Loading history...
246
      $result[NICK_PREMIUM] = $user_premium;
247
    }
248
  }
249
250
  if ((isset($options['class']) && $options['class'])) {
251
    $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...
252
  }
253
254
  if ($render_user['ally_tag'] && ($options === true || (isset($options['ally']) && $options['ally']))) {
255
    $result[NICK_ALLY] = $render_user['ally_tag'];
256
  }
257
258
  $result[NICK_NICK] = $render_user['username'];
259
260
  return $result;
261
}
262