Total Complexity | 56 |
Total Lines | 367 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 1 |
Complex classes like ZipContainer often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ZipContainer, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
13 | class ZipContainer extends ImmutableZipContainer |
||
14 | { |
||
15 | /** @var ImmutableZipContainer|null */ |
||
16 | private $sourceContainer; |
||
17 | |||
18 | /** |
||
19 | * @var int|null Apk zipalign value |
||
20 | * |
||
21 | * @todo remove and use in ApkFileWriter |
||
22 | */ |
||
23 | private $zipAlign; |
||
24 | |||
25 | /** |
||
26 | * MutableZipContainer constructor. |
||
27 | * |
||
28 | * @param ImmutableZipContainer|null $sourceContainer |
||
29 | */ |
||
30 | public function __construct(ImmutableZipContainer $sourceContainer = null) |
||
31 | { |
||
32 | $entries = []; |
||
33 | $archiveComment = null; |
||
34 | |||
35 | if ($sourceContainer !== null) { |
||
36 | foreach ($sourceContainer->getEntries() as $entryName => $entry) { |
||
37 | $entries[$entryName] = clone $entry; |
||
38 | } |
||
39 | $archiveComment = $sourceContainer->getArchiveComment(); |
||
40 | } |
||
41 | parent::__construct($entries, $archiveComment); |
||
42 | $this->sourceContainer = $sourceContainer; |
||
43 | } |
||
44 | |||
45 | /** |
||
46 | * @return ImmutableZipContainer|null |
||
47 | */ |
||
48 | public function getSourceContainer() |
||
49 | { |
||
50 | return $this->sourceContainer; |
||
51 | } |
||
52 | |||
53 | /** |
||
54 | * @param ZipEntry $entry |
||
55 | */ |
||
56 | public function addEntry(ZipEntry $entry) |
||
57 | { |
||
58 | $this->entries[$entry->getName()] = $entry; |
||
59 | } |
||
60 | |||
61 | /** |
||
62 | * @param string|ZipEntry $entry |
||
63 | * |
||
64 | * @return bool |
||
65 | */ |
||
66 | public function deleteEntry($entry) |
||
67 | { |
||
68 | $entry = $entry instanceof ZipEntry ? $entry->getName() : (string) $entry; |
||
69 | |||
70 | if (isset($this->entries[$entry])) { |
||
71 | unset($this->entries[$entry]); |
||
72 | |||
73 | return true; |
||
74 | } |
||
75 | |||
76 | return false; |
||
77 | } |
||
78 | |||
79 | /** |
||
80 | * @param string|ZipEntry $old |
||
81 | * @param string|ZipEntry $new |
||
82 | * |
||
83 | * @throws ZipException |
||
84 | * |
||
85 | * @return ZipEntry New zip entry |
||
86 | */ |
||
87 | public function renameEntry($old, $new) |
||
88 | { |
||
89 | $old = $old instanceof ZipEntry ? $old->getName() : (string) $old; |
||
90 | $new = $new instanceof ZipEntry ? $new->getName() : (string) $new; |
||
91 | |||
92 | if (isset($this->entries[$new])) { |
||
93 | throw new InvalidArgumentException('New entry name ' . $new . ' is exists.'); |
||
94 | } |
||
95 | |||
96 | $entry = $this->getEntry($old); |
||
97 | $newEntry = $entry->rename($new); |
||
98 | |||
99 | $this->deleteEntry($entry); |
||
100 | $this->addEntry($newEntry); |
||
101 | |||
102 | return $newEntry; |
||
103 | } |
||
104 | |||
105 | /** |
||
106 | * @param string|ZipEntry $entryName |
||
107 | * |
||
108 | * @throws ZipEntryNotFoundException |
||
109 | * |
||
110 | * @return ZipEntry |
||
111 | */ |
||
112 | public function getEntry($entryName) |
||
113 | { |
||
114 | $entry = $this->getEntryOrNull($entryName); |
||
115 | |||
116 | if ($entry !== null) { |
||
117 | return $entry; |
||
118 | } |
||
119 | |||
120 | throw new ZipEntryNotFoundException($entryName); |
||
121 | } |
||
122 | |||
123 | /** |
||
124 | * @param string|ZipEntry $entryName |
||
125 | * |
||
126 | * @return ZipEntry|null |
||
127 | */ |
||
128 | public function getEntryOrNull($entryName) |
||
129 | { |
||
130 | $entryName = $entryName instanceof ZipEntry ? $entryName->getName() : (string) $entryName; |
||
131 | |||
132 | return isset($this->entries[$entryName]) ? $this->entries[$entryName] : null; |
||
133 | } |
||
134 | |||
135 | /** |
||
136 | * @param string|ZipEntry $entryName |
||
137 | * |
||
138 | * @return bool |
||
139 | */ |
||
140 | public function hasEntry($entryName) |
||
141 | { |
||
142 | $entryName = $entryName instanceof ZipEntry ? $entryName->getName() : (string) $entryName; |
||
143 | |||
144 | return isset($this->entries[$entryName]); |
||
145 | } |
||
146 | |||
147 | /** |
||
148 | * Delete all entries. |
||
149 | */ |
||
150 | public function deleteAll() |
||
151 | { |
||
152 | $this->entries = []; |
||
153 | } |
||
154 | |||
155 | /** |
||
156 | * Delete entries by regex pattern. |
||
157 | * |
||
158 | * @param string $regexPattern Regex pattern |
||
159 | * |
||
160 | * @return ZipEntry[] Deleted entries |
||
161 | */ |
||
162 | public function deleteByRegex($regexPattern) |
||
163 | { |
||
164 | if (empty($regexPattern)) { |
||
165 | throw new InvalidArgumentException('The regex pattern is not specified'); |
||
166 | } |
||
167 | |||
168 | /** @var ZipEntry[] $found */ |
||
169 | $found = []; |
||
170 | |||
171 | foreach ($this->entries as $entryName => $entry) { |
||
172 | if (preg_match($regexPattern, $entryName)) { |
||
173 | $found[] = $entry; |
||
174 | } |
||
175 | } |
||
176 | |||
177 | foreach ($found as $entry) { |
||
178 | $this->deleteEntry($entry); |
||
179 | } |
||
180 | |||
181 | return $found; |
||
182 | } |
||
183 | |||
184 | /** |
||
185 | * Undo all changes done in the archive. |
||
186 | */ |
||
187 | public function unchangeAll() |
||
188 | { |
||
189 | $this->entries = []; |
||
190 | |||
191 | if ($this->sourceContainer !== null) { |
||
192 | foreach ($this->sourceContainer->getEntries() as $entry) { |
||
193 | $this->entries[$entry->getName()] = clone $entry; |
||
194 | } |
||
195 | } |
||
196 | $this->unchangeArchiveComment(); |
||
197 | } |
||
198 | |||
199 | /** |
||
200 | * Undo change archive comment. |
||
201 | */ |
||
202 | public function unchangeArchiveComment() |
||
203 | { |
||
204 | $this->archiveComment = null; |
||
205 | |||
206 | if ($this->sourceContainer !== null) { |
||
207 | $this->archiveComment = $this->sourceContainer->archiveComment; |
||
208 | } |
||
209 | } |
||
210 | |||
211 | /** |
||
212 | * Revert all changes done to an entry with the given name. |
||
213 | * |
||
214 | * @param string|ZipEntry $entry Entry name or ZipEntry |
||
215 | * |
||
216 | * @return bool |
||
217 | */ |
||
218 | public function unchangeEntry($entry) |
||
219 | { |
||
220 | $entry = $entry instanceof ZipEntry ? $entry->getName() : (string) $entry; |
||
221 | |||
222 | if ( |
||
223 | $this->sourceContainer !== null && |
||
224 | isset($this->entries[$entry], $this->sourceContainer->entries[$entry]) |
||
225 | ) { |
||
226 | $this->entries[$entry] = clone $this->sourceContainer->entries[$entry]; |
||
227 | |||
228 | return true; |
||
229 | } |
||
230 | |||
231 | return false; |
||
232 | } |
||
233 | |||
234 | /** |
||
235 | * Entries sort by name. |
||
236 | * |
||
237 | * Example: |
||
238 | * ```php |
||
239 | * $zipContainer->sortByName(static function (string $nameA, string $nameB): int { |
||
240 | * return strcmp($nameA, $nameB); |
||
241 | * }); |
||
242 | * ``` |
||
243 | * |
||
244 | * @param callable $cmp |
||
245 | */ |
||
246 | public function sortByName(callable $cmp) |
||
247 | { |
||
248 | uksort($this->entries, $cmp); |
||
249 | } |
||
250 | |||
251 | /** |
||
252 | * Entries sort by entry. |
||
253 | * |
||
254 | * Example: |
||
255 | * ```php |
||
256 | * $zipContainer->sortByEntry(static function (ZipEntry $a, ZipEntry $b): int { |
||
257 | * return strcmp($a->getName(), $b->getName()); |
||
258 | * }); |
||
259 | * ``` |
||
260 | * |
||
261 | * @param callable $cmp |
||
262 | */ |
||
263 | public function sortByEntry(callable $cmp) |
||
266 | } |
||
267 | |||
268 | /** |
||
269 | * @param string|null $archiveComment |
||
270 | */ |
||
271 | public function setArchiveComment($archiveComment) |
||
282 | } |
||
283 | |||
284 | /** |
||
285 | * @return ZipEntryMatcher |
||
286 | */ |
||
287 | public function matcher() |
||
288 | { |
||
289 | return new ZipEntryMatcher($this); |
||
290 | } |
||
291 | |||
292 | /** |
||
293 | * Specify a password for extracting files. |
||
294 | * |
||
295 | * @param string|null $password |
||
296 | */ |
||
297 | public function setReadPassword($password) |
||
298 | { |
||
299 | if ($this->sourceContainer !== null) { |
||
300 | foreach ($this->sourceContainer->entries as $entry) { |
||
301 | if ($entry->isEncrypted()) { |
||
302 | $entry->setPassword($password); |
||
303 | } |
||
304 | } |
||
305 | } |
||
306 | } |
||
307 | |||
308 | /** |
||
309 | * @param string $entryName |
||
310 | * @param string $password |
||
311 | * |
||
312 | * @throws ZipEntryNotFoundException |
||
313 | * @throws ZipException |
||
314 | */ |
||
315 | public function setReadPasswordEntry($entryName, $password) |
||
323 | } |
||
324 | } |
||
325 | |||
326 | /** |
||
327 | * @return int|null |
||
328 | */ |
||
329 | public function getZipAlign() |
||
330 | { |
||
331 | return $this->zipAlign; |
||
332 | } |
||
333 | |||
334 | /** |
||
335 | * @param int|null $zipAlign |
||
336 | */ |
||
337 | public function setZipAlign($zipAlign) |
||
338 | { |
||
339 | $this->zipAlign = $zipAlign === null ? null : (int) $zipAlign; |
||
340 | } |
||
341 | |||
342 | /** |
||
343 | * @return bool |
||
344 | */ |
||
345 | public function isZipAlign() |
||
348 | } |
||
349 | |||
350 | /** |
||
351 | * @param string|null $writePassword |
||
352 | */ |
||
353 | public function setWritePassword($writePassword) |
||
354 | { |
||
355 | $this->matcher()->all()->setPassword($writePassword); |
||
356 | } |
||
357 | |||
358 | /** |
||
359 | * Remove password. |
||
360 | */ |
||
361 | public function removePassword() |
||
364 | } |
||
365 | |||
366 | /** |
||
367 | * @param string|ZipEntry $entryName |
||
368 | */ |
||
369 | public function removePasswordEntry($entryName) |
||
372 | } |
||
373 | |||
374 | /** |
||
375 | * @param int $encryptionMethod |
||
376 | */ |
||
377 | public function setEncryptionMethod($encryptionMethod = ZipEncryptionMethod::WINZIP_AES_256) |
||
380 | } |
||
381 | } |
||
382 |