Completed
Push — master ( 52038d...574828 )
by Emily
02:11
created

HashMap::size()   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 0
crap 1
1
<?php
2
/**
3
 * This file is part of the Composite Utils package.
4
 *
5
 * (c) Emily Shepherd <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the
8
 * LICENSE.md file that was distributed with this source code.
9
 *
10
 * @package spaark/composite-utils
11
 * @author Emily Shepherd <[email protected]>
12
 * @license MIT
13
 */
14
15
namespace Spaark\CompositeUtils\Model\Collection;
16
17
/**
18
 * Represents a HashMap which contains mapings from one element to
19
 * another
20
 *
21
 * @generic KeyType
22
 * @generic ValueType
23
 */
24
class HashMap extends AbstractMap
25
{
26
    /**
27
     * @var Pair<KeyType, ValueType>[]
28
     */
29
    protected $data = [];
30
31
    /**
32
     * {@inheritDoc}
33
     */
34 3
    public function getIterator()
35
    {
36 3
        return new HashMapIterator($this->data);
37
    }
38
39
    /**
40
     * {@inheritDoc}
41
     */
42 31
    public function containsKey($key) : bool
43
    {
44 31
        return isset($this->data[$this->getScalar($key)]);
45
    }
46
47
    /**
48
     * {@inheritDoc}
49
     */
50 33
    public function get($key)
51
    {
52 33
        return $this->data[$this->getScalar($key)]->value;
53
    }
54
55
    /**
56
     * {@inheritDoc}
57
     */
58 26
    public function insert(Pair $pair)
59
    {
60 26
        $this->data[$this->getScalar($pair->key)] = $pair;
0 ignored issues
show
Documentation introduced by
The property $key is declared protected in Spaark\CompositeUtils\Model\Collection\Pair. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
61 26
    }
62
63
    /**
64
     * {@inheritDoc}
65
     */
66 1
    public function remove($key)
67
    {
68 1
        unset($this->data[$this->getScalar($key)]);
69 1
    }
70
71
    /**
72
     * {@inheritDoc}
73
     */
74 7
    public function size() : int
75
    {
76 7
        return count($this->data);
77
    }
78
79
    /**
80
     * Returns a good scalar value to use for a native PHP array
81
     *
82
     * @param KeyType $value The key to convert to a scalar
83
     * @return string Scalar value
84
     */
85 39
    private function getScalar($value)
86
    {
87 39
        switch (gettype($value))
88
        {
89 39
            case 'object':
90
                return spl_object_hash($value);
91 39
            case 'array':
92
                return implode($value);
93
            default:
94 39
                return (string)$value;
95
        }
96
    }
97
}
98