Completed
Push — work-fleets ( b19a67...de8005 )
by SuperNova.WS
06:26
created

buddy.php (4 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/**
4
 * buddy.php
5
 *   Friend system
6
 *
7
 * v3.0 Fully rewrote by Gorlum for http://supernova.ws
8
 *   [!] Full rewrote from scratch
9
 *
10
 * Idea from buddy.php Created by Perberos. All rights reversed (C) 2006
11
 * */
12
include('common.' . substr(strrchr(__FILE__, '.'), 1));
13
14
class_exists('Buddy');
15
16
17
18
/**
19
 * @var array $user
20
 */
21
global $user;
22
23
lng_include('buddy');
24
25
$result = array();
26
try {
27
  $buddy_id = sys_get_param_id('buddy_id');
28
  if ($buddy_id) {
29
    sn_db_transaction_start();
30
    /**
31
     * @var BuddyModel $buddy
32
     */
33
    $buddy = classSupernova::$gc->dbRowOperator->getById(classSupernova::$gc->buddy, $buddy_id);
0 ignored issues
show
\classSupernova::$gc->buddy is of type callable, but the function expects a object<Entity>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
The method getById does only exist in DbRowSimple, 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...
34
35
    if ($buddy->isEmpty()) {
36
      throw new Exception('buddy_err_not_exist', ERR_ERROR);
37
    }
38
39
    $mode = sys_get_param_str('mode');
40
    switch ($mode) {
41
      case 'accept':
42
        $buddy->accept($user);
43
      break;
44
      case 'delete':
45
        $buddy->decline($user);
46
      break;
47
    }
48
    unset($buddy);
49
  }
50
51
  // New request?
52
  // Checking for user ID - in case if it was request from outside buddy system
53
  $new_friend_id_safe = sys_get_param_id('request_user_id');
54
  $new_friend_name = sys_get_param_str_unsafe('request_user_name');
55
  if($new_friend_id_safe || $new_friend_name) {
56
    sn_db_transaction_start();
57
    $buddy = classSupernova::$gc->buddy;
58
    $new_request_text = sys_get_param_str('request_text');
59
    $buddy->beFriend($user, $new_friend_id_safe, $new_friend_name, $new_request_text);
0 ignored issues
show
The method beFriend cannot be called on $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...
60
  }
61
} catch (Exception $e) {
62
  $result[] = array(
63
    'STATUS'  => in_array($e->getCode(), array(ERR_NONE, ERR_WARNING, ERR_ERROR)) ? $e->getCode() : ERR_ERROR,
64
    'MESSAGE' => classLocale::$lang[$e->getMessage()],
65
  );
66
  $e->getCode() == ERR_NONE ? sn_db_transaction_commit() : sn_db_transaction_rollback();
67
}
68
69
// TODO - Это просто заглушка. Дойдут руки - разобраться, в чём проблема
70
sn_db_transaction_rollback();
71
72
empty($template_result) ? $template_result = array() : false;
73
74
foreach (Buddy::db_buddy_list_by_user(classSupernova::$gc->db, $user['id']) as $row) {
0 ignored issues
show
It seems like \classSupernova::$gc->db can also be of type object<Closure>; however, Buddy::db_buddy_list_by_user() does only seem to accept object<db_mysql>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
75
  $row['BUDDY_REQUEST'] = sys_bbcodeParse($row['BUDDY_REQUEST']);
76
77
  $row['BUDDY_ACTIVE'] = $row['BUDDY_STATUS'] == BUDDY_REQUEST_ACTIVE;
78
  $row['BUDDY_DENIED'] = $row['BUDDY_STATUS'] == BUDDY_REQUEST_DENIED;
79
  $row['BUDDY_INCOMING'] = $row['BUDDY_OWNER_ID'] == $user['id'];
80
  $row['BUDDY_ONLINE'] = floor((SN_TIME_NOW - $row['onlinetime']) / 60);
81
82
  $template_result['.']['buddy'][] = $row;
83
}
84
85
$template_result += array(
86
  'PAGE_HEADER'       => classLocale::$lang['buddy_buddies'],
87
  'PAGE_HINT'         => classLocale::$lang['buddy_hint'],
88
  'USER_ID'           => $user['id'],
89
  'REQUEST_USER_ID'   => isset($new_friend_row['id']) ? $new_friend_row['id'] : 0,
90
  'REQUEST_USER_NAME' => isset($new_friend_row['username']) ? $new_friend_row['username'] : '',
91
);
92
93
$template_result['.']['result'] = is_array($template_result['.']['result']) ? $template_result['.']['result'] : array();
94
$template_result['.']['result'] += $result;
95
96
$template = gettemplate('buddy', true);
97
$template->assign_recursive($template_result);
98
99
display($template);
100