renderIndexObjectColumn()   B
last analyzed

Complexity

Conditions 10
Paths 13

Size

Total Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 31
rs 7.6666
c 0
b 0
f 0
cc 10
nc 13
nop 1

How to fix   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 Storeman\Cli\Helper;
4
5
use Storeman\FilesystemUtility;
6
use Storeman\Index\Comparison\IndexComparison;
7
use Storeman\Index\Comparison\IndexObjectComparison;
8
use Storeman\Index\IndexObject;
9
use Symfony\Component\Console\Helper\Helper;
10
use Symfony\Component\Console\Helper\HelperInterface;
11
use Symfony\Component\Console\Helper\Table;
12
use Symfony\Component\Console\Output\OutputInterface;
13
14
class DisplayIndexComparisonHelper extends Helper implements HelperInterface
15
{
16
    /**
17
     * {@inheritdoc}
18
     */
19
    public function getName(): string
20
    {
21
        return 'displayIndexComparison';
22
    }
23
24
    public function displayIndexComparison(IndexComparison $indexComparison, OutputInterface $output, string $titleA = 'IndexA', string $titleB = 'IndexB')
25
    {
26
        $table = new Table($output);
27
        $table->setStyle('compact');
28
        $table->setHeaders(['Path', $titleA, $titleB]);
29
        $table->addRows(array_map(function(IndexObjectComparison $difference) {
30
31
            return [
32
                $difference->getRelativePath(),
33
                $this->renderIndexObjectColumn($difference->getIndexObjectA()),
34
                $this->renderIndexObjectColumn($difference->getIndexObjectB()),
35
            ];
36
37
        }, iterator_to_array($indexComparison->getIterator())));
38
39
        $table->render();
40
    }
41
42
    protected function renderIndexObjectColumn(?IndexObject $indexObject): string
43
    {
44
        if ($indexObject === null)
45
        {
46
            return '-';
47
        }
48
49
        $parts = [
50
            $indexObject->getTypeName(),
51
            "mtime: " . ($indexObject->getMtime() === null ? '-' : FilesystemUtility::buildTime($indexObject->getMtime())),
52
            "ctime: " . ($indexObject->getCtime() === null ? '-' : FilesystemUtility::buildTime($indexObject->getCtime())),
53
            "perms: 0{$indexObject->getPermissionsString()}",
54
        ];
55
56
        if ($indexObject->isFile())
57
        {
58
            $parts = array_merge($parts, [
59
                "size: " . (($indexObject->getSize() !== null) ? static::formatMemory($indexObject->getSize()) : '-'),
60
                "blobId: " . ($indexObject->getBlobId() ?: '-'),
61
                "hash(es):" . ($indexObject->getHashes() && $indexObject->getHashes()->count() ? ("\n" . str_replace(', ', "\n", $indexObject->getHashes()->__toString())) : ' -'),
62
            ]);
63
        }
64
        elseif ($indexObject->isLink())
65
        {
66
            $parts = array_merge($parts, [
67
                "target: {$indexObject->getLinkTarget()}"
68
            ]);
69
        }
70
71
        return implode("\n", $parts);
72
    }
73
}
74