Completed
Push — master ( 4aee11...dd7177 )
by Kacper
04:24
created

Roster::appendChild()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 1
dl 0
loc 11
ccs 0
cts 6
cp 0
crap 6
rs 9.4285
c 0
b 0
f 0
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\Iq\Query;
17
18
19
use Kadet\Xmpp\Exception\InvalidArgumentException;
20
use Kadet\Xmpp\Stanza\Iq\Query;
21
use Kadet\Xmpp\Stanza\Iq\Query\Roster\Item;
22
use function \Kadet\Xmpp\Utils\helper\format;
23
use Kadet\Xmpp\Xml\XmlFactoryCollocations;
24
25
/**
26
 * Class Roster
27
 * @package Kadet\Xmpp\Stanza\Iq\Query
28
 *
29
 * @property string $version Roster version (if server supports)
30
 * @property Query\Roster\Item[] $items Roster version (if server supports)
31
 */
32
class Roster extends Query implements XmlFactoryCollocations
33
{
34
    /**
35
     * Query constructor
36
     *
37
     * @param array  $options    {
38
     *     @var mixed               $content    Content of element
39
     *     @var array               $attributes Element attributes
40
     *     @var array               string      $version Roster version (if server supports)
41
     *     @var Query\Roster\Item[] $items      Roster version (if server supports)
42
     * }
43
     */
44
    public function __construct(array $options = [])
45
    {
46
        parent::__construct('jabber:iq:roster', 'query', $options);
47
    }
48
49
50
    #region Version
51
    /**
52
     * @return string
53
     */
54
    public function getVersion(): string
55
    {
56
        return $this->getAttribute('ver');
57
    }
58
59
    /**
60
     * @param string $version
61
     */
62
    public function setVersion(string $version)
63
    {
64
        $this->setAttribute('ver', $version);
65
    }
66
    #endregion
67
68
    #region Items
69
    /**
70
     * @return Query\Roster\Item[]
71
     */
72
    public function getItems(): array
73
    {
74
        return $this->all(Item::class);
75
    }
76
77
    /**
78
     * @param Query\Roster\Item[] $items
79
     */
80
    public function setItems(array $items)
81
    {
82
        $this->remove(\Kadet\Xmpp\Utils\filter\instance(Item::class));
83
        $this->append($items);
0 ignored issues
show
Documentation introduced by
$items is of type array<integer,object<Kad...\Iq\Query\Roster\Item>>, but the function expects a object<Kadet\Xmpp\Xml\XmlElement>|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...
84
    }
85
    #endregion
86
87
    protected function appendChild($element)
88
    {
89
        if(!$element instanceof Item) {
90
            throw new InvalidArgumentException(format('Only instances of roster item ({expected}) can be added into roster, tried to add {actual}', [
91
                'expected' => Item::class,
92
                'actual'   => get_class($element)
93
            ]));
94
        }
95
96
        return parent::appendChild($element);
97
    }
98
99
    public static function getXmlCollocations() : array
100
    {
101
        return [
102
            [ Item::class, 'name' => 'item', 'uri' => 'jabber:iq:roster' ]
103
        ];
104
    }
105
}
106