Passed
Push — master ( 3f95f4...2f7c15 )
by Mickael
06:15 queued 11s
created

PackageCollection::getPackages()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
/**
4
 * Author: mickael Louzet @micklouzet
5
 * File: Package.php
6
 * Created: 07/12/2019
7
 */
8
9
declare(strict_types=1);
10
11
namespace ComposerLockParser\Package;
12
13
use ComposerLockParser\SearcherTrait;
14
use ComposerLockParser\Package\Package;
15
use ArrayObject;
16
17
/**
18
 * Class PackageCollection
19
 *
20
 * @package ComposerLockParser\Package
21
 */
22
class PackageCollection extends ArrayObject
23
{
24
    use SearcherTrait;
25
26
    /** @var array */
27
    private $indexedBy;
28
29
    /**
30
     * @return array
31
     */
32
    public function getPackages(): array
33
    {
34
        return $this->getIndexedByName();
35
    }
36
37
    /**
38
     * @return array
39
     */
40
    public function getNameSpaces(): array
41
    {
42
        return array_keys($this->getIndexedByNamespace());
43
    }
44
45
    /**
46
     * @param mixed $index
47
     * @param Package $package
48
     *
49
     * @return void
50
     */
51
    public function offsetSet($index, $package): void
52
    {
53
        if ($package instanceof Package) {
0 ignored issues
show
introduced by
$package is always a sub-type of ComposerLockParser\Package\Package.
Loading history...
54
            $this->indexedBy['name'][$package->getName()] = $package;
55
            $this->indexedBy['namespace'][$package->getNamespace()] = $package;
56
        }
57
        parent::offsetSet($index, $package);
58
    }
59
    
60
    /**
61
     * @return array
62
     */
63
    private function getIndexedByName(): array
64
    {
65
        if (!empty($this->indexedBy['name'])) {
66
            return $this->indexedBy['name'];
67
        }
68
        /** @var Package $package */
69
        foreach($this->getArrayCopy() as $package) {
70
            if (!($package instanceof Package)) {
71
                continue;
72
            }
73
            $this->indexedBy['name'][$package->getName()] = $package;
74
        }
75
        return $this->indexedBy['name'];
76
    }
77
78
    /**
79
     * @return array
80
     */
81
    private function getIndexedByNamespace(): array
82
    {
83
        if (!empty($this->indexedBy['namespace'])) {
84
            return $this->indexedBy['namespace'];
85
        }
86
        /** @var Package $package */
87
        foreach($this->getArrayCopy() as $package) {
88
            if (!($package instanceof Package)) {
89
                continue;
90
            }
91
        }
92
        return $this->indexedBy['namespace'][$package->getNamespace()];
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $package seems to be defined by a foreach iteration on line 87. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
93
    }
94
}
95