Completed
Push — master ( 7a5aed...4799e1 )
by Kacper
06:25
created

Stanza::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 1
eloc 4
nc 1
nop 3
dl 0
loc 7
rs 9.4285
c 2
b 0
f 1
1
<?php
2
/**
3
 * XMPP Library
4
 *
5
 * Copyright (C) 2016, Some right 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\Stanza;
17
18
19
use Kadet\Xmpp\Jid;
20
use Kadet\Xmpp\Xml\XmlElement;
21
22
/**
23
 * Class Stanza
24
 * @package Kadet\Xmpp\Stanza
25
 *
26
 * @property Jid|null $from Jid representing "from" stanza attribute
27
 * @property Jid|null $to   Jid representing "to" stanza attribute
28
 * @property string   $type Stanza type
29
 * @property string   $id   Unique stanza id
30
 */
31
class Stanza extends XmlElement
32
{
33
    private $_from = false;
34
    private $_to   = false;
35
36
    /**
37
     * Stanza constructor.
38
     * @param string   $kind    Stanza kind. According to XMPP RFC, one of "iq", "message", "presence"
39
     * @param array    $options {
40
     *     @var Jid    $from    Jid representing "from" stanza attribute
41
     *     @var Jid    $to      Jid representing "to" stanza attribute
42
     *     @var string $id      Unique id, will be generated if omitted
43
     *     @var string $type    Stanza type
44
     * }
45
     * @param mixed    $content Content to append
46
     */
47
    public function __construct(string $kind, array $options = [], $content = null)
48
    {
49
        parent::__construct($kind, 'jabber:client', $content);
50
51
        $this->regenerateId($this->localName);
52
        $this->applyOptions($options);
53
    }
54
55
    public function getFrom()
56
    {
57
        if($this->_from === false) {
58
            $this->_from = $this->hasAttribute('from') ? new Jid($this->_from) : null;
0 ignored issues
show
Documentation introduced by
$this->_from is of type boolean, but the function expects a string.

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...
Documentation Bug introduced by
It seems like $this->hasAttribute('fro...id($this->_from) : null can also be of type object<Kadet\Xmpp\Jid>. However, the property $_from is declared as type boolean. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
59
        }
60
61
        return $this->_from;
62
    }
63
64
    public function getTo()
65
    {
66
        if($this->_to === false) {
67
            $this->_to = $this->hasAttribute('to') ? new Jid($this->_to) : null;
0 ignored issues
show
Documentation introduced by
$this->_to is of type boolean, but the function expects a string.

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...
Documentation Bug introduced by
It seems like $this->hasAttribute('to'...\Jid($this->_to) : null can also be of type object<Kadet\Xmpp\Jid>. However, the property $_to is declared as type boolean. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
68
        }
69
70
        return $this->_to;
71
    }
72
73
    public function getType()
74
    {
75
        return $this->getAttribute('type');
76
    }
77
78
    public function getId()
79
    {
80
        return $this->getAttribute('id');
81
    }
82
83
    public function setFrom($from)
84
    {
85
        if($from instanceof Jid) {
86
            $this->_from = $from instanceof Jid ? $from : new Jid($from);
0 ignored issues
show
Documentation Bug introduced by
It seems like $from instanceof \Kadet\... \Kadet\Xmpp\Jid($from) of type object<Kadet\Xmpp\Jid> is incompatible with the declared type boolean of property $_from.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
87
        }
88
89
        $this->setAttribute('from', (string)$from);
90
    }
91
92
    public function setTo($to)
93
    {
94
        if($to instanceof Jid) {
95
            $this->_to = $to instanceof Jid ? $to : new Jid($to);
0 ignored issues
show
Documentation Bug introduced by
It seems like $to instanceof \Kadet\Xm...ew \Kadet\Xmpp\Jid($to) of type object<Kadet\Xmpp\Jid> is incompatible with the declared type boolean of property $_to.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
96
        }
97
98
        $this->setAttribute('to', (string)$to);
99
    }
100
101
    public function setType(string $type)
102
    {
103
        $this->setAttribute('type', $type);
104
    }
105
106
    public function setId(string $id)
107
    {
108
        $this->setAttribute('id', $id);
109
    }
110
111
    public function regenerateId(string $prefix = null)
112
    {
113
        $this->id = uniqid($prefix, true);
114
    }
115
116
    public function response()
117
    {
118
        $response = static::plain($this->name, $this->namespace);
119
120
        $response->to   = $this->from;
121
        $response->from = $this->to;
122
        $response->id   = $this->id;
123
124
        return $response;
125
    }
126
127
    /**
128
     * Initializes element with given name and URI
129
     *
130
     * @param string $name Element name, including prefix if needed
131
     * @param string $uri  Namespace URI of element
132
     */
133
    protected function init(string $name, string $uri = null)
134
    {
135
        parent::init($name, $uri);
136
        $this->regenerateId();
137
    }
138
139
140
}
141