Completed
Push — master ( 79f24a...4b5497 )
by Emily
02:16
created

AbstractMap::add()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
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\Map;
16
17
use Spaark\CompositeUtils\Model\Collection\AbstractCollection;
18
19
/**
20
 * Represents an abstract collection which maps one value to another
21
 *
22
 * These are stored as pairs
23
 *
24
 * @generic KeyType
25
 * @generic ValueType
26
 */
27
abstract class AbstractMap
28
    extends AbstractCollection
0 ignored issues
show
Coding Style introduced by
The extends keyword must be on the same line as the class name
Loading history...
Coding Style introduced by
Expected 0 spaces between "AbstractCollection" and comma; 1 found
Loading history...
29
    implements Map
0 ignored issues
show
Coding Style introduced by
The implements keyword must be on the same line as the class name
Loading history...
30
{
31
    /**
32
     * Adds an element to the Map
33
     *
34
     * @param KeyType $key The key to add
35
     * @param ValueType $value The value to add
36
     */
37 40
    public function offsetSet($key, $value)
38
    {
39 40
        $this->add($key, $value);
40 40
    }
41
42
    /**
43
     * Adds an element to the Map
44
     *
45
     * @param KeyType $key The key to add
46
     * @param ValueType $value The value to add
47
     */
48 43
    public function add($key, $value)
49
    {
50 43
        $this->insert(new Pair($key, $value));
51 43
    }
52
53
    /**
54
     * Checks if a key exists
55
     *
56
     * @param KeyType $key The key to search for
57
     * @return boolean
58
     */
59
    public function offsetExists($key) : bool
60
    {
61
        return $this->contains($key);
62
    }
63
64
    /**
65
     * Removes an item from the map
66
     *
67
     * @param KeyType $key The key of the keypair to remove
68
     */
69
    public function offsetUnset($key)
70
    {
71
        $this->remove($key);
72
    }
73
74
    /**
75
     * Gets an item from the map, looking it up by the specified key
76
     *
77
     * @param KeyType $key
78
     * @return ValueType
79
     */
80 49
    public function offsetGet($key)
81
    {
82 49
        return $this->get($key);
83
    }
84
85
    /**
86
     * {@inheritDoc}
87
     */
88 49
    public function get($key)
89
    {
90 49
        return $this->getPair($key)->value;
0 ignored issues
show
Documentation introduced by
The property $value is declared protected in Spaark\CompositeUtils\Model\Collection\Map\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...
91
    }
92
}
93