Test Failed
Push — main ( b1c0ad...997a09 )
by Sebastian
03:50
created

Application::doRun()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
nc 2
nop 2
dl 0
loc 7
ccs 7
cts 7
cp 1
crap 2
rs 10
c 1
b 0
f 0
1
<?php
2
3
/**
4
 * This file is part of CaptainHook
5
 *
6
 * (c) Sebastian Feldmann <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace CaptainHook\App\Console;
13
14
use CaptainHook\App\CH;
15
use CaptainHook\App\Console\Command as Cmd;
16
use CaptainHook\App\Console\Runtime\Resolver;
17
use Symfony\Component\Console\Application as SymfonyApplication;
18
use Symfony\Component\Console\Input\InputInterface;
19
use Symfony\Component\Console\Output\OutputInterface;
20
21
/**
22
 * Class Application
23
 *
24
 * @package CaptainHook
25
 * @author  Sebastian Feldmann <[email protected]>
26
 * @link    https://github.com/captainhook-git/captainhook
27
 * @since   Class available since Release 0.9.0
28
 */
29
class Application extends SymfonyApplication
30
{
31
    /**
32
     * Path to captainhook binary
33
     *
34
     * @var string
35
     */
36
    protected string $executable;
37
38
    /**
39
     * Cli constructor.
40
     *
41 2
     * @param string $executable
42
     */
43 2
    public function __construct(string $executable)
44
    {
45 2
        $this->executable = $executable;
46
47 2
        parent::__construct('CaptainHook', CH::VERSION);
48 2
49
        $this->setDefaultCommand('list');
50
        $this->silenceXDebug();
51
    }
52
53
    /**
54
     * Make sure the list command is run on default `-h|--help` executions
55
     *
56 1
     * @param  \Symfony\Component\Console\Input\InputInterface   $input
57
     * @param  \Symfony\Component\Console\Output\OutputInterface $output
58 1
     * @return int
59 1
     * @throws \Symfony\Component\Console\Exception\ExceptionInterface
60
     * @throws \Throwable
61 1
     */
62 1
    public function doRun(InputInterface $input, OutputInterface $output): int
63 1
    {
64 1
        if ($this->isHelpWithoutCommand($input)) {
65 1
            // Run the `list` command not `list --help`
66 1
            return $this->find('list')->run($input, $output);
67 1
        }
68 1
        return parent::doRun($input, $output);
69 1
    }
70 1
71 1
    /**
72 1
     * Initializes all the CaptainHook commands
73 1
     *
74 1
     * @return \Symfony\Component\Console\Command\Command[]
75 1
     */
76 1
    public function getDefaultCommands(): array
77 1
    {
78 1
        $resolver        = new Resolver($this->executable);
79 1
        $symfonyDefaults = parent::getDefaultCommands();
80 1
81
        return array_merge(
82
            array_slice($symfonyDefaults, 0, 2),
83
            [
84
                new Cmd\Install($resolver),
85
                new Cmd\Uninstall($resolver),
86
                new Cmd\Configuration($resolver),
87
                new Cmd\Info($resolver),
88 2
                new Cmd\Add($resolver),
89
                new Cmd\Disable($resolver),
90 2
                new Cmd\Enable($resolver),
91 2
                new Cmd\Hook\CommitMsg($resolver),
92 2
                new Cmd\Hook\PostCheckout($resolver),
93 2
                new Cmd\Hook\PostCommit($resolver),
94 2
                new Cmd\Hook\PostMerge($resolver),
95 2
                new Cmd\Hook\PostRewrite($resolver),
96
                new Cmd\Hook\PreCommit($resolver),
97
                new Cmd\Hook\PrepareCommitMsg($resolver),
98
                new Cmd\Hook\PrePush($resolver),
99
            ]
100
        );
101
    }
102
103
    /**
104
     * Append release date to version output
105
     *
106
     * @return string
107
     */
108
    public function getLongVersion(): string
109
    {
110
        return sprintf(
111
            '<info>%s</info> version <comment>%s</comment> %s <fg=blue>#StandWith</><fg=yellow>Ukraine</>',
112
            $this->getName(),
113
            $this->getVersion(),
114
            CH::RELEASE_DATE
115
        );
116
    }
117
118
    /**
119
     * Make sure X-Debug does not interfere with the exception handling
120
     *
121
     * @return void
122
     *
123
     * @codeCoverageIgnore
124
     */
125
    private function silenceXDebug(): void
126
    {
127
        if (function_exists('ini_set') && extension_loaded('xdebug')) {
128
            ini_set('xdebug.show_exception_trace', '0');
129
            ini_set('xdebug.scream', '0');
130
        }
131
    }
132
133
    /**
134
     * Checks if the --help is called without any sub command
135
     *
136
     * @param \Symfony\Component\Console\Input\InputInterface $input
137
     * @return bool
138
     */
139
    private function isHelpWithoutCommand(InputInterface $input): bool
140
    {
141
        return $input->hasParameterOption(['--help', '-h'], true) && !$input->getFirstArgument();
142
    }
143
}
144