Completed
Pull Request — master (#15)
by Gabriel
03:16
created

Collection::find()   B

Complexity

Conditions 6
Paths 8

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 18
ccs 12
cts 12
cp 1
rs 8.8571
cc 6
eloc 9
nc 8
nop 2
crap 6
1
<?php
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the MIT license. For more information, see
17
 * <http://www.doctrine-project.org>.
18
 */
19
20
namespace Baleen\Migrations\Version\Collection;
21
22
use Baleen\Migrations\Exception\InvalidArgumentException;
23
use Baleen\Migrations\Exception\Version\Collection\CollectionException;
24
use Baleen\Migrations\Shared\Collection\AbstractCollection;
25
use Baleen\Migrations\Version\Collection\Resolver\DefaultResolverStackFactory;
26
use Baleen\Migrations\Version\Collection\Resolver\ResolverInterface;
27
use Baleen\Migrations\Version\Comparator\ComparatorInterface;
28
use Baleen\Migrations\Version\Comparator\MigrationComparator;
29
use Baleen\Migrations\Version\VersionInterface;
30
31
/**
32
 * Class CollectionAbstract.
33
 *
34
 * @author Gabriel Somoza <[email protected]>
35
 *
36
 * IMPROVE: this class has many methods. Consider refactoring it to keep number of methods under 10.
37
 *
38
 * @SuppressWarnings(PHPMD.TooManyMethods)
39
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
40
 */
41
class Collection extends AbstractCollection implements ResolvableCollectionInterface
42
{
43
    /** @var ResolverInterface */
44
    private $resolver;
45
46
    /** @var ComparatorInterface */
47
    private $comparator;
48
49
    /**
50
     * @param VersionInterface[]|\Traversable $versions
51
     * @param ResolverInterface $resolver
0 ignored issues
show
Documentation introduced by
Should the type for parameter $resolver not be null|ResolverInterface?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
52
     * @param ComparatorInterface $comparator
0 ignored issues
show
Documentation introduced by
Should the type for parameter $comparator not be null|ComparatorInterface?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
53
     *
54
     * @throws InvalidArgumentException
55
     */
56 157
    public function __construct(
57
        $versions = [],
58
        ResolverInterface $resolver = null,
59 1
        ComparatorInterface $comparator = null
60
    ) {
61 157
        if (null === $resolver) {
62 122
            $resolver = DefaultResolverStackFactory::create();
63 122
        }
64 157
        $this->resolver = $resolver;
65
66 157
        if (null === $comparator) {
67 147
            $comparator = new MigrationComparator();
68 147
        }
69 157
        $this->comparator = $comparator;
70
71 157
        parent::__construct($versions);
0 ignored issues
show
Bug introduced by
It seems like $versions defined by parameter $versions on line 57 can also be of type object<Traversable>; however, Baleen\Migrations\Shared...llection::__construct() does only seem to accept array<integer,object<Bal...sion\VersionInterface>>, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
72 155
    }
73
74
    /**
75
     * @return ResolverInterface
76
     */
77 149
    final protected function getResolver()
78
    {
79 149
        return $this->resolver;
80
    }
81
82
    /**
83
     * Gets an element.
84
     *
85
     * @param mixed $element If an alias is given then it will be resolved to an element. Otherwise the $key will be used
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 121 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
86
     *                   to fetch the element by index.
87
     * @param bool $resolve Whether to use the resolver or not.
88
     *
89
     * @return VersionInterface|null Null if not present
90
     */
91 32
    public function find($element, $resolve = true)
92
    {
93 32
        $result = null;
94
95 32
        if (is_object($element)) {
96 1
            $element = (string) $element;
97 1
        }
98
99 32
        if ($resolve && is_string($element)) {
100 32
            $result = $this->getResolver()->resolve($element, $this);
101 32
        }
102
103 32
        if (null === $result && is_scalar($element)) {
104 1
            $result = $this->get($element);
105 1
        }
106
107 32
        return $result;
108
    }
109
110
    /**
111
     * Returns whether the key exists in the collection.
112
     *
113
     * @param      $index
114
     * @param bool $resolve
115
     *
116
     * @return bool
117
     */
118 1
    public function has($index, $resolve = true)
119
    {
120 1
        return $this->find($index, $resolve) !== null;
121
    }
122
123
    /**
124
     * @inheritdoc
125
     */
126 144
    public function validate(VersionInterface $version)
127
    {
128 144
        return !empty($version->getId()); // basically: any VersionInterface is valid for this collection
129 2
    }
130
131
    /**
132
     * invalidateResolverCache
133
     */
134 147
    final protected function invalidateResolverCache()
135
    {
136 147
        $this->getResolver()->clearCache($this);
137 147
    }
138
139
    /**
140
     * Add a version to the collection
141
     *
142
     * @param VersionInterface $version
143
     *
144
     * @return bool
145
     *
146
     * @throws CollectionException
147
     * @throws InvalidArgumentException
148
     */
149 144
    public function add(VersionInterface $version)
150
    {
151 144
        $this->validate($version);
152 143
        $result = parent::add($version);
153 143
        if ($result) {
154 143
            $this->invalidateResolverCache();
155 143
        }
156
157 143
        return $result;
158
    }
159
160
    /**
161
     * @param $key
162
     *
163
     * @return VersionInterface|null
164
     */
165 4
    public function remove($key)
166
    {
167 4
        $result = parent::remove($key);
168 4
        if ($result) {
169 3
            $this->invalidateResolverCache();
170 3
        }
171
172 4
        return $result;
173
    }
174
175
    /**
176
     * {@inheritDoc}
177
     */
178 10
    public function replace(VersionInterface $version)
179
    {
180 10
        $this->validate($version);
181 10
        $removedElement = parent::replace($version);
182 10
        $this->invalidateResolverCache();
183
184 10
        return $removedElement;
185
    }
186
187
    /**
188
     * {@inheritDoc}
189
     */
190 8
    public function clear()
191
    {
192 8
        parent::clear();
193 8
        $this->invalidateResolverCache();
194 8
    }
195
196
    /**
197
     * Sort the collection
198
     * @param ComparatorInterface $comparator
0 ignored issues
show
Documentation introduced by
Should the type for parameter $comparator not be null|ComparatorInterface?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
199
     * @return static
200
     */
201 19
    public function sort(ComparatorInterface $comparator = null)
202
    {
203 19
        if (null === $comparator) {
204 9
            $comparator = $this->comparator;
205 9
        }
206 19
        $elements = $this->toArray();
207 19
        uasort($elements, $comparator);
208 19
        return new static($elements, $this->getResolver(), $comparator);
209
    }
210
211
    /**
212
     * Returns a collection with elements sorted in reverse order.
213
     *
214
     * @return static
215
     */
216 8
    public function getReverse()
217
    {
218 8
        return $this->sort($this->comparator->getReverse());
219
    }
220
221
    /**
222
     * @return ComparatorInterface
223
     */
224 19
    public function getComparator()
225
    {
226 19
        return $this->comparator;
227
    }
228
}
229