Completed
Push — master ( 29472a...0fa5c2 )
by Richan
04:47 queued 03:37
created

Application   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 132
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 2
dl 0
loc 132
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A disableXdebug() 0 11 2
A doRun() 0 6 1
A getCommandName() 0 4 1
A getDefaultCommands() 0 8 1
A getDefaultInputDefinition() 0 9 1
A getDefinition() 0 7 1
A getProcessBuilder() 0 7 2
A setProcessBuilder() 0 4 1
1
<?php
2
3
namespace Suitmedia\LighthouseAudit;
4
5
use Symfony\Component\Console\Application as AbstractApplication;
6
use Symfony\Component\Console\Input\InputArgument;
7
use Symfony\Component\Console\Input\InputDefinition;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Input\InputOption;
10
use Symfony\Component\Console\Output\OutputInterface;
11
12
final class Application extends AbstractApplication
13
{
14
    /**
15
     * Process builder object.
16
     *
17
     * @var ProcessBuilder
18
     */
19
    protected $processBuilder;
20
21
    /**
22
     * Application constructor.
23
     *
24
     * @param string $name
25
     * @param string $version
26
     */
27
    public function __construct(string $name = 'lighthouse-audit', string $version = '1.0.0')
28
    {
29
        parent::__construct($name, $version);
30
    }
31
32
    /**
33
     * Disable XDebug. This method is copied from:
34
     * https://github.com/sebastianbergmann/phpcpd/blob/ca6b97f32ebdd3585652a3035d6221a8d2a6c11b/src/CLI/Application.php#L87
35
     *
36
     * @return void
37
     */
38
    private function disableXdebug() :void
39
    {
40
        if (!\extension_loaded('xdebug')) {
41
            return;
42
        }
43
        \ini_set('xdebug.scream', 0);
44
        \ini_set('xdebug.max_nesting_level', 8192);
45
        \ini_set('xdebug.show_exception_trace', 0);
46
        \ini_set('xdebug.show_error_trace', 0);
47
        \xdebug_disable();
48
    }
49
50
    /**
51
     * Run the Lighthouse Audit application.
52
     *
53
     * @param InputInterface $input
54
     * @param OutputInterface $output
55
     * @return int
56
     * @throws \Throwable
57
     */
58
    public function doRun(InputInterface $input, OutputInterface $output) :int
59
    {
60
        $this->disableXdebug();
61
62
        return parent::doRun($input, $output);
63
    }
64
65
    /**
66
     * Get Lighthouse Audit command name.
67
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
68
     *
69
     * @param InputInterface $input
70
     * @return string
71
     */
72
    protected function getCommandName(InputInterface $input) :string
73
    {
74
        return $this->getName();
75
    }
76
77
    /**
78
     * Gets the default commands that should always be available.
79
     *
80
     * @return array
81
     */
82
    protected function getDefaultCommands() :array
83
    {
84
        $commands = parent::getDefaultCommands();
85
86
        $commands[] = new Command($this->getName());
87
88
        return $commands;
89
    }
90
91
    /**
92
     * Gets the default input definition.
93
     *
94
     * @return InputDefinition
95
     */
96
    protected function getDefaultInputDefinition() :InputDefinition
97
    {
98
        return new InputDefinition([
99
            new InputArgument('command', InputArgument::REQUIRED, 'The command to execute'),
100
101
            new InputOption('--help', '-h', InputOption::VALUE_NONE, 'Display this help message'),
102
            new InputOption('--version', '-V', InputOption::VALUE_NONE, 'Display this application version'),
103
        ]);
104
    }
105
106
    /**
107
     * Gets the InputDefinition related to this Application.
108
     * This method is copied from :
109
     * https://github.com/sebastianbergmann/phpcpd/blob/ca6b97f32ebdd3585652a3035d6221a8d2a6c11b/src/CLI/Application.php#L31
110
     *
111
     * @return InputDefinition
112
     */
113
    public function getDefinition() :InputDefinition
114
    {
115
        $inputDefinition = parent::getDefinition();
116
        $inputDefinition->setArguments();
117
118
        return $inputDefinition;
119
    }
120
121
    /**
122
     * Get the process builder object.
123
     *
124
     * @return ProcessBuilder
125
     */
126
    public function getProcessBuilder() :ProcessBuilder
127
    {
128
        if ($this->processBuilder === null) {
129
            $this->processBuilder = new ProcessBuilder();
130
        }
131
        return $this->processBuilder;
132
    }
133
134
    /**
135
     * Set the process builder object.
136
     *
137
     * @param ProcessBuilder $builder
138
     */
139
    public function setProcessBuilder(ProcessBuilder $builder) :void
140
    {
141
        $this->processBuilder = $builder;
142
    }
143
}
144