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

SortedArrayHashMap   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
c 1
b 0
f 0
lcom 1
cbo 2
dl 0
loc 62
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 3
A set() 0 6 1
A removeAt() 0 9 2
A sort() 0 8 2
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
 * SortedArrayHashMap Class.
17
 *
18
 * @author Karel Osorio Ramírez <[email protected]>
19
 * @author Ivannis Suárez Jerez <[email protected]>
20
 */
21
class SortedArrayHashMap extends ArrayHashMap
22
{
23
    /**
24
     * @var ComparatorInterface
25
     */
26
    protected $criteria;
27
28
    /**
29
     * SortedArrayHashMap constructor.
30
     *
31
     * @param array                    $elements
32
     * @param ComparatorInterface|null $criteria
33
     */
34
    public function __construct(array $elements = array(), ComparatorInterface $criteria = null)
35
    {
36
        if ($criteria === null) {
37
            $criteria = new Comparator();
38
        }
39
40
        $this->criteria = $criteria;
41
        foreach ($elements as $key => $element) {
42
            parent::set($key, $element);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (set() instead of __construct()). Are you sure this is correct? If so, you might want to change this to $this->set().

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...
43
        }
44
45
        $this->sort();
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51
    public function set($key, $value)
52
    {
53
        parent::set($key, $value);
54
55
        $this->sort();
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    public function removeAt($key)
62
    {
63
        $removed = parent::removeAt($key);
64
        if ($removed !== null) {
65
            $this->sort();
66
        }
67
68
        return $removed;
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74
    public function sort(ComparatorInterface $criteria = null)
75
    {
76
        if ($criteria !== null) {
77
            $this->criteria = $criteria;
78
        }
79
80
        parent::sort($this->criteria);
81
    }
82
}
83