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 PackagesCollection 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
|
|
|
public function offsetSet($index, $package) |
69
|
|
|
{ |
70
|
|
|
if ($package instanceof Package) { |
71
|
|
|
$this->indexedBy['name'][$package->getName()] = $package; |
72
|
|
|
$this->indexedBy['namespace'][$package->getNamespace()] = $package; |
73
|
|
|
} |
74
|
|
|
return parent::offsetSet($index, $package); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @return array |
79
|
|
|
*/ |
80
|
|
View Code Duplication |
private function getIndexedByName(): array |
|
|
|
|
81
|
|
|
{ |
82
|
|
|
if (!empty($this->indexedBy['name'])) { |
83
|
|
|
return $this->indexedBy['name']; |
84
|
|
|
} |
85
|
|
|
/** @var Package $package */ |
86
|
|
|
foreach($this->getArrayCopy() as $package) { |
87
|
|
|
if (!($package instanceof Package)) { |
88
|
|
|
continue; |
89
|
|
|
} |
90
|
|
|
$this->indexedBy['name'][$package->getName()] = $package; |
91
|
|
|
} |
92
|
|
|
return $this->indexedBy['name']; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @return array |
97
|
|
|
*/ |
98
|
|
View Code Duplication |
private function getIndexedByNamespace(): array |
|
|
|
|
99
|
|
|
{ |
100
|
|
|
if (!empty($this->indexedBy['namespace'])) { |
101
|
|
|
return $this->indexedBy['namespace']; |
102
|
|
|
} |
103
|
|
|
/** @var Package $package */ |
104
|
|
|
foreach($this->getArrayCopy() as $package) { |
105
|
|
|
if (!($package instanceof Package)) { |
106
|
|
|
continue; |
107
|
|
|
} |
108
|
|
|
$this->indexedBy['namespace'][$package->getNamespace()] = $package; |
109
|
|
|
} |
110
|
|
|
return $this->indexedBy['namespace']; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
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.