Application::silenceXDebug()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 3
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 5
ccs 0
cts 0
cp 0
crap 12
rs 10
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 3
    public function __construct(string $executable)
44
    {
45 3
        $this->executable = $executable;
46
47 3
        parent::__construct('CaptainHook', CH::VERSION);
48
49 3
        $this->setDefaultCommand('list');
50 3
        $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 3
    public function doRun(InputInterface $input, OutputInterface $output): int
63
    {
64 3
        if ($this->isHelpWithoutCommand($input)) {
65
            // Run the `list` command not `list --help`
66 1
            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 2
    public function getDefaultCommands(): array
77
    {
78 2
        $resolver        = new Resolver($this->executable);
79 2
        $symfonyDefaults = parent::getDefaultCommands();
80
81 2
        return array_merge(
82 2
            array_slice($symfonyDefaults, 0, 2),
83 2
            [
84 2
                new Cmd\Install($resolver),
85 2
                new Cmd\Uninstall($resolver),
86 2
                new Cmd\Configuration($resolver),
87 2
                new Cmd\Info($resolver),
88 2
                new Cmd\Add($resolver),
89 2
                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 2
                new Cmd\Hook\PreCommit($resolver),
97 2
                new Cmd\Hook\PrepareCommitMsg($resolver),
98 2
                new Cmd\Hook\PrePush($resolver),
99 2
            ]
100 2
        );
101
    }
102
103
    /**
104
     * Append release date to version output
105
     *
106
     * @return string
107
     */
108 3
    public function getLongVersion(): string
109
    {
110 3
        return sprintf(
111 3
            '<info>%s</info> version <comment>%s</comment> %s <fg=blue>#StandWith</><fg=yellow>Ukraine</>',
112 3
            $this->getName(),
113 3
            $this->getVersion(),
114 3
            CH::RELEASE_DATE
115 3
        );
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 3
    private function isHelpWithoutCommand(InputInterface $input): bool
140
    {
141 3
        return $input->hasParameterOption(['--help', '-h'], true) && !$input->getFirstArgument();
142
    }
143
}
144