Passed
Push — master ( f73d2a...b6f14a )
by f
14:52
created

TableCommand::execute()   C

Complexity

Conditions 12
Paths 108

Size

Total Lines 84
Code Lines 46

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 12
eloc 46
c 1
b 0
f 0
nc 108
nop 2
dl 0
loc 84
rs 6.9

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\Input\InputOption;
9
use Symfony\Component\Console\Output\OutputInterface;
10
11
class TableCommand extends BaseArchiveCommand
12
{
13
    protected static $defaultName = 'files:table';
14
15
    protected function configure()
16
    {
17
        parent::configure();
18
        $this
19
            ->setDescription('Lists archive entries as table')
20
            ->setHelp('Lists archive entries as table.')
21
            ->addArgument('filter', InputArgument::OPTIONAL, 'Files filter (as for fnmatch). If no * passed in filter, it will be added at the end of filter')
22
            ->addOption('human-readable-size', null, InputOption::VALUE_NONE, 'Use human-readable size')
23
            ->addOption('detect-mimetype', null, InputOption::VALUE_NONE, 'Detect mimetype for entries by its raw content')
24
            ->addOption('sort', 's', InputOption::VALUE_REQUIRED, 'Sort files in table by one of fields: filename, size, xsize, ratio, date, mimetype. By default is stored')
25
            ->addOption('sort-desc', null, InputOption::VALUE_NONE, 'Set sort order to desc. By default it is asc')
26
        ;
27
    }
28
    public function execute(InputInterface $input, OutputInterface $output)
29
    {
30
        $archive = $this->getArchive($input, $output);
31
        $filter = $input->getArgument('filter');
32
        $human_readable_size = $input->getOption('human-readable-size');
33
        $detect_mimetype = $input->getOption('detect-mimetype');
34
        $sort = $input->getOption('sort');
35
        $sort_desc = $input->getOption('sort-desc');
36
37
        $headers = ['Filename', 'Size', 'xSize', 'Ratio', 'Date'];
38
        if ($detect_mimetype) {
39
            $headers[] = 'Mimetype';
40
        }
41
42
        $sort_field = array_search($sort, array_map('strtolower', $headers));
43
44
        if (!empty($filter) && strpos($filter, '*') === false) {
0 ignored issues
show
Bug introduced by
It seems like $filter can also be of type string[]; however, parameter $haystack of strpos() 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

44
        if (!empty($filter) && strpos(/** @scrutinizer ignore-type */ $filter, '*') === false) {
Loading history...
45
            $filter .= '*';
46
        }
47
48
        $table = new Table($output);
49
        $table->setHeaders($headers);
50
        $uncomp_size_length = $comp_size_length = 0;
0 ignored issues
show
Unused Code introduced by
The assignment to $comp_size_length is dead and can be removed.
Loading history...
Unused Code introduced by
The assignment to $uncomp_size_length is dead and can be removed.
Loading history...
51
52
        $rows = [];
53
54
        foreach ($archive->getFileNames($filter) as $i => $file) {
0 ignored issues
show
Bug introduced by
It seems like $filter can also be of type string[]; however, parameter $filter of wapmorgan\UnifiedArchive...Archive::getFileNames() does only seem to accept null|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

54
        foreach ($archive->getFileNames(/** @scrutinizer ignore-type */ $filter) as $i => $file) {
Loading history...
55
            $details = $archive->getFileData($file);
56
57
            if ($human_readable_size) {
58
                $un_comp_size = implode($this->formatSize($details->uncompressedSize));
59
                $comp_size = implode($this->formatSize($details->compressedSize));
60
            } else {
61
                $un_comp_size = $details->uncompressedSize;
62
                $comp_size = $details->compressedSize;
63
            }
64
65
            $row = [
66
                $details->path,
67
                $un_comp_size,
68
                $comp_size,
69
                $details->uncompressedSize > 0
70
                    ? round($details->compressedSize / $details->uncompressedSize, 1)
71
                    : '-',
72
                $this->formatDate($details->modificationTime)];
73
            if ($detect_mimetype) {
74
                // @todo May be a bug in future. Need to review
75
                $row[] = $this->getMimeTypeByStream($archive->getFileStream($file));
76
            }
77
            $rows[] = $row;
78
//            $table->setRow($i, $row);
79
80
//            $len = strlen($un_comp_size);
81
//            if ($len > $uncomp_size_length) {
82
//                $uncomp_size_length = $len;
83
//            }
84
//            $len = strlen($comp_size);
85
//            if ($len > $comp_size_length) {
86
//                $comp_size_length = $len;
87
//            }
88
        }
89
90
        if ($sort !== null) {
91
            usort($rows, function (array $a, array $b) use ($sort_field) {
92
                if ($a[$sort_field] > $b[$sort_field]) {
93
                    return 1;
94
                }
95
                if ($a[$sort_field] < $b[$sort_field]) {
96
                    return -1;
97
                }
98
                return 0;
99
            });
100
            if ($sort_desc) {
101
                $rows = array_reverse($rows);
102
            }
103
        }
104
105
        $table->setRows($rows);
106
107
//        $table->setColumnWidth(0, 0);
108
//        $table->setColumnWidth(1, $uncomp_size_length);
109
//        $table->setColumnWidth(2, $comp_size_length);
110
//        $table->setColumnWidth(3, 18);
111
        $table->render();
112
113
    }
114
}
115