sn_player_nick_render_current_to_array()   F
last analyzed

Complexity

Conditions 39
Paths 14580

Size

Total Lines 64
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 39
eloc 26
nc 14580
nop 3
dl 0
loc 64
rs 0
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Created by Gorlum 04.12.2017 4:20
4
 */
5
6
global $nickSorting;
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;
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
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]] = SN::$gc->skinModel->getImageCurrent("gender_{$nick_array[NICK_GENDER]}|html");
159
    $iconCache['icon_vacation'] = SN::$gc->skinModel->getImageCurrent('icon_vacation|html');
160
    $iconCache['icon_birthday'] = SN::$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] = SN::$gc->playerLevelHelper->renderRank($nick_array[NICK_RANK], $nick_array[NICK_RANK_NO_TEXT]);
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 = SN::$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 = SN::$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 = SN::$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 = SN::$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 = SN::$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
  /*
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 (($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
    if(!empty($render_user['user_birthday'])) {
272
      $result[NICK_BIRTHDAY] = '';
273
    }
274
  }
275
276
  if ($options === true || (isset($options['icons']) && $options['icons']) || (isset($options['gender']) && $options['gender'])) {
277
    if (isset($render_user['gender'])) {
278
      $result[NICK_GENDER] = $render_user['gender'] == GENDER_UNKNOWN ? 'unknown' : ($render_user['gender'] == GENDER_FEMALE ? 'female' : 'male');
279
    }
280
  }
281
282
  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: ($options === true || Is...render_user['vacation'], Probably Intended Meaning: $options === true || Iss...ender_user['vacation'])
Loading history...
283
    if(isset($render_user['vacation'])) {
284
      $result[NICK_VACATION] = $render_user['vacation'];
285
    }
286
  }
287
288
  if ($options === true || !empty($options['icons']) || !empty($options['player_rank'])) {
289
    if(isset($render_user['total_points'])) {
290
      $result[NICK_RANK] = SN::$gc->playerLevelHelper->getPointLevel($render_user['total_points'], $render_user['authlevel']);
291
    }
292
  }
293
294
  if (!empty($options[NICK_RANK_NO_TEXT])) {
295
    $result[NICK_RANK_NO_TEXT] = $options[NICK_RANK_NO_TEXT];
296
  }
297
298
  if ($options === true || (isset($options['color']) && $options['color'])) {
299
    if ($user_auth_level = $render_user['authlevel']) {
300
      $result[NICK_AUTH_LEVEL] = $user_auth_level;
301
    }
302
    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

302
    if ($user_premium = mrc_get_level($render_user, /** @scrutinizer ignore-type */ false, UNIT_PREMIUM)) {
Loading history...
303
      $result[NICK_PREMIUM] = $user_premium;
304
    }
305
  }
306
307
  if ((isset($options['class']) && $options['class'])) {
308
//    $result[NICK_CLASS] = (isset($result_options[NICK_CLASS]) ? ' ' . $result_options[NICK_CLASS] : '') . $options['class'];
309
    $result[NICK_CLASS] = (isset($options[NICK_CLASS]) ? ' ' . $options[NICK_CLASS] : '') . $options['class'];
310
  }
311
312
  if ($render_user['ally_tag'] && ($options === true || (isset($options['ally']) && $options['ally']))) {
313
    if(isset($render_user['ally_tag'])) {
314
      $result[NICK_ALLY] = $render_user['ally_tag'];
315
    }
316
  }
317
318
  $result[NICK_NICK] = $render_user['username'];
319
320
  return $result;
321
}
322
323