1 | <?php |
||||
2 | |||||
3 | /** |
||||
4 | * Author: mickael Louzet @micklouzet |
||||
5 | * File: SearcherTrait.php |
||||
6 | * Created: 08/12/2019 |
||||
7 | */ |
||||
8 | |||||
9 | namespace ComposerLockParser; |
||||
10 | |||||
11 | use ComposerLockParser\Package\Package; |
||||
12 | |||||
13 | trait SearcherTrait { |
||||
14 | |||||
15 | /** |
||||
16 | * @param string $name |
||||
17 | * |
||||
18 | * @return Package |
||||
19 | */ |
||||
20 | public function getByName(string $name): Package |
||||
21 | { |
||||
22 | if (!$this->nameExists($name)) { |
||||
23 | throw new \UnexpectedValueException(sprintf('Sorry, Package %s not found !', $name)); |
||||
24 | } |
||||
25 | return $this->getIndexedByName()[$name]; |
||||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||
26 | } |
||||
27 | |||||
28 | /** |
||||
29 | * @param string $namespace |
||||
30 | * |
||||
31 | * @return Package |
||||
32 | */ |
||||
33 | public function getByNamespace(string $namespace): Package |
||||
34 | { |
||||
35 | if (!$this->namespaceExists($namespace)) { |
||||
36 | throw new \UnexpectedValueException(sprintf('Sorry, namespace %s not found !', $namespace)); |
||||
37 | } |
||||
38 | return $this->getIndexedByNamespace()[$namespace]; |
||||
0 ignored issues
–
show
It seems like
getIndexedByNamespace() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
39 | } |
||||
40 | |||||
41 | /** |
||||
42 | * @param string $name |
||||
43 | * |
||||
44 | * @return bool |
||||
45 | */ |
||||
46 | public function nameExists(string $name): bool |
||||
47 | { |
||||
48 | return array_key_exists($name, $this->getIndexedByName()); |
||||
49 | } |
||||
50 | |||||
51 | /** |
||||
52 | * @param string $namespace |
||||
53 | * |
||||
54 | * @return bool |
||||
55 | */ |
||||
56 | public function namespaceExists($namespace): bool |
||||
57 | { |
||||
58 | return array_key_exists($namespace, $this->getIndexedByNamespace()); |
||||
59 | } |
||||
60 | } |
||||
61 |