SubscriptionManager::init()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * Nucleus - XMPP Library for PHP
4
 *
5
 * Copyright (C) 2017, Some rights reserved.
6
 *
7
 * @author Kacper "Kadet" Donat <[email protected]>
8
 *
9
 * Contact with author:
10
 * Xmpp: [email protected]
11
 * E-mail: [email protected]
12
 *
13
 * From Kadet with love.
14
 */
15
16
namespace Kadet\Xmpp\Component\Subscription;
17
18
19
use Kadet\Xmpp\Component\Component;
20
use Kadet\Xmpp\Jid;
21
use Kadet\Xmpp\Stanza\Presence;
22
use Kadet\Xmpp\Utils\BetterEmitter;
23
24
use function \Kadet\Xmpp\Utils\filter\{
25
    stanza\type, in
26
};
27
28
class SubscriptionManager extends Component
29
{
30
    use BetterEmitter;
31
32
    protected function init()
33
    {
34 4
        $this->_client->on('presence', function(...$args) {
35 1
            $this->handleSubscriptionRequest(...$args);
0 ignored issues
show
Documentation introduced by
$args is of type array<integer,?>, but the function expects a object<Kadet\Xmpp\Stanza\Presence>.

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...
36 4
        }, type('subscribe'));
37 4
    }
38
39 1
    private function handleSubscriptionRequest(Presence $presence)
40
    {
41 1
        $this->emit('request', [ $presence ]);
42 1
    }
43
44
    /**
45
     * Sends subscription request presence to server.
46
     *
47
     * @param Jid|string $jid
48
     * @return \React\Promise\ExtendedPromiseInterface
49
     */
50 1
    public function subscribe($jid)
51
    {
52 1
        return $this->_client->send($this->presence('subscribe', $jid));
53
    }
54
55
    /**
56
     * Sends subscription removal request presence to server.
57
     *
58
     * @param Jid|string $jid
59
     * @return \React\Promise\ExtendedPromiseInterface
60
     */
61 1
    public function unsubscribe(Jid $jid)
62
    {
63 1
        return $this->_client->send($this->presence('unsubscribe', $jid));
64
    }
65
66
    /**
67
     * Sends subscription cancellation request presence to server.
68
     *
69
     * @param Jid|string $jid
70
     * @return \React\Promise\ExtendedPromiseInterface
71
     */
72 1
    public function cancel($jid)
73
    {
74 1
        return $this->_client->send($this->presence('unsubscribed', $jid));
75
    }
76
77 3
    private function presence($type, $jid)
78
    {
79 3
        $jid = $jid instanceof Jid ? $jid : new Jid($jid);
80 3
        return new Presence([
81 3
            'type' => $type,
82 3
            'to' => $jid->bare()
83
        ]);
84
    }
85
}