Passed
Push — 1.1.x ( bc52f7...e8e75c )
by f
12:04
created

FoldersCommand::execute()   B

Complexity

Conditions 9
Paths 50

Size

Total Lines 60
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 9
eloc 36
c 1
b 0
f 0
nc 50
nop 2
dl 0
loc 60
rs 8.0555

How to fix   Long Method   

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\InputInterface;
7
use Symfony\Component\Console\Output\OutputInterface;
8
9
class FoldersCommand extends BaseArchiveCommand
10
{
11
    protected static $defaultName = 'files:folders';
12
13
    protected function configure()
14
    {
15
        parent::configure();
16
        $this
17
            ->setDescription('Accumulates folders list and their size')
18
            ->setHelp('Accumulates folders list and their size.')
19
        ;
20
    }
21
22
    public function execute(InputInterface $input, OutputInterface $output)
23
    {
24
        $archive = $this->getArchive($input, $output);
25
26
        $folders = [];
27
        foreach ($archive->getFileNames() as $file) {
28
            $file_folder = dirname($file);
29
            if (empty($file_folder)) {
30
                $file_folder = '/';
31
            }
32
33
            if (!isset($folders[$file_folder])) {
34
                $folders[$file_folder] = [
35
                    0, // total files number
36
                    0, // total uncompressed size
37
                    0, // total compressed size
38
                ];
39
            }
40
41
            $details = $archive->getFileData($file);
42
            $folders[$file_folder][0]++;
43
            $folders[$file_folder][1] = $details->uncompressedSize;
44
            $folders[$file_folder][2] = $details->compressedSize;
45
        }
46
47
        ksort($folders);
48
49
        // iterate again and add sub-dirs stats to parent dirs
50
        foreach ($folders as $folder => $folderStat) {
51
            $parent_folder = $folder;
52
            do {
53
                $parent_folder = dirname($parent_folder);
54
                if (in_array($parent_folder, ['.', '/'], true)) {
55
                    $parent_folder = null;
56
                }
57
58
                if (isset($folders[$parent_folder])) {
59
                    $folders[$parent_folder][0] += $folderStat[0];
60
                    $folders[$parent_folder][1] += $folderStat[1];
61
                    $folders[$parent_folder][2] += $folderStat[2];
62
                }
63
64
            } while (!empty($parent_folder));
65
        }
66
67
        $table = new Table($output);
68
        $table->setHeaders(['Folder', 'Files', 'Size', 'xSize']);
69
        $i = 0;
70
        foreach ($folders as $folder => $folderStat) {
71
            $table->setRow($i++, [
72
                $folder,
73
                $folderStat[0],
74
                implode($this->formatSize($folderStat[1])),
75
                implode($this->formatSize($folderStat[2])),
76
            ]);
77
        }
78
        $table->setStyle('compact');
79
        $table->render();
80
81
        return 0;
82
    }
83
}
84