Completed
Push — master ( b8da3f...f66325 )
by Emily
10s
created

OrderedMap::remove()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
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 an abstract collection which maps one value to another
19
 *
20
 * These are stored as pairs
21
 *
22
 * @generic KeyType
23
 * @generic ValueType
24
 */
25
class OrderedMap extends AbstractMap
26
{
27
    private $map;
28
29
    private $list;
30
31 29
    public function __construct(AbstractMap $map, AbstractList $list)
32
    {
33 29
        $this->map = $map;
34 29
        $this->list = $list;
35 29
    }
36
37
    /**
38
     * Adds an element to the Map
39
     *
40
     * @param KeyType $key The key to add
0 ignored issues
show
Bug introduced by
There is no parameter named $key. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
41
     * @param ValueType $value The value to add
0 ignored issues
show
Bug introduced by
There is no parameter named $value. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
42
     */
43 25
    public function insert(Pair $pair)
44
    {
45 25
        if (!$pair instanceof OrderedPair)
46
        {
47 25
            $pair = new OrderedPair
48
            (
49 25
                $this->size(),
50 25
                $pair->key,
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...
51 25
                $pair->value
0 ignored issues
show
Documentation introduced by
The property $value 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...
52
            );
53
        }
54
55 25
        $this->map->insert($pair);
56 25
        $this->list->add($pair);
0 ignored issues
show
Documentation introduced by
$pair is of type object<Spaark\CompositeU...Collection\OrderedPair>, but the function expects a object<Spaark\CompositeU...l\Collection\ValueType>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
57 25
    }
58
59
    /**
60
     * Checks if a key exists
61
     *
62
     * @param KeyType $key The key to search for
63
     * @return boolean
64
     */
65 24
    public function containsKey($key) : bool
66
    {
67 24
        return $this->map->containsKey($key);
68
    }
69
70
    /**
71
     * Removes an item from the map
72
     *
73
     * @param KeyType $key The key of the keypair to remove
74
     */
75 1
    public function remove($key)
76
    {
77 1
        $pair = $this->getPair($key);
78
79 1
        $this->map->remove($pair->key);
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...
80 1
        $this->list->remove($pair->index);
0 ignored issues
show
Documentation introduced by
The property $index is declared protected in Spaark\CompositeUtils\Model\Collection\OrderedPair. 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...
81 1
    }
82
            
83
    /**
84
     * Gets an item from the map, looking it up by the specified key
85
     *
86
     * @param KeyType $key
87
     * @return OrderedPair
88
     */
89 5
    public function getPair($key) : Pair 
90
    {
91 5
        return is_int($key)
92
            ? $this->list->get($key)
93 5
            : $this->map->getPair($key);
94
    }
95
96 7
    public function getIterator()
97
    {
98 7
        return $this->map->getIterator();
99
    }
100
101 27
    public function size() : int
102
    {
103 27
        return $this->list->size();
104
    }
105
106 1
    public function indexOfKey($key)
107
    {
108 1
        return $this->getPair($key)->index;
0 ignored issues
show
Documentation introduced by
The property $index is declared protected in Spaark\CompositeUtils\Model\Collection\OrderedPair. 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...
109
    }
110
}
111