Queue   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 67
Duplicated Lines 100 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 51.85%

Importance

Changes 0
Metric Value
wmc 10
lcom 0
cbo 1
dl 67
loc 67
rs 10
c 0
b 0
f 0
ccs 14
cts 27
cp 0.5185

5 Methods

Rating   Name   Duplication   Size   Complexity  
A enqueueMultiple() 8 8 2
A fromArray() 13 13 3
A toArray() 13 13 3
A __toString() 4 4 1
A jsonSerialize() 4 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 View Code Duplication
class Queue extends SplQueue implements QueueInterface, \JsonSerializable
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

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