| Total Complexity | 40 |
| Total Lines | 399 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Info 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 Info, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 47 | final class Info extends Command |
||
| 48 | { |
||
| 49 | private const PHAR_ARG = 'phar'; |
||
| 50 | private const LIST_OPT = 'list'; |
||
| 51 | private const METADATA_OPT = 'metadata'; |
||
| 52 | private const MODE_OPT = 'mode'; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * The list of recognized compression algorithms. |
||
| 56 | * |
||
| 57 | * @var array |
||
| 58 | */ |
||
| 59 | private const ALGORITHMS = [ |
||
| 60 | Phar::BZ2 => 'BZ2', |
||
| 61 | Phar::GZ => 'GZ', |
||
| 62 | 'NONE' => 'None', |
||
| 63 | ]; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * The list of recognized file compression algorithms. |
||
| 67 | * |
||
| 68 | * @var array |
||
| 69 | */ |
||
| 70 | private const FILE_ALGORITHMS = [ |
||
| 71 | Phar::BZ2 => 'BZ2', |
||
| 72 | Phar::GZ => 'GZ', |
||
| 73 | ]; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * {@inheritdoc} |
||
| 77 | */ |
||
| 78 | protected function configure(): void |
||
| 79 | { |
||
| 80 | $this->setName('info'); |
||
| 81 | $this->setDescription( |
||
| 82 | 'Displays information about the PHAR extension or file' |
||
| 83 | ); |
||
| 84 | $this->setHelp( |
||
| 85 | <<<'HELP' |
||
| 86 | The <info>%command.name%</info> command will display information about the Phar extension, |
||
| 87 | or the Phar file if specified. |
||
| 88 | |||
| 89 | If the <info>phar</info> argument <comment>(the PHAR file path)</comment> is provided, information |
||
| 90 | about the PHAR file itself will be displayed. |
||
| 91 | |||
| 92 | If the <info>--list|-l</info> option is used, the contents of the PHAR file will |
||
| 93 | be listed. By default, the list is shown as an indented tree. You may |
||
| 94 | instead choose to view a flat listing, by setting the <info>--mode|-m</info> option |
||
| 95 | to <comment>flat</comment>. |
||
| 96 | HELP |
||
| 97 | ); |
||
| 98 | $this->addArgument( |
||
| 99 | self::PHAR_ARG, |
||
| 100 | InputArgument::OPTIONAL, |
||
| 101 | 'The Phar file.' |
||
| 102 | ); |
||
| 103 | $this->addOption( |
||
| 104 | self::LIST_OPT, |
||
| 105 | 'l', |
||
| 106 | InputOption::VALUE_NONE, |
||
| 107 | 'List the contents of the Phar?' |
||
| 108 | ); |
||
| 109 | $this->addOption( |
||
| 110 | self::METADATA_OPT, |
||
| 111 | null, |
||
| 112 | InputOption::VALUE_NONE, |
||
| 113 | 'Display metadata?' |
||
| 114 | ); |
||
| 115 | $this->addOption( |
||
| 116 | self::MODE_OPT, |
||
| 117 | 'm', |
||
| 118 | InputOption::VALUE_OPTIONAL, |
||
| 119 | 'The listing mode. (default: indent, options: indent, flat)', |
||
| 120 | 'indent' |
||
| 121 | ); |
||
| 122 | } |
||
| 123 | |||
| 124 | /** |
||
| 125 | * @override |
||
| 126 | */ |
||
| 127 | public function execute(InputInterface $input, OutputInterface $output): int |
||
| 128 | { |
||
| 129 | $io = new SymfonyStyle($input, $output); |
||
| 130 | $io->writeln(''); |
||
| 131 | |||
| 132 | if (null === ($file = $input->getArgument(self::PHAR_ARG))) { |
||
| 133 | return $this->showGlobalInfo($output, $io); |
||
| 134 | } |
||
| 135 | |||
| 136 | $file = realpath($file); |
||
| 137 | |||
| 138 | if (false === $file) { |
||
| 139 | $io->error( |
||
| 140 | sprintf( |
||
| 141 | 'The file "%s" could not be found.', |
||
| 142 | $input->getArgument(self::PHAR_ARG) |
||
| 143 | ) |
||
| 144 | ); |
||
| 145 | |||
| 146 | return 1; |
||
| 147 | } |
||
| 148 | |||
| 149 | if ('' === pathinfo($file, PATHINFO_EXTENSION)) { |
||
| 150 | // It is likely to be a PHAR without extension |
||
| 151 | copy($file, $tmpFile = sys_get_temp_dir().'/'.(new DateTimeImmutable())->getTimestamp().$file.'.phar'); |
||
| 152 | |||
| 153 | try { |
||
| 154 | return $this->showInfo($tmpFile, $file, $input, $output, $io); |
||
| 155 | } finally { |
||
| 156 | remove($tmpFile); |
||
| 157 | } |
||
| 158 | } |
||
| 159 | |||
| 160 | return $this->showInfo($file, $file, $input, $output, $io); |
||
| 161 | } |
||
| 162 | |||
| 163 | public function showInfo(string $file, string $originalFile, InputInterface $input, OutputInterface $output, SymfonyStyle $io): int |
||
| 164 | { |
||
| 165 | try { |
||
| 166 | try { |
||
| 167 | $phar = new Phar($file); |
||
| 168 | } catch (UnexpectedValueException $exception) { |
||
| 169 | $phar = new PharData($file); |
||
| 170 | } |
||
| 171 | |||
| 172 | return $this->showPharInfo( |
||
| 173 | $phar, |
||
| 174 | $input->getOption(self::LIST_OPT), |
||
| 175 | 'indent' === $input->getOption(self::MODE_OPT), |
||
| 176 | $output, |
||
| 177 | $io |
||
| 178 | ); |
||
| 179 | } catch (Throwable $throwable) { |
||
| 180 | $io->error( |
||
| 181 | sprintf( |
||
| 182 | 'Could not read the file "%s".', |
||
| 183 | $originalFile |
||
| 184 | ) |
||
| 185 | ); |
||
| 186 | |||
| 187 | return 1; |
||
| 188 | } |
||
| 189 | } |
||
| 190 | |||
| 191 | private function showGlobalInfo(OutputInterface $output, SymfonyStyle $io): int |
||
| 206 | } |
||
| 207 | |||
| 208 | /** |
||
| 209 | * @param Phar|PharData $phar |
||
| 210 | */ |
||
| 211 | private function showPharInfo($phar, bool $content, bool $indent, OutputInterface $output, SymfonyStyle $io): int |
||
| 212 | { |
||
| 213 | $signature = $phar->getSignature(); |
||
| 214 | |||
| 215 | $this->showPharGlobalInfo($phar, $io, $signature); |
||
| 216 | |||
| 217 | if ($content) { |
||
| 218 | $root = 'phar://'.str_replace('\\', '/', realpath($phar->getPath())).'/'; |
||
| 219 | |||
| 220 | $this->renderContents( |
||
| 221 | $output, |
||
| 222 | $phar, |
||
|
|
|||
| 223 | $indent ? 0 : false, |
||
| 224 | $root, |
||
| 225 | $phar, |
||
| 226 | $root |
||
| 227 | ); |
||
| 228 | } else { |
||
| 229 | $io->comment('Use the <info>--list|-l</info> option to list the content of the PHAR.'); |
||
| 230 | } |
||
| 231 | |||
| 232 | return 0; |
||
| 233 | } |
||
| 234 | |||
| 235 | /** |
||
| 236 | * @param Phar|PharData $phar |
||
| 237 | * @param mixed $signature |
||
| 238 | */ |
||
| 239 | private function showPharGlobalInfo($phar, SymfonyStyle $io, $signature): void |
||
| 240 | { |
||
| 241 | $io->writeln( |
||
| 242 | sprintf( |
||
| 243 | '<comment>API Version:</comment> %s', |
||
| 244 | '' !== $phar->getVersion() ? $phar->getVersion() : 'No information found' |
||
| 245 | ) |
||
| 246 | ); |
||
| 247 | $io->writeln(''); |
||
| 248 | |||
| 249 | $count = array_filter($this->retrieveCompressionCount($phar)); |
||
| 250 | $totalCount = array_sum($count); |
||
| 251 | |||
| 252 | if (1 === count($count)) { |
||
| 253 | $io->writeln( |
||
| 254 | sprintf( |
||
| 255 | '<comment>Archive Compression:</comment> %s', |
||
| 256 | key($count) |
||
| 257 | ) |
||
| 258 | ); |
||
| 259 | } else { |
||
| 260 | $io->writeln('<comment>Archive Compression:</comment>'); |
||
| 261 | |||
| 262 | end($count); |
||
| 263 | $lastAlgorithmName = key($count); |
||
| 264 | |||
| 265 | $totalPercentage = 100; |
||
| 266 | |||
| 267 | foreach ($count as $algorithmName => $nbrOfFiles) { |
||
| 268 | if ($lastAlgorithmName === $algorithmName) { |
||
| 269 | $percentage = $totalPercentage; |
||
| 270 | } else { |
||
| 271 | $percentage = $nbrOfFiles * 100 / $totalCount; |
||
| 272 | |||
| 273 | $totalPercentage -= $percentage; |
||
| 274 | } |
||
| 275 | |||
| 276 | $io->writeln( |
||
| 277 | sprintf( |
||
| 278 | ' - %s (%0.2f%%)', |
||
| 279 | $algorithmName, |
||
| 280 | $percentage |
||
| 281 | ) |
||
| 282 | ); |
||
| 283 | } |
||
| 284 | } |
||
| 285 | $io->writeln(''); |
||
| 286 | |||
| 287 | if (false !== $signature) { |
||
| 288 | $io->writeln( |
||
| 289 | sprintf( |
||
| 290 | '<comment>Signature:</comment> %s', |
||
| 291 | $signature['hash_type'] |
||
| 292 | ) |
||
| 293 | ); |
||
| 294 | $io->writeln( |
||
| 295 | sprintf( |
||
| 296 | '<comment>Signature Hash:</comment> %s', |
||
| 297 | $signature['hash'] |
||
| 298 | ) |
||
| 299 | ); |
||
| 300 | $io->writeln(''); |
||
| 301 | } |
||
| 302 | |||
| 303 | $metadata = var_export($phar->getMetadata(), true); |
||
| 304 | |||
| 305 | if ('NULL' === $metadata) { |
||
| 306 | $io->writeln('<comment>Metadata:</comment> None'); |
||
| 307 | } else { |
||
| 308 | $io->writeln('<comment>Metadata:</comment>'); |
||
| 309 | $io->writeln($metadata); |
||
| 310 | } |
||
| 311 | $io->writeln(''); |
||
| 312 | |||
| 313 | $io->writeln( |
||
| 314 | sprintf( |
||
| 315 | '<comment>Contents:</comment>%s (%s)', |
||
| 316 | 1 === $totalCount ? ' 1 file' : " $totalCount files", |
||
| 317 | formatted_filesize($phar->getPath()) |
||
| 318 | ) |
||
| 319 | ); |
||
| 320 | } |
||
| 321 | |||
| 322 | private function render(OutputInterface $output, array $attributes): void |
||
| 323 | { |
||
| 324 | $out = false; |
||
| 325 | |||
| 326 | foreach ($attributes as $name => $value) { |
||
| 327 | if ($out) { |
||
| 328 | $output->writeln(''); |
||
| 329 | } |
||
| 330 | |||
| 331 | $output->write("<comment>$name:</comment>"); |
||
| 332 | |||
| 333 | if (is_array($value)) { |
||
| 334 | $output->writeln(''); |
||
| 335 | |||
| 336 | foreach ($value as $v) { |
||
| 337 | $output->writeln(" - $v"); |
||
| 338 | } |
||
| 339 | } else { |
||
| 340 | $output->writeln(" $value"); |
||
| 341 | } |
||
| 342 | |||
| 343 | $out = true; |
||
| 344 | } |
||
| 345 | } |
||
| 346 | |||
| 347 | /** |
||
| 348 | * @param OutputInterface $output |
||
| 349 | * @param iterable|PharFileInfo[] $list |
||
| 350 | * @param bool|int $indent Nbr of indent or `false` |
||
| 351 | * @param string $base |
||
| 352 | * @param Phar $phar |
||
| 353 | * @param string $root |
||
| 354 | */ |
||
| 355 | private function renderContents( |
||
| 356 | OutputInterface $output, |
||
| 357 | iterable $list, |
||
| 358 | $indent, |
||
| 359 | string $base, |
||
| 360 | Phar $phar, |
||
| 361 | string $root |
||
| 362 | ): void { |
||
| 363 | foreach ($list as $item) { |
||
| 364 | $item = $phar[str_replace($root, '', $item->getPathname())]; |
||
| 365 | |||
| 366 | if (false !== $indent) { |
||
| 367 | $output->write(str_repeat(' ', $indent)); |
||
| 368 | |||
| 369 | $path = $item->getFilename(); |
||
| 370 | |||
| 371 | if ($item->isDir()) { |
||
| 372 | $path .= '/'; |
||
| 373 | } |
||
| 374 | } else { |
||
| 375 | $path = str_replace($base, '', $item->getPathname()); |
||
| 376 | } |
||
| 377 | |||
| 378 | if ($item->isDir()) { |
||
| 379 | if (false !== $indent) { |
||
| 380 | $output->writeln("<info>$path</info>"); |
||
| 381 | } |
||
| 382 | } else { |
||
| 383 | $compression = ' <fg=red>[NONE]</fg=red>'; |
||
| 384 | |||
| 385 | foreach (self::FILE_ALGORITHMS as $code => $name) { |
||
| 386 | if ($item->isCompressed($code)) { |
||
| 387 | $compression = " <fg=cyan>[$name]</fg=cyan>"; |
||
| 388 | break; |
||
| 389 | } |
||
| 390 | } |
||
| 391 | |||
| 392 | $output->writeln($path.$compression); |
||
| 393 | } |
||
| 394 | |||
| 395 | if ($item->isDir()) { |
||
| 396 | $this->renderContents( |
||
| 397 | $output, |
||
| 398 | new DirectoryIterator($item->getPathname()), |
||
| 399 | (false === $indent) ? $indent : $indent + 2, |
||
| 400 | $base, |
||
| 401 | $phar, |
||
| 402 | $root |
||
| 403 | ); |
||
| 404 | } |
||
| 405 | } |
||
| 406 | } |
||
| 407 | |||
| 408 | /** |
||
| 409 | * @param Phar|PharData $phar |
||
| 410 | */ |
||
| 411 | private function retrieveCompressionCount($phar): array |
||
| 446 | ); |
||
| 447 | } |
||
| 448 | } |
||
| 449 |