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\Package\Package; |
14
|
|
|
use ArrayObject; |
15
|
|
|
|
16
|
|
|
class PackageCollection extends ArrayObject |
17
|
|
|
{ |
18
|
|
|
|
19
|
|
|
/** @var array */ |
20
|
|
|
private $indexedBy; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param string $name |
24
|
|
|
* |
25
|
|
|
* @return Package |
26
|
|
|
*/ |
27
|
|
|
public function getByName(string $name): Package |
28
|
|
|
{ |
29
|
|
|
if (!$this->hasByName($name)) { |
30
|
|
|
throw new \UnexpectedValueException(sprintf('Sorry, package %s not found !', $name)); |
31
|
|
|
} |
32
|
|
|
return $this->getIndexedByName()[$name]; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param string $namespace |
37
|
|
|
* |
38
|
|
|
* @return Package |
39
|
|
|
*/ |
40
|
|
|
public function getByNamespace(string $namespace): Package |
41
|
|
|
{ |
42
|
|
|
if (!$this->hasByNamespace($namespace)) { |
43
|
|
|
throw new \UnexpectedValueException(sprintf('Sorry, namespace %s not found !', $namespace)); |
44
|
|
|
} |
45
|
|
|
return $this->getIndexedByNamespace()[$namespace]; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param string $name |
50
|
|
|
* |
51
|
|
|
* @return bool |
52
|
|
|
*/ |
53
|
|
|
public function hasByName(string $name): bool |
54
|
|
|
{ |
55
|
|
|
return array_key_exists($name, $this->getIndexedByName()); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param string $namespace |
60
|
|
|
* |
61
|
|
|
* @return bool |
62
|
|
|
*/ |
63
|
|
|
public function hasByNamespace($namespace): bool |
64
|
|
|
{ |
65
|
|
|
return array_key_exists($namespace, $this->getIndexedByNamespace()); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param mixed $index |
70
|
|
|
* @param mixed $package |
71
|
|
|
* |
72
|
|
|
* @return void |
73
|
|
|
*/ |
74
|
|
|
public function offsetSet($index, $package): void |
75
|
|
|
{ |
76
|
|
|
if ($package instanceof Package) { |
77
|
|
|
$this->indexedBy['name'][$package->getName()] = $package; |
78
|
|
|
$this->indexedBy['namespace'][$package->getNamespace()] = $package; |
79
|
|
|
} |
80
|
|
|
parent::offsetSet($index, $package); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return array |
85
|
|
|
*/ |
86
|
|
View Code Duplication |
private function getIndexedByName(): array |
|
|
|
|
87
|
|
|
{ |
88
|
|
|
if (!empty($this->indexedBy['name'])) { |
89
|
|
|
return $this->indexedBy['name']; |
90
|
|
|
} |
91
|
|
|
/** @var Package $package */ |
92
|
|
|
foreach($this->getArrayCopy() as $package) { |
93
|
|
|
if (!($package instanceof Package)) { |
94
|
|
|
continue; |
95
|
|
|
} |
96
|
|
|
$this->indexedBy['name'][$package->getName()] = $package; |
97
|
|
|
} |
98
|
|
|
return $this->indexedBy['name']; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @return array |
103
|
|
|
*/ |
104
|
|
View Code Duplication |
private function getIndexedByNamespace(): array |
|
|
|
|
105
|
|
|
{ |
106
|
|
|
if (!empty($this->indexedBy['namespace'])) { |
107
|
|
|
return $this->indexedBy['namespace']; |
108
|
|
|
} |
109
|
|
|
/** @var Package $package */ |
110
|
|
|
foreach($this->getArrayCopy() as $package) { |
111
|
|
|
if (!($package instanceof Package)) { |
112
|
|
|
continue; |
113
|
|
|
} |
114
|
|
|
$this->indexedBy['namespace'][$package->getNamespace()] = $package; |
115
|
|
|
} |
116
|
|
|
return $this->indexedBy['namespace']; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.