Failed Conditions
Pull Request — master (#56)
by Bernhard
11:07
created

BindingDescriptorCollection::get()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 4.3731

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 16
ccs 5
cts 7
cp 0.7143
rs 9.2
cc 4
eloc 9
nc 3
nop 2
crap 4.3731
1
<?php
2
3
/*
4
 * This file is part of the puli/manager package.
5
 *
6
 * (c) Bernhard Schussek <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Puli\Manager\Discovery\Binding;
13
14
use OutOfBoundsException;
15
use Puli\Discovery\Api\Binding\Binding;
16
use Puli\Manager\Api\Discovery\BindingDescriptor;
17
use Puli\Manager\Util\TwoDimensionalHashMap;
18
use Rhumsaa\Uuid\Uuid;
19
20
/**
21
 * A collection of binding descriptors.
22
 *
23
 * @since  1.0
24
 *
25
 * @author Bernhard Schussek <[email protected]>
26
 */
27
class BindingDescriptorCollection
28
{
29
    /**
30
     * @var BindingDescriptor[][]
31
     */
32
    private $map = array();
33
34
    /**
35
     * Adds a binding descriptor.
36
     *
37
     * @param BindingDescriptor $bindingDescriptor The binding descriptor.
38
     */
39 24
    public function add(BindingDescriptor $bindingDescriptor)
40
    {
41 24
        if ($this->contains($bindingDescriptor->getBinding(), $bindingDescriptor->getContainingModule()->getName())) {
42 1
            return;
43
        }
44
45 24
        $this->map[$bindingDescriptor->getContainingModule()->getName()][] = $bindingDescriptor;
46 24
    }
47
48
    /**
49
     * Removes a binding descriptor.
50
     *
51
     * This method ignores non-existing binding descriptors.
52
     *
53
     * @param Binding $binding    The described binding.
54
     * @param string  $moduleName The name of the module containing the type.
55
     */
56
    public function remove(Binding $binding, $moduleName)
57
    {
58
        if (!isset($this->map[$moduleName])) {
59
            return;
60
        }
61
62
        foreach ($this->map[$moduleName] as $key => $bindingDescriptor) {
63
            if ($bindingDescriptor->getBinding()->equals($binding)) {
64
                unset($this->map[$moduleName][$key]);
65
66
                break;
67
            }
68
        }
69
    }
70
71
    /**
72
     * Returns a binding descriptor.
73
     *
74
     * @param Uuid $uuid The UUID of the binding descriptor.
0 ignored issues
show
Bug introduced by
There is no parameter named $uuid. 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...
75
     *
76
     * @return BindingDescriptor The binding descriptor.
77
     *
78
     * @throws OutOfBoundsException If no binding descriptor was set for the
79
     *                              given UUID.
80
     */
81 1
    public function get(Binding $binding, $moduleName)
82
    {
83 1
        if (isset($this->map[$moduleName])) {
84 1
            foreach ($this->map[$moduleName] as $bindingDescriptor) {
85 1
                if ($bindingDescriptor->getBinding()->equals($binding)) {
86 1
                    return $bindingDescriptor;
87
                }
88
            }
89
        }
90
91
        throw new OutOfBoundsException(sprintf(
92
            'The binding %s in module "%s" does not exist.',
93
            get_class($binding),
94
            $moduleName
95
        ));
96
    }
97
98
    /**
99
     * Returns whether a binding descriptor exists.
100
     *
101
     * @param Uuid $uuid The UUID of the binding descriptor.
0 ignored issues
show
Bug introduced by
There is no parameter named $uuid. 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...
102
     *
103
     * @return bool Returns `true` if a binding descriptor was set for the given
104
     *              UUID.
105
     */
106 24
    public function contains(Binding $binding, $moduleName = null)
107
    {
108 24
        if (null === $moduleName) {
109 8 View Code Duplication
            foreach ($this->map as $bindingDescriptors) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
110 5
                foreach ($bindingDescriptors as $key => $bindingDescriptor) {
111 5
                    if ($bindingDescriptor->getBinding()->equals($binding)) {
112 5
                        return true;
113
                    }
114
                }
115
            }
116
117 5
            return false;
118
        }
119
120 24
        if (!isset($this->map[$moduleName])) {
121 24
            return false;
122
        }
123
124 9
        foreach ($this->map[$moduleName] as $key => $bindingDescriptor) {
125 9
            if ($bindingDescriptor->getBinding()->equals($binding)) {
126 9
                return true;
127
            }
128
        }
129
130 7
        return false;
131
    }
132
133 6
    public function listByBinding(Binding $binding)
134
    {
135 6
        $bindingDescriptors = array();
136
137 6 View Code Duplication
        foreach ($this->map as $bindingDescriptors) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
138 5
            foreach ($bindingDescriptors as $bindingDescriptor) {
139 5
                if ($bindingDescriptor->getBinding()->equals($binding)) {
140 5
                    $bindingDescriptors[] = $bindingDescriptor;
141
                }
142
            }
143
        }
144
145 6
        return $bindingDescriptors;
146
    }
147
148
    /**
149
     * Returns the contents of the collection as array.
150
     *
151
     * @return BindingDescriptor[] An array containing all bindings indexed by UUID.
0 ignored issues
show
Documentation introduced by
Should the return type not be BindingDescriptor[][]?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
152
     */
153 52
    public function toArray()
154
    {
155 52
        return $this->map;
156
    }
157
158
    /**
159
     * Returns whether the collection is empty.
160
     *
161
     * @return bool Returns `true` if the collection is empty and `false`
162
     *              otherwise.
163
     */
164 2
    public function isEmpty()
165
    {
166 2
        return 0 === count($this->map);
167
    }
168
}
169