Completed
Push — master ( e86954...7cbcb1 )
by Ítalo
02:48
created

Queue::enqueueMultiple()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 9.4286
cc 2
eloc 4
nc 2
nop 1
crap 2
1
<?php
2
3
// Copyright (c) Lellys Informática. All rights reserved. See License.txt in the project root for license information.
4
namespace Collections;
5
6
use SplQueue;
7
8
/**
9
 * Represents a first-in, first-out collection of objects.
10
 */
11
class Queue extends SplQueue implements QueueInterface, \JsonSerializable
12
{
13
14
    /**
15
     * Adds multiples objects to the end of the Queue.
16
     * @param CollectionInterface|array $items The objects to add to the Queue. The value can be null.
17
     * @return $this|Queue
18
     */
19 1
    public function enqueueMultiple($items)
20
    {
21 1
        foreach ($items as $item) {
0 ignored issues
show
Bug introduced by
The expression $items of type object<Collections\CollectionInterface>|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
22 1
            $this->enqueue($item);
23
        }
24
25 1
        return $this;
26
    }
27
28
    public static function fromArray(array $arr)
29
    {
30
        $collection = new Queue();
31
        foreach ($arr as $v) {
32
            if (is_array($v)) {
33
                $collection->enqueue(static::fromArray($v));
34
            } else {
35
                $collection->enqueue($v);
36
            }
37
        }
38
39
        return $collection;
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45 2
    public function toArray()
46
    {
47 2
        $array = array();
48 2
        foreach ($this as $key => $value) {
49 2
            if ($value instanceof CollectionConvertableInterface) {
50
                $array[$key] = $value->toArray();
51
            } else {
52 2
                $array[$key] = $value;
53
            }
54
        }
55
56 2
        return $array;
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62 2
    public function __toString()
63
    {
64 2
        return get_class($this);
65
    }
66
67
    /**
68
     * (PHP 5 &gt;= 5.4.0)<br/>
69
     * Specify data which should be serialized to JSON
70
     * @link http://php.net/manual/en/jsonserializable.jsonserialize.php
71
     * @return mixed data which can be serialized by <b>json_encode</b>,
72
     * which is a value of any type other than a resource.
73
     */
74
    public function jsonSerialize()
75
    {
76
//        return $this->getIterator()->toArray();
77
    }
78
}
79