FormatsCommand::execute()   C
last analyzed

Complexity

Conditions 13
Paths 17

Size

Total Lines 71
Code Lines 45

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 45
c 1
b 0
f 0
dl 0
loc 71
rs 6.6166
cc 13
nc 17
nop 2

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\Input\InputArgument;
7
use Symfony\Component\Console\Input\InputInterface;
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 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
            $driver = $this->resolveDriverName($driver);
36
            if (!class_exists($driver) || !is_a($driver, BasicDriver::class, true)) {
37
                throw new \InvalidArgumentException('Class "' . $driver . '" not found or not in BasicDriver children');
38
            }
39
            $output->writeln('Supported formats by <info>' . $driver . '</info>');
40
41
            $headers = array_keys(Abilities::$abilitiesLabels);
42
            array_unshift($headers, 'format');
43
            $table->setHeaders($headers);
44
            foreach ($driver::getFormats() as $i => $format) {
45
                $abilities = $driver::getFormatAbilities($format);
46
                $row = [$format];
47
48
                foreach (Abilities::$abilitiesLabels as $possibleAbility) {
49
                    $row[] = in_array($possibleAbility, $abilities, true) ? '+' : '';
50
                }
51
52
                $table->setRow($i, $row);
53
            }
54
            $table->render();
55
            return 0;
56
        }
57
58
        $formats = Formats::getSupportedDriverFormats();
59
        $headers = array_keys($formats);
60
        array_unshift($headers, 'driver type');
61
        array_unshift($headers, 'driver / format');
62
        $table->setHeaders($headers);
63
        $rows = [];
64
65
        /** @var \wapmorgan\UnifiedArchive\Drivers\Basic\BasicDriver $driverClass */
66
        foreach (Formats::$drivers as $driverClass) {
67
            $row = [
68
                substr($driverClass, strrpos($driverClass, '\\') + 1),
69
                BasicDriver::$typeLabels[$driverClass::TYPE],
70
            ];
71
            foreach ($formats as $format => $formatSupportStatus) {
72
                if (isset($formatSupportStatus[$driverClass])) {
73
                    $shortcuts = null;
74
                    foreach (Abilities::$abilitiesShortCuts as $ability => $abilitiesShortCut) {
75
                        if (in_array($ability, $formatSupportStatus[$driverClass], true)) {
76
                            $shortcuts .= $abilitiesShortCut;
77
                        }
78
                    }
79
                    $row[] = $shortcuts;
80
                } else {
81
                    $row[] = '';
82
                }
83
            }
84
            $rows[] = $row;
85
        }
86
87
        $table->setRows($rows);
88
        //$table->setRow($i++, $row);
89
        $table->render();
90
91
        foreach (array_combine(array_values(Abilities::$abilitiesShortCuts), array_keys(
92
            Abilities::$abilitiesLabels
93
        )) as $shortCut => $label) {
94
            $output->writeln('<info>' . $shortCut . '</info> - ' . $label);
95
        }
96
97
        return 0;
98
    }
99
}
100