Completed
Push — master ( d6f0bd...41a21b )
by Ivannis Suárez
05:43
created

SortedArraySet::add()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
/**
3
 * This file is part of the Cubiche package.
4
 *
5
 * Copyright (c) Cubiche
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace Cubiche\Core\Collections\ArrayCollection;
11
12
use Cubiche\Core\Comparable\Comparator;
13
use Cubiche\Core\Comparable\ComparatorInterface;
14
15
/**
16
 * SortedArraySet Class.
17
 *
18
 * @author Karel Osorio Ramírez <[email protected]>
19
 * @author Ivannis Suárez Jerez <[email protected]>
20
 */
21
class SortedArraySet extends ArraySet
22
{
23
    /**
24
     * @var ComparatorInterface
25
     */
26
    protected $criteria;
27
28
    /**
29
     * SortedArraySet constructor.
30
     *
31
     * @param array                    $elements
32
     * @param ComparatorInterface|null $criteria
33
     */
34 View Code Duplication
    public function __construct(array $elements = array(), ComparatorInterface $criteria = null)
0 ignored issues
show
Duplication introduced by
This method 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...
35
    {
36
        if ($criteria === null) {
37
            $criteria = new Comparator();
38
        }
39
40
        $this->criteria = $criteria;
41
        parent::__construct($elements);
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47
    public function add($element)
48
    {
49
        if (!$this->contains($element)) {
50
            $this->elements[] = $element;
51
52
            $this->sort();
53
        }
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59
    public function addAll($elements)
60
    {
61
        $this->validateTraversable($elements);
62
63
        $changed = false;
64
        foreach ($elements as $element) {
65
            if (!$this->contains($element)) {
66
                $this->elements[] = $element;
67
                $changed = true;
68
            }
69
        }
70
71
        if ($changed) {
72
            $this->sort();
73
        }
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79
    public function remove($element)
80
    {
81
        if (parent::remove($element)) {
82
            $this->sort();
83
84
            return true;
85
        }
86
87
        return false;
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93 View Code Duplication
    public function removeAll($elements)
0 ignored issues
show
Duplication introduced by
This method 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...
94
    {
95
        $this->validateTraversable($elements);
96
97
        $changed = false;
98
        foreach ($elements as $element) {
99
            if (parent::remove($element)) {
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (remove() instead of removeAll()). Are you sure this is correct? If so, you might want to change this to $this->remove().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
100
                $changed = true;
101
            }
102
        }
103
104
        if ($changed) {
105
            $this->sort();
106
        }
107
108
        return $changed;
109
    }
110
111
    /**
112
     * {@inheritdoc}
113
     */
114
    public function sort(ComparatorInterface $criteria = null)
115
    {
116
        if ($criteria !== null) {
117
            $this->criteria = $criteria;
118
        }
119
120
        parent::sort($this->criteria);
121
    }
122
123
    /**
124
     * {@inheritdoc}
125
     */
126
    public function offsetUnset($offset)
127
    {
128
        parent::offsetUnset($offset);
129
130
        $this->sort();
131
    }
132
}
133