HashMap::add()   C
last analyzed

Complexity

Conditions 12
Paths 9

Size

Total Lines 34
Code Lines 22

Duplication

Lines 25
Ratio 73.53 %

Code Coverage

Tests 5
CRAP Score 78.4347

Importance

Changes 0
Metric Value
dl 25
loc 34
ccs 5
cts 22
cp 0.2273
rs 5.1612
c 0
b 0
f 0
cc 12
eloc 22
nc 9
nop 2
crap 78.4347

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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