ListCommand::execute()   C
last analyzed

Complexity

Conditions 14
Paths 6

Size

Total Lines 56
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 14
eloc 40
c 1
b 0
f 0
nc 6
nop 2
dl 0
loc 56
rs 6.2666

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\Input\InputArgument;
6
use Symfony\Component\Console\Input\InputInterface;
7
use Symfony\Component\Console\Input\InputOption;
8
use Symfony\Component\Console\Output\OutputInterface;
9
10
class ListCommand extends BaseArchiveCommand
11
{
12
    protected static $defaultName = 'files:list';
13
14
    protected function configure()
15
    {
16
        parent::configure();
17
        $this
18
            ->setDescription('Lists archive entries')
19
            ->setHelp('Lists archive entries.')
20
            ->addArgument('filter', InputArgument::OPTIONAL, 'Files filter (as for fnmatch). If no * passed in filter, it will be added at the end of filter')
21
            ->addOption('longFormat', 'l', InputOption::VALUE_NONE, 'Use long format')
22
            ->addOption('human-readable-size', null, InputOption::VALUE_NONE, 'Use human-readable size')
23
        ;
24
    }
25
26
    public function execute(InputInterface $input, OutputInterface $output)
27
    {
28
        $archive = $this->getArchive($input, $output);
29
        $filter = $input->getArgument('filter');
30
        $long_format = $input->getOption('longFormat');
31
        $human_readable_size = $input->getOption('human-readable-size');
32
33
        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

33
        if (!empty($filter) && strpos(/** @scrutinizer ignore-type */ $filter, '*') === false) {
Loading history...
34
            $filter .= '*';
35
        }
36
37
        if ($long_format) {
38
            $uncomp_size_length = $comp_size_length = 0;
39
            foreach ($archive->getFiles($filter) as $file) {
0 ignored issues
show
Bug introduced by
It seems like $filter can also be of type string[]; however, parameter $filter of wapmorgan\UnifiedArchive...fiedArchive::getFiles() 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

39
            foreach ($archive->getFiles(/** @scrutinizer ignore-type */ $filter) as $file) {
Loading history...
40
                $details = $archive->getFileData($file);
41
42
                if ($human_readable_size) {
43
                    $un_comp_size = implode($this->formatSize($details->uncompressedSize));
44
                    $comp_size = implode($this->formatSize($details->compressedSize));
45
                } else {
46
                    $un_comp_size = $details->uncompressedSize;
47
                    $comp_size = $details->compressedSize;
48
                }
49
50
                $len = strlen($un_comp_size);
51
                if ($len > $uncomp_size_length) {
52
                    $uncomp_size_length = $len;
53
                }
54
                $len = strlen($comp_size);
55
                if ($len > $comp_size_length) {
56
                    $comp_size_length = $len;
57
                }
58
            }
59
60
            foreach ($archive->getFiles($filter) as $file) {
61
                $details = $archive->getFileData($file);
62
                $output->writeln(($details->isCompressed && $details->uncompressedSize > 0 ? 'x' : '-')
63
                                 . ' ' . str_pad(
64
                                     $human_readable_size ? implode($this->formatSize($details->uncompressedSize)) : $details->uncompressedSize,
65
                                     $uncomp_size_length,
66
                                     ' ',
67
                                     STR_PAD_LEFT)
68
                                 . ' ' . str_pad(
69
                                     $human_readable_size ? implode($this->formatSize($details->compressedSize)) : $details->compressedSize,
70
                                     $comp_size_length,
71
                                     ' ',
72
                                     STR_PAD_LEFT)
73
                                 . ' ' . $this->formatDateShort($details->modificationTime) . ' ' . $details->path);
74
            }
75
        } else {
76
            foreach ($archive->getFiles($filter) as $file) {
77
                $output->writeln($file);
78
            }
79
        }
80
81
        return 0;
82
    }
83
84
    protected function formatDateShort($timestamp)
85
    {
86
        static $current_year;
87
        if ($current_year === null) {
88
            $current_year = strtotime('1 january');
89
        }
90
        if ($timestamp < $current_year) {
91
            return strtolower(date('M d  o', $timestamp));
92
        } else {
93
            return strtolower(date('M d H:i', $timestamp));
94
        }
95
    }
96
}
97