PackageCollection   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Importance

Changes 2
Bugs 2 Features 1
Metric Value
wmc 14
eloc 26
c 2
b 2
f 1
dl 0
loc 77
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getIndexedByNamespace() 0 12 4
A getPackages() 0 6 2
A offsetSet() 0 7 2
A getIndexedByName() 0 13 4
A getNameSpaces() 0 6 2
1
<?php
2
3
/**
4
 * Author: mickael Louzet @micklouzet
5
 * File: PackageCollection.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 ArrayObject;
15
16
/**
17
 * Class PackageCollection
18
 *
19
 * @package ComposerLockParser\Package
20
 */
21
class PackageCollection extends ArrayObject
22
{
23
    use SearcherTrait;
24
25
    /** @var array|null */
26
    public $indexedBy;
27
28
    /**
29
     * @return array|null
30
     */
31
    public function getPackages(): ?array
32
    {
33
        if (null === $this->indexedBy) {
34
            return [];
35
        }
36
        return $this->getIndexedByName();
37
    }
38
39
    /**
40
     * @return array|null
41
     */
42
    public function getNameSpaces(): ?array
43
    {
44
        if (null === $this->indexedBy) {
45
            return [];
46
        }
47
        return $this->getIndexedByNamespace();
48
    }
49
50
    /**
51
     * @param mixed $index
52
     * @param Package $package
53
     *
54
     * @return void
55
     */
56
    public function offsetSet($index, $package): void
57
    {
58
        if ($package instanceof Package) {
0 ignored issues
show
introduced by
$package is always a sub-type of ComposerLockParser\Package\Package.
Loading history...
59
            $this->indexedBy['name'][$package->getName()] = $package;
60
            $this->indexedBy['namespace'][$package->getNamespace()] = $package;
61
        }
62
        parent::offsetSet($index, $package);
63
    }
64
    
65
    /**
66
     * @return array|null
67
     */
68
    private function getIndexedByName(): ?array
69
    {
70
        if (!empty($this->indexedBy['name'])) {
71
            return $this->indexedBy['name'];
72
        }
73
        /** @var Package $package */
74
        foreach ($this->getArrayCopy() as $package) {
75
            if (!$package instanceof Package) {
76
                continue;
77
            }
78
            $this->indexedBy['name'][$package->getName()] = $package;
79
        }
80
        return $this->indexedBy['name'];
81
    }
82
83
    /**
84
     * @return array|null
85
     */
86
    private function getIndexedByNamespace(): ?array
87
    {
88
        if (!empty($this->indexedBy['namespace'])) {
89
            return $this->indexedBy['namespace'];
90
        }
91
        /** @var Package $package */
92
        foreach ($this->getArrayCopy() as $package) {
93
            if (!($package instanceof Package)) {
94
                continue;
95
            }
96
        }
97
        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 92. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
98
    }
99
}
100