Completed
Push — master ( 01dc8d...fb21bc )
by Emily
02:05
created

HashMap::offsetExists()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php namespace Spaark\CompositeUtils\Model\Collection;
2
/**
3
 * Spaark Framework
4
 *
5
 * @author Emily Shepherd <[email protected]>
6
 * @copyright 2012-2015 Emily Shepherd
7
 */
8
9
10
/**
11
 * Represents a HashMap which contains mapings from one element to
12
 * another
13
 *
14
 * This is similar to PHP's existing array system, which supports
15
 * key-value pairs (<code>array('key' =&gt; 'value'));</code>) with the
16
 * added benefit that key values can be objects.
17
 *
18
 */
19
class HashMap extends Collection
20
{
21
    protected $keys = array( );
22
23 9 View Code Duplication
    private function getScalar($value)
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...
24
    {
25
        return
26 9
              (is_object($value) ? spl_object_hash($value)
27 9
            : (is_array($value)  ? implode($value)
28 9
            : (                    (string)$value)));
29
    }
30
31
    public function key()
32
    {
33
        return $this->keys[parent::key()];
34
    }
35
36 7
    public function offsetExists($key)
37
    {
38 7
        return parent::offsetExists($this->getScalar($key));
39
    }
40
41 7
    public function offsetGet($offset)
42
    {
43 7
        return parent::offsetGet($this->getScalar($offset));
44
    }
45
46 9
    public function offsetSet($offset, $value)
47
    {
48 9
        $hash = $this->getScalar($offset);
49
50 9
        $this->keys[$hash] = $offset;
51 9
        $this->data[$hash] = $value;
52 9
    }
53
54
    public function offsetUnset($offset)
55
    {
56
        $hash = $this->getScalar($offset);
0 ignored issues
show
Unused Code introduced by
$hash is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
57
58
        unset($this->data[$offset]);
59
        unset($this->keys[$offset]);
60
    }
61
62 7
    public function contains($offset)
63
    {
64 7
        return $this->offsetExists($offset);
65
    }
66
}
67