Passed
Push — master ( bf29cf...6612ae )
by f
15:14
created

FormatsCommand::execute()   C

Complexity

Conditions 16
Paths 46

Size

Total Lines 64
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 16
eloc 40
nc 46
nop 2
dl 0
loc 64
rs 5.5666
c 1
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace wapmorgan\UnifiedArchive\Commands;
4
5
use Symfony\Component\Console\Helper\Table;
6
use Symfony\Component\Console\Helper\TableCell;
7
use Symfony\Component\Console\Input\InputArgument;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Output\OutputInterface;
10
use wapmorgan\UnifiedArchive\Drivers\BasicDriver;
11
use wapmorgan\UnifiedArchive\Formats;
12
13
class FormatsCommand extends BaseCommand
14
{
15
    protected static $defaultName = 'system:formats';
16
17
    protected function configure()
18
    {
19
        $this
20
            ->setDescription('Lists supported archive formats with current system configuration')
21
            ->setHelp('Lists supported archive formats with current system configuration. You can check supported formats and supported actions with them.')
22
            ->addArgument('driver', InputArgument::OPTIONAL, 'Filter formats support by specific driver')
23
            ->addUsage('\'wapmorgan\UnifiedArchive\Drivers\SevenZip\'')
24
        ;
25
    }
26
27
    public function execute(InputInterface $input, OutputInterface $output)
28
    {
29
        $table = new Table($output);
30
31
        /** @var string|BasicDriver $driver */
32
        $driver = $input->getArgument('driver');
33
34
        if ($driver !== null) {
0 ignored issues
show
introduced by
The condition $driver !== null is always true.
Loading history...
35
            if (strpos($driver, '\\') === false) {
36
                if (class_exists('\\wapmorgan\\UnifiedArchive\\Drivers\\' . $driver)) {
37
                    $driver = '\\wapmorgan\\UnifiedArchive\\Drivers\\' . $driver;
38
                } else if (class_exists('\\wapmorgan\\UnifiedArchive\\Drivers\\OneFile\\' . $driver)) {
39
                    $driver = '\\wapmorgan\\UnifiedArchive\\Drivers\\OneFile\\' . $driver;
40
                }
41
            }
42
            if ($driver[0] !== '\\') {
43
                $driver = '\\'.$driver;
44
            }
45
            if (!class_exists($driver) || !is_a($driver, BasicDriver::class, true)) {
46
                throw new \InvalidArgumentException('Class "' . $driver . '" not found or not in BasicDriver children');
47
            }
48
            $output->writeln('Supported formats by <info>' . $driver . '</info>');
49
50
            $table->setHeaders(['format', ...array_keys(self::$abilitiesLabels)]);
51
            foreach ($driver::getSupportedFormats() as $i => $format) {
52
                $abilities = $driver::checkFormatSupport($format);
53
                $row = [$format];
54
55
                foreach (self::$abilitiesLabels as $possibleAbility) {
56
                    $row[] = in_array($possibleAbility, $abilities, true) ? '+' : '';
57
                }
58
59
                $table->setRow($i, $row);
60
            }
61
            $table->render();
62
            return 0;
63
        }
64
65
        $headers = array_map(function ($v) { return substr($v, strrpos($v, '\\') + 1);}, Formats::$drivers);
66
        array_unshift($headers, 'format');
67
68
        $table->setHeaders($headers);
69
        $i = 0;
70
        foreach (Formats::getSupportedDriverFormats() as $format => $formatSupportStatus) {
71
            $row = [$format];
72
            foreach (Formats::$drivers as $driverClass) {
73
                if (isset($formatSupportStatus[$driverClass])) {
74
                    $shortcuts = null;
75
                    foreach (self::$abilitiesShortCuts as $ability => $abilitiesShortCut) {
76
                        if (in_array($ability, $formatSupportStatus[$driverClass], true)) {
77
                            $shortcuts .= $abilitiesShortCut;
78
                        }
79
                    }
80
                    $row[] = $shortcuts;
81
                } else {
82
                    $row[] = '';
83
                }
84
            }
85
86
            $table->setRow($i++, $row);
87
        }
88
        $table->render();
89
90
        return 0;
91
    }
92
}
93