Collection::__construct()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 17
ccs 12
cts 12
cp 1
rs 9.4285
cc 3
eloc 11
nc 4
nop 3
crap 3
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\Delta\Collection;
21
22
use Baleen\Migrations\Exception\InvalidArgumentException;
23
use Baleen\Migrations\Exception\Version\Collection\CollectionException;
24
use Baleen\Migrations\Migration\Options\Direction;
25
use Baleen\Migrations\Common\Collection\AbstractCollection;
26
use Baleen\Migrations\Delta\Collection\Resolver\DefaultResolverStackFactory;
27
use Baleen\Migrations\Delta\Collection\Resolver\ResolverInterface;
28
use Baleen\Migrations\Delta\Comparator\ComparatorInterface;
29
use Baleen\Migrations\Delta\Comparator\MigrationComparator;
30
use Baleen\Migrations\Delta\DeltaInterface;
31
use Zend\Stdlib\ArrayUtils;
32
33
/**
34
 * Class CollectionAbstract.
35
 *
36
 * @author Gabriel Somoza <[email protected]>
37
 *
38
 * IMPROVE: this class has many methods. Consider refactoring it to keep number of methods under 10.
39
 *
40
 * @SuppressWarnings(PHPMD.TooManyMethods)
41
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
42
 */
43
class Collection extends AbstractCollection implements ResolvableCollectionInterface
44
{
45
    /** @var ResolverInterface */
46
    private $resolver;
47
48
    /** @var ComparatorInterface */
49
    private $comparator;
50
51
    /**
52
     * @param DeltaInterface[]|\Traversable $versions
53
     * @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...
54
     * @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...
55
     *
56
     * @throws InvalidArgumentException
57
     */
58 157
    public function __construct(
59 1
        $versions = [],
60
        ResolverInterface $resolver = null,
61
        ComparatorInterface $comparator = null
62
    ) {
63 157
        if (null === $resolver) {
64 122
            $resolver = DefaultResolverStackFactory::create();
65 122
        }
66 157
        $this->resolver = $resolver;
67
68 157
        if (null === $comparator) {
69 147
            $comparator = new MigrationComparator();
70 147
        }
71 157
        $this->comparator = $comparator;
72
73 157
        parent::__construct($versions);
0 ignored issues
show
Bug introduced by
It seems like $versions defined by parameter $versions on line 59 can also be of type object<Traversable>; however, Baleen\Migrations\Common...llection::__construct() does only seem to accept array<integer,object<Bal...\Delta\DeltaInterface>>, 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...
74 155
    }
75
76
    /**
77
     * @return ResolverInterface
78
     */
79 149
    final protected function getResolver()
80
    {
81 149
        return $this->resolver;
82
    }
83
84
    /**
85
     * Gets an element.
86
     *
87
     * @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...
88
     *                   to fetch the element by index.
89
     * @param bool $resolve Whether to use the resolver or not.
90
     *
91
     * @return DeltaInterface|null Null if not present
92
     */
93 32
    public function find($element, $resolve = true)
94
    {
95 32
        $result = null;
96
97 32
        if (is_object($element)) {
98 1
            $element = (string) $element;
99 1
        }
100
101 32
        if ($resolve && is_string($element)) {
102 32
            $result = $this->getResolver()->resolve($element, $this);
103 32
        }
104
105 32
        if (null === $result && is_scalar($element)) {
106 1
            $result = $this->get($element);
107 1
        }
108
109 32
        return $result;
110
    }
111
112
    /**
113
     * Returns whether the key exists in the collection.
114
     *
115
     * @param      $index
116
     * @param bool $resolve
117
     *
118
     * @return bool
119
     */
120 1
    public function has($index, $resolve = true)
121
    {
122 1
        return $this->find($index, $resolve) !== null;
123
    }
124
125
    /**
126
     * @inheritdoc
127
     */
128 144
    public function validate(DeltaInterface $version)
129 2
    {
130 144
        return !empty($version->getId()); // basically: any DeltaInterface is valid for this collection
131
    }
132
133
    /**
134
     * invalidateResolverCache
135
     */
136 147
    final protected function invalidateResolverCache()
137
    {
138 147
        $this->getResolver()->clearCache($this);
139 147
    }
140
141
    /**
142
     * Add a version to the collection
143
     *
144
     * @param mixed $version
145
     *
146
     * @return bool
147
     *
148
     * @throws CollectionException
149
     * @throws InvalidArgumentException
150
     */
151 144
    public function add(DeltaInterface $version)
152
    {
153 144
        $this->validate($version);
154 143
        $result = parent::add($version);
155 143
        if ($result) {
156 143
            $this->invalidateResolverCache();
157 143
        }
158
159 143
        return $result;
160
    }
161
162
    /**
163
     * @param $key
164
     *
165
     * @return DeltaInterface|null
166
     */
167 4
    public function remove($key)
168
    {
169 4
        $result = parent::remove($key);
170 4
        if ($result) {
171 3
            $this->invalidateResolverCache();
172 3
        }
173
174 4
        return $result;
175
    }
176
177
    /**
178
     * {@inheritDoc}
179
     */
180 10
    public function replace(DeltaInterface $version)
181
    {
182 10
        $this->validate($version);
183 10
        $removedElement = parent::replace($version);
184 10
        $this->invalidateResolverCache();
185
186 10
        return $removedElement;
187
    }
188
189
    /**
190
     * {@inheritDoc}
191
     */
192 8
    public function clear()
193
    {
194 8
        parent::clear();
195 8
        $this->invalidateResolverCache();
196 8
    }
197
198
    /**
199
     * Sort the collection
200
     * @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...
201
     * @return static
202
     */
203 19
    public function sort(ComparatorInterface $comparator = null)
204
    {
205 19
        if (null === $comparator) {
206 9
            $comparator = $this->comparator;
207 9
        }
208 19
        $elements = $this->toArray();
209 19
        uasort($elements, $comparator);
210 19
        return new static($elements, $this->getResolver(), $comparator);
211
    }
212
213
    /**
214
     * Returns a collection with elements sorted in reverse order.
215
     *
216
     * @return static
217
     */
218 8
    public function getReverse()
219
    {
220 8
        return $this->sort($this->comparator->getReverse());
221
    }
222
223
    /**
224
     * @return ComparatorInterface
225
     */
226 19
    public function getComparator()
227
    {
228 19
        return $this->comparator;
229
    }
230
}
231