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

OrderedMap::getIterator()   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\Map;
16
17
use Spaark\CompositeUtils\Model\Collection\ListCollection\ArrayList;
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
class OrderedMap extends AbstractMap
28
{
29
    private $map;
30
31
    private $list;
32
33 37
    public function __construct(Map $map, ArrayList $list)
34
    {
35 37
        $this->map = $map;
36 37
        $this->list = $list;
37 37
    }
38
39
    /**
40
     * Adds an element to the Map
41
     *
42
     * @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...
43
     * @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...
44
     */
45 33
    public function insert(Pair $pair)
46
    {
47 33
        if (!$pair instanceof OrderedPair)
48
        {
49 33
            $pair = new OrderedPair
50
            (
51 33
                $this->size(),
52 33
                $pair->key,
0 ignored issues
show
Documentation introduced by
The property $key 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...
53 33
                $pair->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...
54
            );
55
        }
56
57 33
        $this->map->insert($pair);
58 33
        $this->list->add($pair);
0 ignored issues
show
Documentation introduced by
$pair is of type object<Spaark\CompositeU...ection\Map\OrderedPair>, but the function expects a object<Spaark\CompositeU...stCollection\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...
59 33
    }
60
61
    /**
62
     * Checks if a key exists
63
     *
64
     * @param KeyType $key The key to search for
65
     * @return boolean
66
     */
67 32
    public function containsKey($key) : bool
68
    {
69 32
        return $this->map->containsKey($key);
70
    }
71
72
    /**
73
     * Removes an item from the map
74
     *
75
     * @param KeyType $key The key of the keypair to remove
76
     */
77 1
    public function remove($key)
78
    {
79 1
        $pair = $this->getPair($key);
80
81 1
        $this->map->remove($pair->key);
0 ignored issues
show
Documentation introduced by
The property $key 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...
82 1
        $this->list->remove($pair->index);
0 ignored issues
show
Documentation introduced by
The property $index is declared protected in Spaark\CompositeUtils\Mo...lection\Map\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...
83 1
    }
84
            
85
    /**
86
     * Gets an item from the map, looking it up by the specified key
87
     *
88
     * @param KeyType $key
89
     * @return OrderedPair
90
     */
91 11
    public function getPair($key) : Pair 
92
    {
93 11
        return is_int($key)
94
            ? $this->list->get($key)
95 11
            : $this->map->getPair($key);
96
    }
97
98 12
    public function getIterator()
99
    {
100 12
        return $this->map->getIterator();
101
    }
102
103 35
    public function size() : int
104
    {
105 35
        return $this->list->size();
106
    }
107
108 7
    public function indexOfKey($key)
109
    {
110 7
        return $this->getPair($key)->index;
0 ignored issues
show
Documentation introduced by
The property $index is declared protected in Spaark\CompositeUtils\Mo...lection\Map\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...
111
    }
112
}
113