Passed
Pull Request — master (#238)
by Théo
02:58
created

Diff::execute()   B

Complexity

Conditions 7
Paths 44

Size

Total Lines 57
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 57
rs 7.6759
c 0
b 0
f 0
cc 7
eloc 32
nc 44
nop 2

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
declare(strict_types=1);
4
5
/*
6
 * This file is part of the box project.
7
 *
8
 * (c) Kevin Herrera <[email protected]>
9
 *     Théo Fidry <[email protected]>
10
 *
11
 * This source file is subject to the MIT license that is bundled
12
 * with this source code in the file LICENSE.
13
 */
14
15
namespace KevinGH\Box\Console\Command;
16
17
use Assert\Assertion;
18
use ParagonIE\Pharaoh\Pharaoh;
19
use ParagonIE\Pharaoh\PharDiff;
20
use Symfony\Component\Console\Command\Command;
21
use Symfony\Component\Console\Input\InputArgument;
22
use Symfony\Component\Console\Input\InputInterface;
23
use Symfony\Component\Console\Input\InputOption;
24
use Symfony\Component\Console\Output\OutputInterface;
25
use Symfony\Component\Console\Style\SymfonyStyle;
26
use Throwable;
27
use function array_map;
28
use function KevinGH\Box\FileSystem\remove;
29
30
/**
31
 * @private
32
 */
33
final class Diff extends Command
34
{
35
    use CreateTemporaryPharFile;
36
37
    private const FIRST_PHAR_ARG = 'pharA';
38
    private const SECOND_PHAR_ARG = 'pharB';
39
40
    private const GNU_DIFF_OPTION = 'gnu-diff';
41
    private const CHECK_OPTION = 'check';
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    protected function configure(): void
47
    {
48
        parent::configure();
49
50
        $this->setName('diff');
51
        $this->setDescription('Display the differences between all of the files in two PHARs');
52
53
        $this->addArgument(
54
            self::FIRST_PHAR_ARG,
55
            InputArgument::OPTIONAL,
56
            'The first PHAR'
57
        );
58
        $this->addArgument(
59
            self::SECOND_PHAR_ARG,
60
            InputArgument::OPTIONAL,
61
            'The second PHAR'
62
        );
63
64
        $this->addOption(
65
            self::GNU_DIFF_OPTION,
66
            'd',
67
            InputOption::VALUE_NONE,
68
            'Displays a GNU diff instead of the default git diff'
69
        );
70
        $this->addOption(
71
            self::CHECK_OPTION,
72
            'c',
73
            InputOption::VALUE_OPTIONAL,
74
            'Verify the authenticity of the contents between the two PHARs with the given hash function.',
75
            'sha384'
76
        );
77
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82
    protected function execute(InputInterface $input, OutputInterface $output): int
83
    {
84
        $io = new SymfonyStyle($input, $output);
85
86
        $paths = [
87
            $input->getArgument(self::FIRST_PHAR_ARG),
88
            $input->getArgument(self::SECOND_PHAR_ARG),
89
        ];
90
91
        Assertion::allFile($paths);
0 ignored issues
show
Bug introduced by
$paths of type array<integer,mixed> is incompatible with the type string expected by parameter $value of Assert\Assertion::allFile(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

91
        Assertion::allFile(/** @scrutinizer ignore-type */ $paths);
Loading history...
92
93
        $tmpFiles = [];
94
95
        try {
96
            $diff = new PharDiff(
97
                ...array_map(
98
                    function (string $path) use (&$tmpFiles): Pharaoh {
99
                        $path = false !== realpath($path) ? realpath($path) : $path;
100
101
                        $tmpPath = $this->createTemporaryPhar($path);
102
103
                        if ($path !== $tmpPath) {
104
                            $tmpFiles[] = $tmpPath;
105
                        }
106
107
                        return new Pharaoh($tmpPath);
108
                    },
109
                    $paths
110
                )
111
            );
112
            $diff->setVerbose(true);
113
        } catch (Throwable $throwable) {
114
            if ($output->isDebug()) {
115
                throw $throwable;
116
            }
117
118
            $io->writeln(
119
                sprintf(
120
                    '<error>Could not check the PHARs: %s</error>',
121
                    $throwable->getMessage()
122
                )
123
            );
124
125
            return 1;
126
        } finally {
127
            remove($tmpFiles);
128
        }
129
130
        if ($input->hasParameterOption(['-c', '--check'])) {
131
            return $diff->listChecksums($input->getOption(self::CHECK_OPTION) ?? 'sha384');
132
        }
133
134
        if ($input->getOption(self::GNU_DIFF_OPTION)) {
135
            return $diff->printGnuDiff();
136
        }
137
138
        return $diff->printGitDiff();
139
    }
140
}
141