Passed
Pull Request — master (#274)
by Théo
05:31
created

Diff.php$0 ➔ execute()   B

Complexity

Conditions 5

Size

Total Lines 59

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 59
rs 8.5833
c 0
b 0
f 0
cc 5

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 KevinGH\Box\PhpSettingsHandler;
19
use ParagonIE\Pharaoh\Pharaoh;
20
use ParagonIE\Pharaoh\PharDiff;
21
use Symfony\Component\Console\Command\Command;
22
use Symfony\Component\Console\Input\InputArgument;
23
use Symfony\Component\Console\Input\InputInterface;
24
use Symfony\Component\Console\Input\InputOption;
25
use Symfony\Component\Console\Logger\ConsoleLogger;
26
use Symfony\Component\Console\Output\OutputInterface;
27
use Symfony\Component\Console\Style\SymfonyStyle;
28
use Throwable;
29
use function array_map;
30
31
/**
32
 * @private
33
 */
34
final class Diff extends Command
35
{
36
    use CreateTemporaryPharFile;
37
38
    private const FIRST_PHAR_ARG = 'pharA';
39
    private const SECOND_PHAR_ARG = 'pharB';
40
41
    private const GNU_DIFF_OPTION = 'gnu-diff';
42
    private const CHECK_OPTION = 'check';
43
44
    /**
45
     * {@inheritdoc}
46
     */
47
    protected function configure(): void
48
    {
49
        parent::configure();
50
51
        $this->setName('diff');
52
        $this->setDescription('Display the differences between all of the files in two PHARs');
53
54
        $this->addArgument(
55
            self::FIRST_PHAR_ARG,
56
            InputArgument::REQUIRED,
57
            'The first PHAR'
58
        );
59
        $this->addArgument(
60
            self::SECOND_PHAR_ARG,
61
            InputArgument::REQUIRED,
62
            'The second PHAR'
63
        );
64
65
        $this->addOption(
66
            self::GNU_DIFF_OPTION,
67
            'd',
68
            InputOption::VALUE_NONE,
69
            'Displays a GNU diff instead of the default git diff'
70
        );
71
        $this->addOption(
72
            self::CHECK_OPTION,
73
            'c',
74
            InputOption::VALUE_OPTIONAL,
75
            'Verify the authenticity of the contents between the two PHARs with the given hash function.',
76
            'sha384'
77
        );
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83
    protected function execute(InputInterface $input, OutputInterface $output): int
84
    {
85
        $io = new SymfonyStyle($input, $output);
86
87
        (new PhpSettingsHandler(new ConsoleLogger($output)))->check();
88
89
        $paths = [
90
            $input->getArgument(self::FIRST_PHAR_ARG),
91
            $input->getArgument(self::SECOND_PHAR_ARG),
92
        ];
93
94
        Assertion::allFile($paths);
1 ignored issue
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

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