This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | /* |
||
4 | * (c) 2018 Dennis Birkholz <[email protected]> |
||
5 | * |
||
6 | * $Id$ |
||
7 | * Author: $Format:%an <%ae>, %ai$ |
||
8 | * Committer: $Format:%cn <%ce>, %ci$ |
||
9 | */ |
||
10 | |||
11 | namespace morgue\archive; |
||
12 | |||
13 | /** |
||
14 | * @author Dennis Birkholz <[email protected]> |
||
15 | */ |
||
16 | final class Archive |
||
17 | { |
||
18 | /** |
||
19 | * @var string |
||
20 | */ |
||
21 | private $comment; |
||
22 | |||
23 | /** |
||
24 | * @var ArchiveEntry[] |
||
25 | */ |
||
26 | private $entries = []; |
||
27 | |||
28 | /** |
||
29 | * @var int[] |
||
30 | */ |
||
31 | private $entriesByName = []; |
||
32 | |||
33 | /** |
||
34 | * @return string|null |
||
35 | */ |
||
36 | 1 | public function getComment() |
|
37 | { |
||
38 | 1 | return $this->comment; |
|
39 | } |
||
40 | |||
41 | /** |
||
42 | * @param string $comment |
||
43 | * @return Archive |
||
44 | */ |
||
45 | 1 | public function withComment(string $comment = null): Archive |
|
46 | { |
||
47 | 1 | $obj = clone $this; |
|
48 | 1 | $obj->comment = $comment; |
|
49 | 1 | return $obj; |
|
50 | } |
||
51 | |||
52 | /** |
||
53 | * @return ArchiveEntry[] |
||
54 | */ |
||
55 | 1 | public function getEntries(): array |
|
56 | { |
||
57 | 1 | return $this->entries; |
|
58 | } |
||
59 | |||
60 | /** |
||
61 | * @param $idOrName |
||
62 | * @return ArchiveEntry|null |
||
63 | */ |
||
64 | 2 | public function getEntry($idOrName) |
|
65 | { |
||
66 | 2 | if (\is_int($idOrName)) { |
|
67 | 1 | return (isset($this->entries[$idOrName]) ? $this->entries[$idOrName] : null); |
|
68 | } |
||
69 | |||
70 | 2 | elseif (\is_string($idOrName)) { |
|
71 | 1 | return (isset($this->entriesByName[$idOrName]) ? $this->entries[$this->entriesByName[$idOrName]] : null); |
|
72 | } |
||
73 | |||
74 | else { |
||
75 | 1 | throw new \InvalidArgumentException("Can only search by entry id or name."); |
|
76 | } |
||
77 | } |
||
78 | |||
79 | /** |
||
80 | * Add an entry to the archive. |
||
81 | * |
||
82 | * @param ArchiveEntry $entry |
||
83 | * @param int|null $index |
||
84 | * @return Archive |
||
85 | */ |
||
86 | 2 | public function addEntry(ArchiveEntry $entry, int $index = null): Archive |
|
87 | { |
||
88 | 2 | if (\in_array($entry, $this->entries, true)) { |
|
89 | 1 | throw new \InvalidArgumentException("The supplied entry exists already in this Archive."); |
|
90 | } |
||
91 | |||
92 | 2 | if ($index !== null && isset($this->entries[$index])) { |
|
93 | 1 | throw new \InvalidArgumentException("Can not replace existing index #$index in addEntry()."); |
|
94 | } |
||
95 | |||
96 | 2 | if (isset($this->entriesByName[$entry->getName()])) { |
|
97 | 1 | throw new \InvalidArgumentException("Can not add entry with already existing name '" . $entry->getName() . "'."); |
|
98 | } |
||
99 | |||
100 | 2 | $obj = clone $this; |
|
101 | 2 | if ($index !== null) { |
|
102 | 1 | $obj->entries[$index] = $entry; |
|
103 | } else { |
||
104 | 2 | $obj->entries[] = $entry; |
|
105 | 2 | \end($obj->entries); |
|
106 | 2 | $index = \key($obj->entries); |
|
107 | } |
||
108 | 2 | $obj->entriesByName[$entry->getName()] = $index; |
|
109 | 2 | return $obj; |
|
110 | } |
||
111 | |||
112 | /** |
||
113 | * @param ArchiveEntry $oldEntry |
||
114 | * @param ArchiveEntry $newEntry |
||
115 | * @return Archive |
||
116 | */ |
||
117 | 2 | public function replaceEntry(ArchiveEntry $oldEntry, ArchiveEntry $newEntry): Archive |
|
118 | { |
||
119 | 2 | if (!isset($this->entriesByName[$oldEntry->getName()])) { |
|
120 | 1 | throw new \InvalidArgumentException("Can not remove entry, not found!"); |
|
121 | } |
||
122 | |||
123 | 1 | $oldIndex = $this->entriesByName[$oldEntry->getName()]; |
|
124 | 1 | return $this->delEntry($oldIndex)->addEntry($newEntry, $oldIndex); |
|
125 | } |
||
126 | |||
127 | /** |
||
128 | * Remove the supplied entry from the archive. |
||
129 | * |
||
130 | * @param ArchiveEntry|int|string $entryOrIdOrName The entry, the entry ID or the entry name |
||
131 | * @return Archive |
||
132 | */ |
||
133 | 2 | public function delEntry($entryOrIdOrName): Archive |
|
134 | { |
||
135 | 2 | if (\is_string($entryOrIdOrName)) { |
|
136 | 1 | View Code Duplication | if (!isset($this->entriesByName[$entryOrIdOrName])) { |
0 ignored issues
–
show
|
|||
137 | 1 | throw new \InvalidArgumentException("No entry with name '$entryOrIdOrName' found in this Archive."); |
|
138 | } else { |
||
139 | 1 | $index = $this->entriesByName[$entryOrIdOrName]; |
|
140 | } |
||
141 | } |
||
142 | |||
143 | 2 | elseif ($entryOrIdOrName instanceof ArchiveEntry) { |
|
144 | 2 | if (($index = \array_search($entryOrIdOrName, $this->entries, true)) === false) { |
|
145 | 2 | throw new \InvalidArgumentException("The supplied entry is not found in this Archive."); |
|
146 | } |
||
147 | } |
||
148 | |||
149 | 2 | elseif (\is_int($entryOrIdOrName)) { |
|
150 | 2 | View Code Duplication | if (!isset($this->entries[$entryOrIdOrName])) { |
0 ignored issues
–
show
This code seems to be duplicated across your project.
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. ![]() |
|||
151 | 1 | throw new \InvalidArgumentException("No entry with id #$entryOrIdOrName is found in this Archive."); |
|
152 | } else { |
||
153 | 2 | $index = (int)$entryOrIdOrName; |
|
154 | } |
||
155 | } |
||
156 | |||
157 | else { |
||
158 | 1 | throw new \InvalidArgumentException("Invalid argument, can only delete ArchiveEntry instance, by index or by name."); |
|
159 | } |
||
160 | |||
161 | 2 | $entry = $this->entries[$index]; |
|
162 | |||
163 | 2 | $obj = clone $this; |
|
164 | 2 | unset($obj->entriesByName[$entry->getName()]); |
|
165 | 2 | unset($obj->entries[$index]); |
|
166 | 2 | return $obj; |
|
167 | } |
||
168 | } |
||
169 |
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.