Completed
Push — master ( a3140b...10de85 )
by Kacper
04:29
created

Stanza::init()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 5
ccs 0
cts 4
cp 0
crap 2
rs 9.4285
c 1
b 0
f 1
1
<?php
2
/**
3
 * Nucleus - XMPP Library for PHP
4
 *
5
 * Copyright (C) 2016, 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\Stanza;
17
18
19
use Kadet\Xmpp\Jid;
20
use Kadet\Xmpp\Xml\XmlElement;
21
use Kadet\Xmpp\Xml\XmlFactoryCollocations;
22
23
/**
24
 * Class Stanza
25
 * @package Kadet\Xmpp\Stanza
26
 *
27
 * @property Jid|null $from  Jid representing "from" stanza attribute
28
 * @property Jid|null $to    Jid representing "to" stanza attribute
29
 * @property string   $type  Stanza type
30
 * @property string   $id    Unique stanza id
31
 * @property Error    $error Error details
32
 */
33
class Stanza extends XmlElement implements XmlFactoryCollocations
34
{
35
    private $_from = false;
36
    private $_to   = false;
37
38
    /**
39
     * Stanza constructor.
40
     * @param string   $kind    Stanza kind. According to XMPP RFC, one of "iq", "message", "presence"
41
     * @param array    $options {
42
     *     @var Jid    $from    Jid representing "from" stanza attribute
43
     *     @var Jid    $to      Jid representing "to" stanza attribute
44
     *     @var string $id      Unique id, will be generated if omitted
45
     *     @var string $type    Stanza type
46
     * }
47
     */
48
    public function __construct(string $kind, array $options = [])
49
    {
50
        $this->regenerateId($kind);
51
        parent::__construct($kind, 'jabber:client', $options);
52
    }
53
54
    public function getFrom()
55
    {
56
        if((string)$this->_from !== $this->hasAttribute('from')) {
57
            $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...
58
        }
59
60
        return $this->_from;
61
    }
62
63
    public function getTo()
64
    {
65
        if((string)$this->_to !== $this->hasAttribute('to')) {
66
            $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...
67
        }
68
69
        return $this->_to;
70
    }
71
72
    public function getType()
73
    {
74
        return $this->getAttribute('type');
75
    }
76
77
    public function getId()
78
    {
79
        return $this->getAttribute('id');
80
    }
81
82
    public function getError()
83
    {
84
        return $this->element('error');
85
    }
86
87
    public function setFrom($from)
88
    {
89
        if($from instanceof Jid) {
90
            $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...
91
        }
92
93
        $this->setAttribute('from', (string)$from);
94
    }
95
96
    public function setTo($to)
97
    {
98
        if($to instanceof Jid) {
99
            $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...
100
        }
101
102
        $this->setAttribute('to', (string)$to);
103
    }
104
105
    public function setType(string $type)
106
    {
107
        $this->setAttribute('type', $type);
108
    }
109
110
    public function setId(string $id)
111
    {
112
        $this->setAttribute('id', $id);
113
    }
114
115
    public function regenerateId(string $prefix = null)
116
    {
117
        $this->id = uniqid($prefix, true);
118
    }
119
120
    public function response()
121
    {
122
        $response = static::plain($this->name, $this->namespace);
123
124
        $response->to   = $this->from;
125
        $response->from = $this->to;
126
        $response->id   = $this->id;
127
128
        return $response;
129
    }
130
131
    public static function getXmlCollocations() : array
132
    {
133
        return [
134
            [ Error::class, 'name' => 'error', 'uri' => 'jabber:client' ],
135
        ];
136
    }
137
}
138