Completed
Push — work-fleets ( d28adc...7496c9 )
by SuperNova.WS
06:01
created

BuddyView::makeTemplate()   C

Complexity

Conditions 8
Paths 544

Size

Total Lines 65
Code Lines 44

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
eloc 44
c 1
b 0
f 0
nc 544
nop 2
dl 0
loc 65
rs 5.1111
ccs 0
cts 49
cp 0
crap 72

How to fix   Long Method   

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 30.07.2016 20:52
4
 */
5
namespace Buddy;
6
use Common\GlobalContainer;
7
8
9
class BuddyView {
10
11
  /**
12
   * @param GlobalContainer $gc
13
   * @param array           $user
14
   *
15
   * @return \template
16
   */
17
  public function makeTemplate($gc, $user) {
18
    $playerLocale = $gc->localePlayer;
19
20
    $result = array();
21
    try {
22
      sn_db_transaction_start();
23
24
      $cBuddy = new BuddyRoutingParams();
25
26
      $cBuddy->gc = $gc;
27
      $cBuddy->buddy_id = sys_get_param_id('buddy_id');
28
      $cBuddy->mode = sys_get_param_str('mode');
29
      $cBuddy->newFriendIdSafe = sys_get_param_id('request_user_id');
30
      $cBuddy->new_friend_name_unsafe = sys_get_param_str_unsafe('request_user_name');
31
      $cBuddy->new_request_text_unsafe = sys_get_param_str_unsafe('request_text');
32
      $cBuddy->playerArray = $user;
33
34
      $cBuddy->playerId = function (BuddyRoutingParams $cBuddy) {
35
        return $cBuddy->playerArray['id'];
36
      };
37
      $cBuddy->playerName = function (BuddyRoutingParams $cBuddy) {
38
        return $cBuddy->playerArray['username'];
39
      };
40
      $cBuddy->playerNameAndCoordinates = function (BuddyRoutingParams $cBuddy) {
41
        return "{$cBuddy->playerArray['username']} " . uni_render_coordinates($cBuddy->playerArray);
42
      };
43
44
      $gc->buddy->route($cBuddy);
0 ignored issues
show
Bug introduced by
The method route cannot be called on $gc->buddy (of type callable).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
45
    } catch (BuddyException $e) {
46
      $exceptionCode = \ResultMessages::parseException($e, $result);
47
48
      $exceptionCode == ERR_NONE ? sn_db_transaction_commit() : sn_db_transaction_rollback();
49
    }
50
    sn_db_transaction_rollback();
51
    unset($buddy);
52
53
    empty($template_result) ? $template_result = array() : false;
0 ignored issues
show
Bug introduced by
The variable $template_result seems only to be defined at a later point. As such the call to empty() seems to always evaluate to true.

This check marks calls to isset(...) or empty(...) that are found before the variable itself is defined. These will always have the same result.

This is likely the result of code being shifted around. Consider removing these calls.

Loading history...
54
55
    foreach (BuddyModel::db_buddy_list_by_user($gc->db, $user['id']) as $row) {
56
      $row['BUDDY_REQUEST'] = sys_bbcodeParse($row['BUDDY_REQUEST']);
57
58
      $row['BUDDY_ACTIVE'] = $row['BUDDY_STATUS'] == BUDDY_REQUEST_ACTIVE;
59
      $row['BUDDY_DENIED'] = $row['BUDDY_STATUS'] == BUDDY_REQUEST_DENIED;
60
      $row['BUDDY_INCOMING'] = $row['BUDDY_OWNER_ID'] == $user['id'];
61
      $row['BUDDY_ONLINE'] = floor((SN_TIME_NOW - $row['onlinetime']) / 60);
62
63
      $template_result['.']['buddy'][] = $row;
64
    }
65
66
    $template_result += array(
67
      'PAGE_HEADER'       => $playerLocale['buddy_buddies'],
68
      'PAGE_HINT'         => $playerLocale['buddy_hint'],
69
      'USER_ID'           => $user['id'],
70
      'REQUEST_USER_ID'   => isset($new_friend_row['id']) ? $new_friend_row['id'] : 0,
0 ignored issues
show
Bug introduced by
The variable $new_friend_row seems to never exist, and therefore isset should always return false. Did you maybe rename this variable?

This check looks for calls to isset(...) or empty() on variables that are yet undefined. These calls will always produce the same result and can be removed.

This is most likely caused by the renaming of a variable or the removal of a function/method parameter.

Loading history...
71
      'REQUEST_USER_NAME' => isset($new_friend_row['username']) ? $new_friend_row['username'] : '',
72
    );
73
74
    $template_result['.']['result'] = is_array($template_result['.']['result']) ? $template_result['.']['result'] : array();
75
    $template_result['.']['result'] += $result;
76
77
    $template = gettemplate('buddy', true);
78
    $template->assign_recursive($template_result);
79
80
    return $template;
81
  }
82
83
}
84