Total Complexity | 44 |
Total Lines | 444 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like Box 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 Box, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
50 | final class Box implements Countable |
||
51 | { |
||
52 | public const DEBUG_DIR = '.box_dump'; |
||
53 | |||
54 | /** |
||
55 | * @var Compactor[] |
||
56 | */ |
||
57 | private $compactors = []; |
||
58 | |||
59 | /** |
||
60 | * @var string The path to the PHAR file |
||
61 | */ |
||
62 | private $file; |
||
63 | |||
64 | /** |
||
65 | * @var Phar The PHAR instance |
||
66 | */ |
||
67 | private $phar; |
||
68 | |||
69 | /** |
||
70 | * @var scalar[] The placeholders with their values |
||
71 | */ |
||
72 | private $placeholders = []; |
||
73 | |||
74 | /** |
||
75 | * @var string |
||
76 | */ |
||
77 | private $basePath; |
||
78 | |||
79 | /** |
||
80 | * @var Closure|MapFile |
||
81 | */ |
||
82 | private $mapFile; |
||
83 | |||
84 | /** |
||
85 | * @var Scoper |
||
86 | */ |
||
87 | private $scoper; |
||
88 | |||
89 | private $buffering = false; |
||
90 | |||
91 | private $bufferedFiles = []; |
||
92 | |||
93 | private function __construct(Phar $phar, string $file) |
||
94 | { |
||
95 | $this->phar = $phar; |
||
96 | $this->file = $file; |
||
97 | |||
98 | $this->basePath = getcwd(); |
||
99 | $this->mapFile = function (): void { }; |
||
100 | $this->scoper = new NullScoper(); |
||
101 | } |
||
102 | |||
103 | /** |
||
104 | * Creates a new PHAR and Box instance. |
||
105 | * |
||
106 | * @param string $file The PHAR file name |
||
107 | * @param int $flags Flags to pass to the Phar parent class RecursiveDirectoryIterator |
||
108 | * @param string $alias Alias with which the Phar archive should be referred to in calls to stream functionality |
||
109 | * |
||
110 | * @return Box |
||
111 | * |
||
112 | * @see RecursiveDirectoryIterator |
||
113 | */ |
||
114 | public static function create(string $file, int $flags = null, string $alias = null): self |
||
121 | } |
||
122 | |||
123 | public function startBuffering(): void |
||
130 | } |
||
131 | |||
132 | public function endBuffering(bool $dumpAutoload): void |
||
167 | } |
||
168 | |||
169 | public function removeComposerArtefacts(string $vendorDir): void |
||
170 | { |
||
171 | Assertion::false($this->buffering, 'The buffering must have ended before removing the Composer artefacts'); |
||
172 | |||
173 | $composerFiles = [ |
||
174 | 'composer.json', |
||
175 | 'composer.lock', |
||
176 | $vendorDir.'/composer/installed.json', |
||
177 | ]; |
||
178 | |||
179 | $this->phar->startBuffering(); |
||
180 | |||
181 | foreach ($composerFiles as $composerFile) { |
||
182 | // TODO: the file map could return the unchanged path when no mapping is found... |
||
183 | $localComposerFile = ($this->mapFile)($composerFile); |
||
184 | |||
185 | if (null === $localComposerFile) { |
||
186 | $localComposerFile = $composerFile; |
||
187 | } |
||
188 | |||
189 | if (file_exists('phar://'.$this->phar->getPath().'/'.$localComposerFile)) { |
||
190 | $this->phar->delete($localComposerFile); |
||
191 | } |
||
192 | } |
||
193 | |||
194 | $this->phar->stopBuffering(); |
||
195 | } |
||
196 | |||
197 | /** |
||
198 | * @return null|string The required extension to execute the PHAR now that it is compressed |
||
199 | */ |
||
200 | public function compress(int $compressionAlgorithm): ?string |
||
201 | { |
||
202 | Assertion::false($this->buffering, 'Cannot compress files while buffering.'); |
||
203 | Assertion::inArray($compressionAlgorithm, get_phar_compression_algorithms()); |
||
204 | |||
205 | $extensionRequired = get_phar_compression_algorithm_extension($compressionAlgorithm); |
||
206 | |||
207 | if (null !== $extensionRequired && false === extension_loaded($extensionRequired)) { |
||
208 | throw new RuntimeException( |
||
209 | sprintf( |
||
210 | 'Cannot compress the PHAR with the compression algorithm "%s": the extension "%s" is required but appear to not ' |
||
211 | .'be loaded', |
||
212 | array_flip(get_phar_compression_algorithms())[$compressionAlgorithm], |
||
213 | $extensionRequired |
||
214 | ) |
||
215 | ); |
||
216 | } |
||
217 | |||
218 | try { |
||
219 | if (Phar::NONE === $compressionAlgorithm) { |
||
220 | $this->getPhar()->decompressFiles(); |
||
221 | } else { |
||
222 | $this->phar->compressFiles($compressionAlgorithm); |
||
223 | } |
||
224 | } catch (BadMethodCallException $exception) { |
||
225 | $exceptionMessage = 'unable to create temporary file' !== $exception->getMessage() |
||
226 | ? 'Could not compress the PHAR: '.$exception->getMessage() |
||
227 | : sprintf( |
||
228 | 'Could not compress the PHAR: the compression requires too many file descriptors to be opened (%s). Check ' |
||
229 | .'your system limits or install the posix extension to allow Box to automatically configure it during the compression', |
||
230 | $this->phar->count() |
||
231 | ) |
||
232 | ; |
||
233 | |||
234 | throw new RuntimeException($exceptionMessage, $exception->getCode(), $exception); |
||
235 | } |
||
236 | |||
237 | return $extensionRequired; |
||
238 | } |
||
239 | |||
240 | /** |
||
241 | * @param Compactor[] $compactors |
||
242 | */ |
||
243 | public function registerCompactors(array $compactors): void |
||
244 | { |
||
245 | Assertion::allIsInstanceOf($compactors, Compactor::class); |
||
246 | |||
247 | $this->compactors = $compactors; |
||
248 | |||
249 | foreach ($this->compactors as $compactor) { |
||
250 | if ($compactor instanceof PhpScoper) { |
||
251 | $this->scoper = $compactor->getScoper(); |
||
252 | |||
253 | break; |
||
254 | } |
||
255 | } |
||
256 | } |
||
257 | |||
258 | /** |
||
259 | * @param scalar[] $placeholders |
||
260 | */ |
||
261 | public function registerPlaceholders(array $placeholders): void |
||
262 | { |
||
263 | $message = 'Expected value "%s" to be a scalar or stringable object.'; |
||
264 | |||
265 | foreach ($placeholders as $i => $placeholder) { |
||
266 | if (is_object($placeholder)) { |
||
267 | Assertion::methodExists('__toString', $placeholder, $message); |
||
268 | |||
269 | $placeholders[$i] = (string) $placeholder; |
||
270 | |||
271 | break; |
||
272 | } |
||
273 | |||
274 | Assertion::scalar($placeholder, $message); |
||
275 | } |
||
276 | |||
277 | $this->placeholders = $placeholders; |
||
278 | } |
||
279 | |||
280 | public function registerFileMapping(string $basePath, MapFile $fileMapper): void |
||
281 | { |
||
282 | $this->basePath = $basePath; |
||
283 | $this->mapFile = $fileMapper; |
||
284 | } |
||
285 | |||
286 | public function registerStub(string $file): void |
||
287 | { |
||
288 | $contents = self::replacePlaceholders( |
||
289 | $this->placeholders, |
||
290 | file_contents($file) |
||
291 | ); |
||
292 | |||
293 | $this->phar->setStub($contents); |
||
294 | } |
||
295 | |||
296 | /** |
||
297 | * @param SplFileInfo[]|string[] $files |
||
298 | */ |
||
299 | public function addFiles(array $files, bool $binary): void |
||
300 | { |
||
301 | Assertion::true($this->buffering, 'Cannot add files if the buffering has not started.'); |
||
302 | |||
303 | $files = array_map( |
||
304 | function ($file): string { |
||
305 | // Convert files to string as SplFileInfo is not serializable |
||
306 | return (string) $file; |
||
307 | }, |
||
308 | $files |
||
309 | ); |
||
310 | |||
311 | if ($binary) { |
||
312 | foreach ($files as $file) { |
||
313 | $this->addFile($file, null, $binary); |
||
314 | } |
||
315 | |||
316 | return; |
||
317 | } |
||
318 | |||
319 | $filesWithContents = $this->processContents($files); |
||
320 | |||
321 | foreach ($filesWithContents as $fileWithContents) { |
||
322 | [$file, $contents] = $fileWithContents; |
||
323 | |||
324 | $this->bufferedFiles[$file] = $contents; |
||
325 | } |
||
326 | } |
||
327 | |||
328 | /** |
||
329 | * Adds the a file to the PHAR. The contents will first be compacted and have its placeholders |
||
330 | * replaced. |
||
331 | * |
||
332 | * @param string $file |
||
333 | * @param null|string $contents If null the content of the file will be used |
||
334 | * @param bool $binary When true means the file content shouldn't be processed |
||
335 | * |
||
336 | * @return string File local path |
||
337 | */ |
||
338 | public function addFile(string $file, string $contents = null, bool $binary = false): string |
||
366 | } |
||
367 | |||
368 | public function getPhar(): Phar |
||
369 | { |
||
370 | return $this->phar; |
||
371 | } |
||
372 | |||
373 | /** |
||
374 | * Signs the PHAR using a private key file. |
||
375 | * |
||
376 | * @param string $file the private key file name |
||
377 | * @param string $password the private key password |
||
378 | */ |
||
379 | public function signUsingFile(string $file, string $password = null): void |
||
380 | { |
||
381 | $this->sign(file_contents($file), $password); |
||
382 | } |
||
383 | |||
384 | /** |
||
385 | * Signs the PHAR using a private key. |
||
386 | * |
||
387 | * @param string $key The private key |
||
388 | * @param string $password The private key password |
||
389 | */ |
||
390 | public function sign(string $key, ?string $password): void |
||
391 | { |
||
392 | $pubKey = $this->file.'.pubkey'; |
||
393 | |||
394 | Assertion::writeable(dirname($pubKey)); |
||
395 | Assertion::extensionLoaded('openssl'); |
||
396 | |||
397 | if (file_exists($pubKey)) { |
||
398 | Assertion::file( |
||
399 | $pubKey, |
||
400 | 'Cannot create public key: "%s" already exists and is not a file.' |
||
401 | ); |
||
402 | } |
||
403 | |||
404 | $resource = openssl_pkey_get_private($key, (string) $password); |
||
405 | |||
406 | openssl_pkey_export($resource, $private); |
||
407 | |||
408 | $details = openssl_pkey_get_details($resource); |
||
|
|||
409 | |||
410 | $this->phar->setSignatureAlgorithm(Phar::OPENSSL, $private); |
||
411 | |||
412 | dump_file($pubKey, $details['key']); |
||
413 | } |
||
414 | |||
415 | /** |
||
416 | * @param string[] $files |
||
417 | * |
||
418 | * @return array array of tuples where the first element is the local file path (path inside the PHAR) and the |
||
419 | * second element is the processed contents |
||
420 | */ |
||
421 | private function processContents(array $files): array |
||
455 | ; |
||
456 | } |
||
457 | |||
458 | /** |
||
459 | * Replaces the placeholders with their values. |
||
460 | * |
||
461 | * @param array $placeholders |
||
462 | * @param string $contents the contents |
||
463 | * |
||
464 | * @return string the replaced contents |
||
465 | */ |
||
466 | private static function replacePlaceholders(array $placeholders, string $contents): string |
||
472 | ); |
||
473 | } |
||
474 | |||
475 | private static function compactContents(array $compactors, string $file, string $contents): string |
||
483 | ); |
||
484 | } |
||
485 | |||
486 | /** |
||
487 | * {@inheritdoc} |
||
488 | */ |
||
489 | public function count(): int |
||
496 |