Completed
Push — master ( 026b2f...fbea6a )
by Taosikai
11:48
created

examples/poll_message.php (2 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
 */
5
use Slince\SmartQQ\Client;
6
use Slince\SmartQQ\Message\Response\FriendMessage;
7
use Slince\SmartQQ\Message\Response\GroupMessage;
8
use Slince\SmartQQ\Message\Response\DiscussMessage;
9
use Slince\SmartQQ\Entity\Discuss;
10
use Slince\SmartQQ\Entity\DiscussDetail;
11
12
include __DIR__.'/bootstrap.php';
13
14
//创建smartQQ客户端
15
$smartQQ = new Client(getCredential());
16
$friends = $smartQQ->getFriends();
17
$groups = $smartQQ->getGroups();
18
$discusses = $smartQQ->getDiscusses();
19
20
/**
21
 * 获取讨论组详情.
22
 *
23
 * @param Discuss $discuss
24
 *
25
 * @return DiscussDetail
26
 */
27
function getDiscussDetails(Discuss $discuss)
28
{
29
    global $smartQQ;
30
    static $discussDetails = [];
31
    if (isset($discussDetails[$discuss->getId()])) {
32
        return $discussDetails[$discuss->getId()];
33
    }
34
35
    return $discussDetails[$discuss->getId()] = $smartQQ->getDiscussDetail($discuss);
36
}
37
38
while (true) {
39
    $messages = $smartQQ->pollMessages();
40
    if ($messages) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $messages of type Slince\SmartQQ\Message\Response\Message[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
41
        printR('收到消息'.PHP_EOL);
42
        foreach ($messages as $message) {
43
            if ($message instanceof FriendMessage) {
44
                $friend = $friends->firstByAttribute('uin', $message->getFromUin());
45
                $content = sprintf("好友[%s] -- %s: \r\n %s",
46
                    $friend->getNick(),
47
                    date('H:i:s'),
48
                    $message
49
                );
50
            } elseif ($message instanceof GroupMessage) {
51
                $group = $groups->firstByAttribute('id', $message->getFromUin());
52
                $content = sprintf("群[%s] -- %s: \r\n %s",
53
                    $group->getName(),
54
                    date('H:i:s'),
55
                    $message
56
                );
57
            } elseif ($message instanceof DiscussMessage) {
58
                $discuss = $discusses->firstByAttribute('id', $message->getFromUin());
59
                $sender = getDiscussDetails($discuss)->getMembers()->firstByAttribute('uin', $message->getSendUin());
0 ignored issues
show
$discuss is of type object|null, but the function expects a object<Slince\SmartQQ\Entity\Discuss>.

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...
60
                $content = sprintf("讨论组[%s] [%s]-- %s: \r\n %s",
61
                    $discuss->getName(),
62
                    $sender->getNick(),
63
                    date('H:i:s'),
64
                    $message
65
                );
66
            } else {
67
                continue;
68
            }
69
            printR($content);
70
            echo str_repeat(PHP_EOL, 2);
71
        }
72
    } else {
73
        //延缓2秒
74
        usleep(2000000);
75
    }
76
}