ArrayHashMap::containsKey()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
/**
3
 * This file is part of the Cubiche package.
4
 *
5
 * Copyright (c) Cubiche
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace Cubiche\Core\Collections\ArrayCollection;
11
12
use Cubiche\Core\Collections\CollectionInterface;
13
use Cubiche\Core\Collections\DataSource\ArrayDataSource;
14
use Cubiche\Core\Collections\DataSourceHashMap;
15
use Cubiche\Core\Comparable\Comparator;
16
use Cubiche\Core\Comparable\ComparatorInterface;
17
use Cubiche\Core\Specification\SpecificationInterface;
18
19
/**
20
 * ArrayHashMap Class.
21
 *
22
 * @author Karel Osorio Ramírez <[email protected]>
23
 * @author Ivannis Suárez Jerez <[email protected]>
24
 */
25
class ArrayHashMap extends ArrayCollection implements ArrayHashMapInterface
26
{
27
    /**
28
     * ArrayHashMap constructor.
29
     *
30
     * @param array $elements
31
     */
32
    public function __construct(array $elements = array())
33
    {
34
        foreach ($elements as $key => $element) {
35
            $this->set($key, $element);
36
        }
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function get($key)
43
    {
44
        if ($this->containsKey($key)) {
45
            return $this->elements[$key];
46
        }
47
48
        return;
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function set($key, $value)
55
    {
56
        $this->validateKey($key);
57
58
        $this->elements[$key] = $value;
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64
    public function containsKey($key)
65
    {
66
        return $this->hasKey($key);
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72
    public function containsValue($value)
73
    {
74
        return $this->values()->contains($value);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Cubiche\Core\Collections\CollectionInterface as the method contains() does only exist in the following implementations of said interface: Cubiche\Core\Collections\ArrayCollection\ArrayList, Cubiche\Core\Collections\ArrayCollection\ArraySet, Cubiche\Core\Collections...lection\SortedArrayList, Cubiche\Core\Collections...llection\SortedArraySet.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80
    public function removeAt($key)
81
    {
82
        if ($this->containsKey($key)) {
83
            $removed = $this->elements[$key];
84
            unset($this->elements[$key]);
85
86
            return $removed;
87
        }
88
89
        return;
90
    }
91
92
    /**
93
     * {@inheritdoc}
94
     */
95
    public function find(SpecificationInterface $criteria)
96
    {
97
        return new DataSourceHashMap(new ArrayDataSource($this->elements, $criteria));
98
    }
99
100
    /**
101
     * {@inheritdoc}
102
     */
103
    public function keys()
104
    {
105
        return new ArraySet(\array_keys($this->elements));
106
    }
107
108
    /**
109
     * {@inheritdoc}
110
     */
111
    public function values()
112
    {
113
        return new ArrayList(\array_values($this->elements));
114
    }
115
116
    /**
117
     * {@inheritdoc}
118
     */
119 View Code Duplication
    public function sort(ComparatorInterface $criteria = null)
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...
120
    {
121
        if ($criteria === null) {
122
            $criteria = new Comparator();
123
        }
124
125
        uksort($this->elements, function ($a, $b) use ($criteria) {
126
            return $criteria->compare($a, $b);
127
        });
128
    }
129
130
    /**
131
     * @param ComparatorInterface $criteria
132
     *
133
     * @return CollectionInterface
134
     */
135
    public function sorted(ComparatorInterface $criteria)
136
    {
137
        return new DataSourceHashMap(new ArrayDataSource($this->elements, null, $criteria));
138
    }
139
140
    /**
141
     * {@inheritdoc}
142
     */
143
    public function offsetUnset($offset)
144
    {
145
        return $this->removeAt($offset);
146
    }
147
}
148