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

Queue   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 54.55%

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 10
c 3
b 0
f 1
lcom 0
cbo 1
dl 0
loc 68
ccs 12
cts 22
cp 0.5455
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __toString() 0 4 1
A jsonSerialize() 0 4 1
A enqueueMultiple() 0 8 2
A fromArray() 0 13 3
A toArray() 0 13 3
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