Passed
Push — main ( 997a09...b8fed3 )
by Sebastian
04:49 queued 54s
created

Application::isHelpWithoutCommand()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 1
nc 2
nop 1
dl 0
loc 3
ccs 2
cts 2
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
     * @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
49 2
        $this->setDefaultCommand('list');
50 2
        $this->silenceXDebug();
51
    }
52
53
    /**
54
     * Make sure the list command is run on default `-h|--help` executions
55
     *
56
     * @param  \Symfony\Component\Console\Input\InputInterface   $input
57
     * @param  \Symfony\Component\Console\Output\OutputInterface $output
58
     * @return int
59
     * @throws \Symfony\Component\Console\Exception\ExceptionInterface
60
     * @throws \Throwable
61
     */
62 2
    public function doRun(InputInterface $input, OutputInterface $output): int
63
    {
64 2
        if ($this->isHelpWithoutCommand($input)) {
65
            // Run the `list` command not `list --help`
66
            return $this->find('list')->run($input, $output);
67
        }
68 2
        return parent::doRun($input, $output);
69
    }
70
71
    /**
72
     * Initializes all the CaptainHook commands
73
     *
74
     * @return \Symfony\Component\Console\Command\Command[]
75
     */
76 1
    public function getDefaultCommands(): array
77
    {
78 1
        $resolver        = new Resolver($this->executable);
79 1
        $symfonyDefaults = parent::getDefaultCommands();
80
81 1
        return array_merge(
82 1
            array_slice($symfonyDefaults, 0, 2),
83 1
            [
84 1
                new Cmd\Install($resolver),
85 1
                new Cmd\Uninstall($resolver),
86 1
                new Cmd\Configuration($resolver),
87 1
                new Cmd\Info($resolver),
88 1
                new Cmd\Add($resolver),
89 1
                new Cmd\Disable($resolver),
90 1
                new Cmd\Enable($resolver),
91 1
                new Cmd\Hook\CommitMsg($resolver),
92 1
                new Cmd\Hook\PostCheckout($resolver),
93 1
                new Cmd\Hook\PostCommit($resolver),
94 1
                new Cmd\Hook\PostMerge($resolver),
95 1
                new Cmd\Hook\PostRewrite($resolver),
96 1
                new Cmd\Hook\PreCommit($resolver),
97 1
                new Cmd\Hook\PrepareCommitMsg($resolver),
98 1
                new Cmd\Hook\PrePush($resolver),
99 1
            ]
100 1
        );
101
    }
102
103
    /**
104
     * Append release date to version output
105
     *
106
     * @return string
107
     */
108 2
    public function getLongVersion(): string
109
    {
110 2
        return sprintf(
111 2
            '<info>%s</info> version <comment>%s</comment> %s <fg=blue>#StandWith</><fg=yellow>Ukraine</>',
112 2
            $this->getName(),
113 2
            $this->getVersion(),
114 2
            CH::RELEASE_DATE
115 2
        );
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 2
    private function isHelpWithoutCommand(InputInterface $input): bool
140
    {
141 2
        return $input->hasParameterOption(['--help', '-h'], true) && !$input->getFirstArgument();
142
    }
143
}
144