ZipContainer   C
last analyzed

Complexity

Total Complexity 56

Size/Duplication

Total Lines 372
Duplicated Lines 0 %

Test Coverage

Coverage 84.17%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 84
c 1
b 0
f 1
dl 0
loc 372
ccs 101
cts 120
cp 0.8417
rs 5.5199
wmc 56

26 Methods

Rating   Name   Duplication   Size   Complexity  
A sortByEntry() 0 3 1
A setArchiveComment() 0 11 4
A deleteByRegex() 0 20 5
A getSourceContainer() 0 3 1
A setReadPasswordEntry() 0 8 3
A setReadPassword() 0 6 4
A isZipAlign() 0 3 1
A unchangeEntry() 0 14 4
A removePassword() 0 3 1
A getEntryOrNull() 0 5 3
A sortByName() 0 3 1
A unchangeArchiveComment() 0 6 2
A deleteEntry() 0 11 3
A removePasswordEntry() 0 3 1
A hasEntry() 0 5 2
A __construct() 0 13 3
A matcher() 0 3 1
A unchangeAll() 0 10 3
A getEntry() 0 9 2
A addEntry() 0 3 1
A setZipAlign() 0 3 2
A getZipAlign() 0 3 1
A setEncryptionMethod() 0 3 1
A renameEntry() 0 16 4
A deleteAll() 0 3 1
A setWritePassword() 0 3 1

How to fix   Complexity   

Complex Class

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
2
3
namespace PhpZip\Model;
4
5
use PhpZip\Constants\ZipEncryptionMethod;
6
use PhpZip\Exception\InvalidArgumentException;
7
use PhpZip\Exception\ZipEntryNotFoundException;
8
use PhpZip\Exception\ZipException;
9
10
/**
11
 * Class ZipContainer.
12
 */
13
class ZipContainer extends ImmutableZipContainer
14
{
15
    /**
16
     * @var ImmutableZipContainer|null The source container contains zip entries from
17
     *                                 an open zip archive. The source container makes
18
     *                                 it possible to undo changes in the archive.
19
     *                                 When cloning, this container is not cloned.
20
     */
21
    private $sourceContainer;
22
23
    /**
24
     * @var int|null Apk zipalign value
25
     *
26
     * @todo remove and use in ApkFileWriter
27
     */
28
    private $zipAlign;
29
30
    /**
31
     * MutableZipContainer constructor.
32
     *
33
     * @param ImmutableZipContainer|null $sourceContainer
34
     */
35 321
    public function __construct(ImmutableZipContainer $sourceContainer = null)
36
    {
37 321
        $entries = [];
38 321
        $archiveComment = null;
39
40 321
        if ($sourceContainer !== null) {
41 136
            foreach ($sourceContainer->getEntries() as $entryName => $entry) {
42 134
                $entries[$entryName] = clone $entry;
43
            }
44 136
            $archiveComment = $sourceContainer->getArchiveComment();
45
        }
46 321
        parent::__construct($entries, $archiveComment);
47 321
        $this->sourceContainer = $sourceContainer;
48 321
    }
49
50
    /**
51
     * @return ImmutableZipContainer|null
52
     */
53
    public function getSourceContainer()
54
    {
55
        return $this->sourceContainer;
56
    }
57
58
    /**
59
     * @param ZipEntry $entry
60
     */
61 159
    public function addEntry(ZipEntry $entry)
62
    {
63 159
        $this->entries[$entry->getName()] = $entry;
64 159
    }
65
66
    /**
67
     * @param string|ZipEntry $entry
68
     *
69
     * @return bool
70
     */
71 21
    public function deleteEntry($entry)
72
    {
73 21
        $entry = $entry instanceof ZipEntry ? $entry->getName() : (string) $entry;
74
75 21
        if (isset($this->entries[$entry])) {
76 19
            unset($this->entries[$entry]);
77
78 19
            return true;
79
        }
80
81 2
        return false;
82
    }
83
84
    /**
85
     * @param string|ZipEntry $old
86
     * @param string|ZipEntry $new
87
     *
88
     * @throws ZipException
89
     *
90
     * @return ZipEntry New zip entry
91
     */
92 9
    public function renameEntry($old, $new)
93
    {
94 9
        $old = $old instanceof ZipEntry ? $old->getName() : (string) $old;
95 9
        $new = $new instanceof ZipEntry ? $new->getName() : (string) $new;
96
97 9
        if (isset($this->entries[$new])) {
98 2
            throw new InvalidArgumentException('New entry name ' . $new . ' is exists.');
99
        }
100
101 7
        $entry = $this->getEntry($old);
102 5
        $newEntry = $entry->rename($new);
103
104 5
        $this->deleteEntry($entry);
105 5
        $this->addEntry($newEntry);
106
107 5
        return $newEntry;
108
    }
109
110
    /**
111
     * @param string|ZipEntry $entryName
112
     *
113
     * @throws ZipEntryNotFoundException
114
     *
115
     * @return ZipEntry
116
     */
117 116
    public function getEntry($entryName)
118
    {
119 116
        $entry = $this->getEntryOrNull($entryName);
120
121 116
        if ($entry !== null) {
122 107
            return $entry;
123
        }
124
125 10
        throw new ZipEntryNotFoundException($entryName);
126
    }
127
128
    /**
129
     * @param string|ZipEntry $entryName
130
     *
131
     * @return ZipEntry|null
132
     */
133 116
    public function getEntryOrNull($entryName)
134
    {
135 116
        $entryName = $entryName instanceof ZipEntry ? $entryName->getName() : (string) $entryName;
136
137 116
        return isset($this->entries[$entryName]) ? $this->entries[$entryName] : null;
138
    }
139
140
    /**
141
     * @param string|ZipEntry $entryName
142
     *
143
     * @return bool
144
     */
145 57
    public function hasEntry($entryName)
146
    {
147 57
        $entryName = $entryName instanceof ZipEntry ? $entryName->getName() : (string) $entryName;
148
149 57
        return isset($this->entries[$entryName]);
150
    }
151
152
    /**
153
     * Delete all entries.
154
     */
155 2
    public function deleteAll()
156
    {
157 2
        $this->entries = [];
158 2
    }
159
160
    /**
161
     * Delete entries by regex pattern.
162
     *
163
     * @param string $regexPattern Regex pattern
164
     *
165
     * @return ZipEntry[] Deleted entries
166
     */
167
    public function deleteByRegex($regexPattern)
168
    {
169
        if (empty($regexPattern)) {
170
            throw new InvalidArgumentException('The regex pattern is not specified');
171
        }
172
173
        /** @var ZipEntry[] $found */
174
        $found = [];
175
176
        foreach ($this->entries as $entryName => $entry) {
177
            if (preg_match($regexPattern, $entryName)) {
178
                $found[] = $entry;
179
            }
180
        }
181
182
        foreach ($found as $entry) {
183
            $this->deleteEntry($entry);
184
        }
185
186
        return $found;
187
    }
188
189
    /**
190
     * Undo all changes done in the archive.
191
     */
192 2
    public function unchangeAll()
193
    {
194 2
        $this->entries = [];
195
196 2
        if ($this->sourceContainer !== null) {
197 2
            foreach ($this->sourceContainer->getEntries() as $entry) {
198 2
                $this->entries[$entry->getName()] = clone $entry;
199
            }
200
        }
201 2
        $this->unchangeArchiveComment();
202 2
    }
203
204
    /**
205
     * Undo change archive comment.
206
     */
207 4
    public function unchangeArchiveComment()
208
    {
209 4
        $this->archiveComment = null;
210
211 4
        if ($this->sourceContainer !== null) {
212 4
            $this->archiveComment = $this->sourceContainer->archiveComment;
213
        }
214 4
    }
215
216
    /**
217
     * Revert all changes done to an entry with the given name.
218
     *
219
     * @param string|ZipEntry $entry Entry name or ZipEntry
220
     *
221
     * @return bool
222
     */
223 2
    public function unchangeEntry($entry)
224
    {
225 2
        $entry = $entry instanceof ZipEntry ? $entry->getName() : (string) $entry;
226
227
        if (
228 2
            $this->sourceContainer !== null &&
229 2
            isset($this->entries[$entry], $this->sourceContainer->entries[$entry])
230
        ) {
231 2
            $this->entries[$entry] = clone $this->sourceContainer->entries[$entry];
232
233 2
            return true;
234
        }
235
236
        return false;
237
    }
238
239
    /**
240
     * Entries sort by name.
241
     *
242
     * Example:
243
     * ```php
244
     * $zipContainer->sortByName(static function (string $nameA, string $nameB): int {
245
     *     return strcmp($nameA, $nameB);
246
     * });
247
     * ```
248
     *
249
     * @param callable $cmp
250
     */
251
    public function sortByName(callable $cmp)
252
    {
253
        uksort($this->entries, $cmp);
254
    }
255
256
    /**
257
     * Entries sort by entry.
258
     *
259
     * Example:
260
     * ```php
261
     * $zipContainer->sortByEntry(static function (ZipEntry $a, ZipEntry $b): int {
262
     *     return strcmp($a->getName(), $b->getName());
263
     * });
264
     * ```
265
     *
266
     * @param callable $cmp
267
     */
268 1
    public function sortByEntry(callable $cmp)
269
    {
270 1
        uasort($this->entries, $cmp);
271 1
    }
272
273
    /**
274
     * @param string|null $archiveComment
275
     */
276 8
    public function setArchiveComment($archiveComment)
277
    {
278 8
        if ($archiveComment !== null && $archiveComment !== '') {
279 8
            $archiveComment = (string) $archiveComment;
280 8
            $length = \strlen($archiveComment);
281
282 8
            if ($length > 0xffff) {
283 2
                throw new InvalidArgumentException('Length comment out of range');
284
            }
285
        }
286 6
        $this->archiveComment = $archiveComment;
287 6
    }
288
289
    /**
290
     * @return ZipEntryMatcher
291
     */
292 18
    public function matcher()
293
    {
294 18
        return new ZipEntryMatcher($this);
295
    }
296
297
    /**
298
     * Specify a password for extracting files.
299
     *
300
     * @param string|null $password
301
     */
302 9
    public function setReadPassword($password)
303
    {
304 9
        if ($this->sourceContainer !== null) {
305 9
            foreach ($this->sourceContainer->entries as $entry) {
306 9
                if ($entry->isEncrypted()) {
307 9
                    $entry->setPassword($password);
308
                }
309
            }
310
        }
311 9
    }
312
313
    /**
314
     * @param string $entryName
315
     * @param string $password
316
     *
317
     * @throws ZipEntryNotFoundException
318
     * @throws ZipException
319
     */
320 2
    public function setReadPasswordEntry($entryName, $password)
321
    {
322 2
        if (!isset($this->sourceContainer->entries[$entryName])) {
323
            throw new ZipEntryNotFoundException($entryName);
324
        }
325
326 2
        if ($this->sourceContainer->entries[$entryName]->isEncrypted()) {
327 2
            $this->sourceContainer->entries[$entryName]->setPassword($password);
328
        }
329 2
    }
330
331
    /**
332
     * @return int|null
333
     */
334
    public function getZipAlign()
335
    {
336
        return $this->zipAlign;
337
    }
338
339
    /**
340
     * @param int|null $zipAlign
341
     */
342 4
    public function setZipAlign($zipAlign)
343
    {
344 4
        $this->zipAlign = $zipAlign === null ? null : (int) $zipAlign;
345 4
    }
346
347
    /**
348
     * @return bool
349
     */
350 134
    public function isZipAlign()
351
    {
352 134
        return $this->zipAlign !== null;
353
    }
354
355
    /**
356
     * @param string|null $writePassword
357
     */
358 11
    public function setWritePassword($writePassword)
359
    {
360 11
        $this->matcher()->all()->setPassword($writePassword);
361 11
    }
362
363
    /**
364
     * Remove password.
365
     */
366 2
    public function removePassword()
367
    {
368 2
        $this->matcher()->all()->setPassword(null);
369 2
    }
370
371
    /**
372
     * @param string|ZipEntry $entryName
373
     */
374 1
    public function removePasswordEntry($entryName)
375
    {
376 1
        $this->matcher()->add($entryName)->setPassword(null);
377 1
    }
378
379
    /**
380
     * @param int $encryptionMethod
381
     */
382 11
    public function setEncryptionMethod($encryptionMethod = ZipEncryptionMethod::WINZIP_AES_256)
383
    {
384 11
        $this->matcher()->all()->setEncryptionMethod($encryptionMethod);
385 10
    }
386
}
387