HashMap   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 97
Duplicated Lines 46.39 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 33.33%

Importance

Changes 0
Metric Value
wmc 18
lcom 1
cbo 8
dl 45
loc 97
ccs 12
cts 36
cp 0.3333
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 20 20 4
C add() 25 34 12
A fromArray() 0 9 2

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
/**
4
 * \AppserverIo\Collections\HashMap
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * This source file is subject to the Open Software License (OSL 3.0)
9
 * that is available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * PHP version 5
13
 *
14
 * @author    Tim Wagner <[email protected]>
15
 * @copyright 2015 TechDivision GmbH <[email protected]>
16
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
 * @link      https://github.com/appserver-io/collections
18
 * @link      http://www.appserver.io
19
 */
20
21
namespace AppserverIo\Collections;
22
23
use \AppserverIo\Lang\Strng;
24
use \AppserverIo\Lang\Integer;
25
use \AppserverIo\Lang\Flt;
26
use \AppserverIo\Lang\Boolean;
27
use \AppserverIo\Lang\NullPointerException;
28
use \AppserverIo\Lang\ClassCastException;
29
30
/**
31
 * This class is the implementation of a HashMap.
32
 *
33
 * @author    Tim Wagner <[email protected]>
34
 * @copyright 2015 TechDivision GmbH <[email protected]>
35
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
36
 * @link      https://github.com/appserver-io/collections
37
 * @link      http://www.appserver.io
38
 */
39
class HashMap extends AbstractMap
40
{
41
42
    /**
43
     * Standard constructor that adds the array passed
44
     * as parameter to the internal member variable.
45
     *
46
     * @param array $items An array to initialize the HashMap
47
     *
48
     * @throws \AppserverIo\Lang\ClassCastException Is thrown if the passed parameter is not of type array
49
     */
50 5 View Code Duplication
    public function __construct($items = array())
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...
51
    {
52
        // parent constructor to ensure property preset
53 5
        parent::__construct();
54
55
        // check if NULL is passed, is yes, to nothing
56 5
        if (is_null($items)) {
57
            return;
58
        }
59
        // check if an array is passed
60 5
        if (is_array($items)) {
61
            // initialize the HashMap with the values of the passed array
62 5
            foreach ($items as $key => $item) {
63
                $this->add($key, $item);
64 5
            }
65 5
            return;
66
        }
67
        // if not a array is passed throw an exception
68
        throw new ClassCastException('Passed object is not an array');
69
    }
70
71
    /**
72
     * This method adds the passed object with the passed key
73
     * to the HashMap.
74
     *
75
     * @param mixed $key    The key to add the passed value under
76
     * @param mixed $object The object to add to the HashMap
77
     *
78
     * @return \AppserverIo\Collections\HashMap The instance
79
     * @throws \AppserverIo\Collections\InvalidKeyException Is thrown if the passed key is NOT an primitive datatype
80
     * @throws \AppserverIo\Lang\NullPointerException Is thrown if the passed key is null or not a flat datatype like Integer, Strng, Double or Boolean
81
     */
82 3
    public function add($key, $object)
83
    {
84 3
        if (is_null($key)) {
85
            throw new NullPointerException('Passed key is null');
86
        }
87
        // check if a primitive datatype is passed
88 3 View Code Duplication
        if (is_integer($key) || is_string($key) || is_double($key) || is_bool($key)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
89
            // add the item to the array
90 3
            $this->items[$key] = $object;
91
            // and return
92 3
            return;
93
        }
94
        // check if an object is passed
95 View Code Duplication
        if (is_object($key)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
96
            if ($key instanceof Strng) {
97
                $newKey = $key->stringValue();
98
            } elseif ($key instanceof Flt) {
99
                $newKey = $key->floatValue();
100
            } elseif ($key instanceof Integer) {
101
                $newKey = $key->intValue();
102
            } elseif ($key instanceof Boolean) {
103
                $newKey = $key->booleanValue();
104
            } elseif (method_exists($key, '__toString')) {
105
                $newKey = $key->__toString();
106
            } else {
107
                throw new InvalidKeyException('Passed key has to be a primitive datatype or  has to implement the __toString() method');
108
            }
109
            // add the item to the array
110
            $this->items[$newKey] = $object;
111
            // and return
112
            return;
113
        }
114
        throw new InvalidKeyException('Passed key has to be a primitive datatype or  has to implement the __toString() method');
115
    }
116
117
    /**
118
     * This method Returns a new HashMap initialized with the
119
     * passed array.
120
     *
121
     * @param array $array Holds the array to initialize the new HashMap
122
     *
123
     * @return \AppserverIo\Collections\HashMap Returns a HashMap initialized with the passed array
124
     * @throws \AppserverIo\Lang\ClassCastException Is thrown if the passed object is not an array
125
     */
126
    public static function fromArray($array)
127
    {
128
        // check if the passed object is an array and set it
129
        if (is_array($array)) {
130
            return new HashMap($array);
131
        }
132
        // throw an exception if the passed object is not an array
133
        throw new ClassCastException('Passed object is not an array');
134
    }
135
}
136