Total Complexity | 40 |
Total Lines | 269 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
Complex classes like NelexaZip 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 NelexaZip, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | class NelexaZip extends BasicPureDriver |
||
17 | { |
||
18 | const PACKAGE_NAME = 'nelexa/zip'; |
||
19 | const MAIN_CLASS = '\\PhpZip\\ZipFile'; |
||
20 | |||
21 | /** |
||
22 | * @var ZipFile |
||
23 | */ |
||
24 | protected $zip; |
||
25 | |||
26 | /** |
||
27 | * @var array |
||
28 | */ |
||
29 | protected $files; |
||
30 | |||
31 | public static function getDescription() |
||
32 | { |
||
33 | return 'nelexa/zip driver'; |
||
34 | } |
||
35 | |||
36 | public static function getSupportedFormats() |
||
40 | ]; |
||
41 | } |
||
42 | |||
43 | public static function checkFormatSupport($format) |
||
59 | ]; |
||
60 | } |
||
61 | |||
62 | /** |
||
63 | * @param array $files |
||
64 | * @param $archiveFileName |
||
65 | * @param $archiveFormat |
||
66 | * @param $compressionLevel |
||
67 | * @param $password |
||
68 | * @param $fileProgressCallable |
||
69 | * @return int |
||
70 | * @throws ArchiveCreationException |
||
71 | * @throws UnsupportedOperationException |
||
72 | */ |
||
73 | public static function createArchive( |
||
74 | array $files, |
||
75 | $archiveFileName, |
||
76 | $archiveFormat, |
||
77 | $compressionLevel = self::COMPRESSION_AVERAGE, |
||
78 | $password = null, |
||
79 | $fileProgressCallable = null) |
||
80 | { |
||
81 | if ($fileProgressCallable !== null && !is_callable($fileProgressCallable)) { |
||
82 | throw new ArchiveCreationException('File progress callable is not callable'); |
||
83 | } |
||
84 | |||
85 | try { |
||
86 | $zipFile = static::createArchiveInternal($files, $password, $fileProgressCallable); |
||
87 | $zipFile->saveAsFile($archiveFileName)->close(); |
||
88 | } catch (\Exception $e) { |
||
89 | throw new ArchiveCreationException('Could not create archive: '.$e->getMessage(), $e->getCode(), $e); |
||
90 | } |
||
91 | return count($files); |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * @param array $files |
||
96 | * @param string $archiveFormat |
||
97 | * @param int $compressionLevel |
||
98 | * @param string $password |
||
99 | * @param callable|null $fileProgressCallable |
||
100 | * @return string Content of archive |
||
101 | * @throws ArchiveCreationException |
||
102 | */ |
||
103 | public static function createArchiveInString( |
||
104 | array $files, |
||
105 | $archiveFormat, |
||
106 | $compressionLevel = self::COMPRESSION_AVERAGE, |
||
107 | $password = null, |
||
108 | $fileProgressCallable = null |
||
109 | ) { |
||
110 | if ($fileProgressCallable !== null && !is_callable($fileProgressCallable)) { |
||
111 | throw new ArchiveCreationException('File progress callable is not callable'); |
||
112 | } |
||
113 | |||
114 | try { |
||
115 | $zipFile = static::createArchiveInternal($files, $password, $fileProgressCallable); |
||
116 | return $zipFile->outputAsString(); |
||
117 | } catch (\Exception $e) { |
||
118 | throw new ArchiveCreationException('Could not create archive: '.$e->getMessage(), $e->getCode(), $e); |
||
119 | } |
||
120 | } |
||
121 | |||
122 | protected static function createArchiveInternal(array $files, $password, $fileProgressCallable = null) |
||
141 | } |
||
142 | |||
143 | /** |
||
144 | * @inheritDoc |
||
145 | * @throws \PhpZip\Exception\ZipException |
||
146 | */ |
||
147 | public function __construct($archiveFileName, $format, $password = null) |
||
148 | { |
||
149 | parent::__construct($archiveFileName, $format); |
||
150 | $this->zip = new ZipFile(); |
||
151 | $this->zip->openFile($archiveFileName); |
||
152 | if ($password !== null) { |
||
153 | $this->zip->setReadPassword($password); |
||
154 | } |
||
155 | } |
||
156 | |||
157 | /** |
||
158 | * @inheritDoc |
||
159 | */ |
||
160 | public function getArchiveInformation() |
||
161 | { |
||
162 | $this->files = []; |
||
163 | $information = new ArchiveInformation(); |
||
164 | |||
165 | $files = method_exists($this->zip, 'getAllInfo') |
||
166 | ? $this->zip->getAllInfo() |
||
167 | : $this->zip->getEntries(); |
||
168 | |||
169 | foreach ($files as $info) { |
||
170 | if (method_exists($info, 'isFolder') ? $info->isFolder() : $info->isDirectory()) |
||
171 | continue; |
||
172 | |||
173 | $this->files[] = $information->files[] = str_replace('\\', '/', $info->getName()); |
||
174 | $information->compressedFilesSize += $info->getCompressedSize(); |
||
175 | $information->uncompressedFilesSize += method_exists($info, 'getSize') ? $info->getSize() : $info->getUncompressedSize(); |
||
176 | } |
||
177 | return $information; |
||
178 | } |
||
179 | |||
180 | /** |
||
181 | * @inheritDoc |
||
182 | */ |
||
183 | public function getFileNames() |
||
184 | { |
||
185 | return $this->files; |
||
186 | } |
||
187 | |||
188 | /** |
||
189 | * @inheritDoc |
||
190 | */ |
||
191 | public function isFileExists($fileName) |
||
192 | { |
||
193 | return $this->zip->hasEntry($fileName); |
||
194 | } |
||
195 | |||
196 | /** |
||
197 | * @inheritDoc |
||
198 | */ |
||
199 | public function getFileData($fileName) |
||
200 | { |
||
201 | $info = method_exists($this->zip, 'getEntryInfo') |
||
202 | ? $this->zip->getEntryInfo($fileName) |
||
203 | : $this->zip->getEntry($fileName); |
||
204 | |||
205 | return new ArchiveEntry( |
||
206 | $fileName, |
||
207 | $info->getCompressedSize(), |
||
208 | method_exists($info, 'getSize') ? $info->getSize() : $info->getUncompressedSize(), |
||
209 | $info->getMtime()->getTimestamp(), |
||
210 | null, |
||
211 | $info->getComment(), |
||
212 | $info->getCrc() |
||
213 | ); |
||
214 | } |
||
215 | |||
216 | /** |
||
217 | * @inheritDoc |
||
218 | */ |
||
219 | public function getFileContent($fileName) |
||
220 | { |
||
221 | return $this->zip->getEntryContents($fileName); |
||
222 | } |
||
223 | |||
224 | /** |
||
225 | * @inheritDoc |
||
226 | * @throws NonExistentArchiveFileException |
||
227 | */ |
||
228 | public function getFileStream($fileName) |
||
229 | { |
||
230 | return static::wrapStringInStream($this->getFileContent($fileName)); |
||
231 | } |
||
232 | |||
233 | /** |
||
234 | * @inheritDoc |
||
235 | */ |
||
236 | public function extractFiles($outputFolder, array $files) |
||
237 | { |
||
238 | $this->zip->extractTo($outputFolder, $files); |
||
239 | return count($files); |
||
240 | } |
||
241 | |||
242 | /** |
||
243 | * @inheritDoc |
||
244 | */ |
||
245 | public function extractArchive($outputFolder) |
||
246 | { |
||
247 | $this->zip->extractTo($outputFolder); |
||
248 | return count($this->files); |
||
249 | } |
||
250 | |||
251 | /** |
||
252 | * @inheritDoc |
||
253 | * @throws \PhpZip\Exception\ZipException |
||
254 | */ |
||
255 | public function addFileFromString($inArchiveName, $content) |
||
256 | { |
||
257 | return $this->zip->addFromString($inArchiveName, $content); |
||
258 | } |
||
259 | |||
260 | public function getComment() |
||
261 | { |
||
262 | return $this->zip->getArchiveComment(); |
||
263 | } |
||
264 | |||
265 | public function setComment($comment) |
||
268 | } |
||
269 | |||
270 | public function deleteFiles(array $files) |
||
271 | { |
||
272 | $deleted = 0; |
||
273 | foreach ($files as $file) { |
||
274 | $this->zip->deleteFromName($file); |
||
275 | $deleted++; |
||
276 | } |
||
277 | return $deleted; |
||
278 | } |
||
279 | |||
280 | public function addFiles(array $files) |
||
285 | } |
||
286 | } |
||
287 | } |
||
288 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths