Issues (155)

src/Commands/BaseArchiveCommand.php (3 issues)

Labels
Severity
1
<?php
2
3
namespace wapmorgan\UnifiedArchive\Commands;
4
5
use Symfony\Component\Console\Input\InputArgument;
6
use Symfony\Component\Console\Input\InputInterface;
7
use Symfony\Component\Console\Input\InputOption;
8
use Symfony\Component\Console\Output\OutputInterface;
9
use wapmorgan\UnifiedArchive\Abilities;
10
use wapmorgan\UnifiedArchive\Drivers\Basic\BasicDriver;
11
use wapmorgan\UnifiedArchive\Formats;
12
13
class BaseArchiveCommand extends BaseCommand
14
{
15
    protected function configure()
16
    {
17
        parent::configure();
18
        $this
19
            ->addArgument('archive', InputArgument::REQUIRED, 'Archive file')
20
            ->addOption('password', null, InputOption::VALUE_REQUIRED, 'Password for archive')
21
        ;
22
    }
23
24
    /**
25
     * @param InputInterface $input
26
     * @param OutputInterface $output
27
     * @return \wapmorgan\UnifiedArchive\UnifiedArchive
28
     * @throws \Exception
29
     */
30
    protected function getArchive(InputInterface $input, OutputInterface $output)
31
    {
32
        $file = realpath($input->getArgument('archive'));
0 ignored issues
show
It seems like $input->getArgument('archive') can also be of type null and string[]; however, parameter $path of realpath() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

32
        $file = realpath(/** @scrutinizer ignore-type */ $input->getArgument('archive'));
Loading history...
33
        $output->writeln('<comment>Opening ' . $file . '</comment>', OutputInterface::VERBOSITY_VERY_VERBOSE);
34
        if (!is_file($file)) {
35
            throw new \InvalidArgumentException('File ' . $input->getArgument('archive') . ' is not accessible');
0 ignored issues
show
Are you sure $input->getArgument('archive') of type null|string|string[] can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

35
            throw new \InvalidArgumentException('File ' . /** @scrutinizer ignore-type */ $input->getArgument('archive') . ' is not accessible');
Loading history...
36
        }
37
        $output->writeln('<comment>Format ' . Formats::detectArchiveFormat($file) . '</comment>', OutputInterface::VERBOSITY_VERY_VERBOSE);
38
        $password = $input->getOption('password');
39
        if (empty($password)) {
40
            $password = null;
41
        } else {
42
            $output->writeln('<comment>Passing password: ' . strlen($password).'</comment>', OutputInterface::VERBOSITY_VERY_VERBOSE);
43
        }
44
        $archive = $this->open($file, $password);
45
        $output->writeln('<comment>Driver ' . $archive->getDriverType() . '</comment>', OutputInterface::VERBOSITY_VERBOSE);
46
        $output->writeln('<comment>Driver abilities: ' . implode(', ', $this->getDriverFormatAbilities($archive->getDriverType(), $archive->getFormat())) . '</comment>', OutputInterface::VERBOSITY_VERBOSE);
0 ignored issues
show
$archive->getDriverType() of type string is incompatible with the type wapmorgan\UnifiedArchive\Drivers\Basic\BasicDriver expected by parameter $driver of wapmorgan\UnifiedArchive...DriverFormatAbilities(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

46
        $output->writeln('<comment>Driver abilities: ' . implode(', ', $this->getDriverFormatAbilities(/** @scrutinizer ignore-type */ $archive->getDriverType(), $archive->getFormat())) . '</comment>', OutputInterface::VERBOSITY_VERBOSE);
Loading history...
47
        return $archive;
48
    }
49
50
    /**
51
     * @param BasicDriver $driver
52
     * @param $format
53
     * @return array
54
     */
55
    protected function getDriverFormatAbilities($driver, $format)
56
    {
57
        $abilities = $driver::getFormatAbilities($format);
58
        return array_keys(array_intersect(Abilities::$abilitiesLabels, $abilities));
59
    }
60
}
61