Failed Conditions
Push — master ( d49c03...6343d0 )
by Bernhard
05:03
created

src/Api/Package/PackageCollection.php (3 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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\Api\Package;
13
14
use ArrayAccess;
15
use ArrayIterator;
16
use Countable;
17
use IteratorAggregate;
18
19
/**
20
 * A collection of Puli packages.
21
 *
22
 * @since  1.0
23
 *
24
 * @author Bernhard Schussek <[email protected]>
25
 */
26
class PackageCollection implements IteratorAggregate, Countable, ArrayAccess
27
{
28
    /**
29
     * @var RootPackage
30
     */
31
    private $rootPackage;
32
33
    /**
34
     * @var Package[]
35
     */
36
    private $packages = array();
37
38 372
    public function __construct(array $packages = array())
39
    {
40 372
        $this->merge($packages);
41 372
    }
42
43
    /**
44
     * Adds a package to the collection.
45
     *
46
     * @param Package $package The added package.
47
     */
48 369
    public function add(Package $package)
49
    {
50 369
        $this->packages[$package->getName()] = $package;
51
52 369
        if ($package instanceof RootPackage) {
53 291
            $this->rootPackage = $package;
54
        }
55 369
    }
56
57
    /**
58
     * Adds multiple packages to the collection.
59
     *
60
     * @param Package[] $packages The added packages.
61
     */
62 372
    public function merge(array $packages)
63
    {
64 372
        foreach ($packages as $package) {
65 112
            $this->add($package);
66
        }
67 372
    }
68
69
    /**
70
     * Replaces the collection with the given packages.
71
     *
72
     * @param Package[] $packages The packages to set.
73
     */
74 2
    public function replace(array $packages)
75
    {
76 2
        $this->clear();
77 2
        $this->merge($packages);
78 2
    }
79
80
    /**
81
     * Removes a package from the collection.
82
     *
83
     * @param string $name The package name.
84
     */
85 12
    public function remove($name)
86
    {
87 12
        if ($this->rootPackage && $name === $this->rootPackage->getName()) {
88 2
            $this->rootPackage = null;
89
        }
90
91 12
        unset($this->packages[$name]);
92 12
    }
93
94
    /**
95
     * Removes all packages from the collection.
96
     */
97 3
    public function clear()
98
    {
99 3
        if ($this->rootPackage) {
100 2
            $this->rootPackage = null;
101
        }
102
103 3
        $this->packages = array();
104 3
    }
105
106
    /**
107
     * Returns the package with the given name.
108
     *
109
     * @param string $name The package name.
110
     *
111
     * @return Package The package with the passed name.
112
     *
113
     * @throws NoSuchPackageException If the package was not found.
114
     */
115 51
    public function get($name)
116
    {
117 51
        if (!isset($this->packages[$name])) {
118 2
            throw new NoSuchPackageException(sprintf(
119 2
                'The package "%s" was not found.',
120
                $name
121
            ));
122
        }
123
124 49
        return $this->packages[$name];
125
    }
126
127
    /**
128
     * Returns whether a package with the given name exists.
129
     *
130
     * @param string $name The package name.
131
     *
132
     * @return bool Whether a package with this name exists.
133
     */
134 34
    public function contains($name)
135
    {
136 34
        return isset($this->packages[$name]);
137
    }
138
139
    /**
140
     * Returns the root package.
141
     *
142
     * If the collection contains no root package, `null` is returned.
143
     *
144
     * @return RootPackage|null The root package or `null` if none exists.
0 ignored issues
show
Consider making the return type a bit more specific; maybe use RootPackage.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
145
     */
146 270
    public function getRootPackage()
147
    {
148 270
        return $this->rootPackage;
149
    }
150
151
    /**
152
     * Returns the name of the root package.
153
     *
154
     * If the collection contains no root package, `null` is returned.
155
     *
156
     * @return string|null The root package name or `null` if none exists.
157
     */
158 2
    public function getRootPackageName()
159
    {
160 2
        return $this->rootPackage ? $this->rootPackage->getName() : null;
161
    }
162
163
    /**
164
     * Returns all installed packages.
165
     *
166
     * The installed packages are all packages that are not the root package.
167
     *
168
     * @return Package[] The installed packages indexed by their names.
169
     */
170 6
    public function getInstalledPackages()
171
    {
172 6
        $packages = $this->packages;
173
174 6
        if ($this->rootPackage) {
175 4
            unset($packages[$this->rootPackage->getName()]);
176
        }
177
178 6
        return $packages;
179
    }
180
181
    /**
182
     * Returns the names of all installed packages.
183
     *
184
     * The installed packages are all packages that are not the root package.
185
     *
186
     * @return string[] The names of the installed packages.
0 ignored issues
show
Should the return type not be integer[]?

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...
187
     */
188 1
    public function getInstalledPackageNames()
189
    {
190 1
        return array_keys($this->getInstalledPackages());
191
    }
192
193
    /**
194
     * Returns the names of all packages.
195
     *
196
     * @return string[] The package names.
0 ignored issues
show
Should the return type not be integer[]?

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...
197
     */
198 93
    public function getPackageNames()
199
    {
200 93
        return array_keys($this->packages);
201
    }
202
203
    /**
204
     * Returns the packages in the collection.
205
     *
206
     * @return Package[] The packages in the collection.
207
     */
208 6
    public function toArray()
209
    {
210 6
        return $this->packages;
211
    }
212
213
    /**
214
     * Returns whether the collection is empty.
215
     *
216
     * @return bool Returns `true` if the collection is empty and `false`
217
     *              otherwise.
218
     */
219 2
    public function isEmpty()
220
    {
221 2
        return 0 === count($this->packages);
222
    }
223
224
    /**
225
     * {@inheritdoc}
226
     */
227 268
    public function getIterator()
228
    {
229 268
        return new ArrayIterator($this->packages);
230
    }
231
232
    /**
233
     * {@inheritdoc}
234
     */
235 33
    public function count()
236
    {
237 33
        return count($this->packages);
238
    }
239
240
    /**
241
     * {@inheritdoc}
242
     */
243 1
    public function offsetExists($name)
244
    {
245 1
        return $this->contains($name);
246
    }
247
248
    /**
249
     * {@inheritdoc}
250
     */
251 6
    public function offsetGet($name)
252
    {
253 6
        return $this->get($name);
254
    }
255
256
    /**
257
     * {@inheritdoc}
258
     */
259 1
    public function offsetSet($name, $package)
260
    {
261 1
        $this->add($package);
262 1
    }
263
264
    /**
265
     * {@inheritdoc}
266
     */
267 1
    public function offsetUnset($name)
268
    {
269 1
        $this->remove($name);
270 1
    }
271
}
272