Application   A
last analyzed

Complexity

Total Complexity 23

Size/Duplication

Total Lines 182
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 15

Test Coverage

Coverage 34.67%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 23
lcom 1
cbo 15
dl 0
loc 182
ccs 26
cts 75
cp 0.3467
rs 9.1666
c 1
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 3
A getConveyor() 0 4 1
A getDefaultCommands() 0 19 2
A getDefaultInputDefinition() 0 9 1
A getName() 0 4 1
A getVersion() 0 12 2
C run() 0 53 10
A doRun() 0 16 2
A setAutoExit() 0 7 1
1
<?php
2
3
/*
4
 * This file is part of the Conveyor package.
5
 *
6
 * (c) Jeroen Fiege <[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 Webcreate\Conveyor\Console;
13
14
use Symfony\Component\Console\Application as BaseApplication;
15
use Symfony\Component\Console\Input\ArgvInput;
16
use Symfony\Component\Console\Input\InputInterface;
17
use Symfony\Component\Console\Input\InputOption;
18
use Symfony\Component\Console\Output\ConsoleOutput;
19
use Symfony\Component\Console\Output\ConsoleOutputInterface;
20
use Symfony\Component\Console\Output\OutputInterface;
21
use Webcreate\Conveyor\Conveyor;
22
use Webcreate\Conveyor\IO\ConsoleIO;
23
24
class Application extends BaseApplication
0 ignored issues
show
Complexity introduced by
The class Application has a coupling between objects value of 20. Consider to reduce the number of dependencies under 13.
Loading history...
25
{
26
    /**
27
     * @var Conveyor
28
     */
29
    protected $conveyor;
30
31
    /**
32
     * @var bool
33
     */
34
    protected $originalAutoExit;
35
36
    /**
37
     * @param Conveyor $conveyor
38
     */
39 2
    public function __construct(Conveyor $conveyor)
40
    {
41 2
        $this->conveyor = $conveyor;
42
43 2
        if (function_exists('date_default_timezone_set') && function_exists('date_default_timezone_get')) {
44 2
            date_default_timezone_set(@date_default_timezone_get());
45
        }
46
47 2
        parent::__construct();
48 2
    }
49
50
    /**
51
     * @return Conveyor
52
     */
53 2
    public function getConveyor()
54
    {
55 2
        return $this->conveyor;
56
    }
57
58
    /**
59
     * @inheritdoc
60
     */
61 2
    protected function getDefaultCommands()
62
    {
63 2
        $commands = parent::getDefaultCommands();
64 2
        $commands[] = new \Webcreate\Conveyor\Command\VersionsCommand();
65 2
        $commands[] = new \Webcreate\Conveyor\Command\BuildCommand();
66 2
        $commands[] = new \Webcreate\Conveyor\Command\SimulateCommand();
67 2
        $commands[] = new \Webcreate\Conveyor\Command\DeployCommand();
68 2
        $commands[] = new \Webcreate\Conveyor\Command\UndeployCommand();
69 2
        $commands[] = new \Webcreate\Conveyor\Command\DiffCommand();
70 2
        $commands[] = new \Webcreate\Conveyor\Command\InitCommand();
71 2
        $commands[] = new \Webcreate\Conveyor\Command\ValidateCommand();
72 2
        $commands[] = new \Webcreate\Conveyor\Command\StatusCommand();
73
74 2
        if ('phar:' === substr(__FILE__, 0, 5)) {
75
            $commands[] = new \Webcreate\Conveyor\Command\UpdateCommand();
76
        }
77
78 2
        return $commands;
0 ignored issues
show
Best Practice introduced by
The expression return $commands; seems to be an array, but some of its elements' types (Webcreate\Conveyor\Comma...r\Command\UpdateCommand) are incompatible with the return type of the parent method Symfony\Component\Consol...ion::getDefaultCommands of type array<Symfony\Component\...le\Command\ListCommand>.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
79
    }
80
81
    /**
82
     * @inheritdoc
83
     */
84 2
    protected function getDefaultInputDefinition()
85
    {
86 2
        $inputDefinition = parent::getDefaultInputDefinition();
87 2
        $inputDefinition->addOption(
88 2
            new InputOption('--configuration', '-c', InputOption::VALUE_REQUIRED, 'Configuration file override')
89
        );
90
91 2
        return $inputDefinition;
92
    }
93
94
    /**
95
     * @inheritdoc
96
     */
97
    public function getName()
98
    {
99
        return 'Conveyor';
100
    }
101
102
    /**
103
     * @inheritdoc
104
     */
105
    public function getVersion()
106
    {
107
        $versionFilename = 'VERSION';
108
109
        if ('phar:' === substr(__FILE__, 0, 5)) {
110
            $versionFile = 'phar://conveyor.phar/' . $versionFilename;
111
        } else {
112
            $versionFile = __DIR__ . '/../../../../' . $versionFilename;
113
        }
114
115
        return trim(file_get_contents($versionFile));
116
    }
117
118
    /**
119
     * @inheritdoc
120
     */
121
    public function run(InputInterface $input = null, OutputInterface $output = null)
0 ignored issues
show
Complexity introduced by
This operation has 252 execution paths which exceeds the configured maximum of 200.

A high number of execution paths generally suggests many nested conditional statements and make the code less readible. This can usually be fixed by splitting the method into several smaller methods.

You can also find more information in the “Code” section of your repository.

Loading history...
122
    {
123
        $this->setCatchExceptions(false);
124
125
        if (null === $input) {
126
            $input = new ArgvInput();
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $input. This often makes code more readable.
Loading history...
127
        }
128
129
        if (null === $output) {
130
            $output = new ConsoleOutput();
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $output. This often makes code more readable.
Loading history...
131
        }
132
133
        try {
134
            $statusCode = parent::run($input, $output);
135
        } catch (\Exception $e) {
136
            $container = $this->getConveyor()->getContainer();
137
138
            if (null !== $container) {
139
                /** @var $logger \Monolog\Logger */
140
                $logger = $this->getConveyor()->getContainer()->get('logger');
141
142
                $message = sprintf(
143
                    '%s: %s (uncaught exception) at %s line %s while running console command `%s`',
144
                    get_class($e),
145
                    $e->getMessage(),
146
                    $e->getFile(),
147
                    $e->getLine(),
148
                    $this->getCommandName($input)
149
                );
150
                $logger->addCritical($message);
151
            }
152
153
            if ($output instanceof ConsoleOutputInterface) {
154
                $this->renderException($e, $output->getErrorOutput());
155
            } else {
156
                $this->renderException($e, $output);
157
            }
158
            $statusCode = $e->getCode();
159
160
            $statusCode = is_numeric($statusCode) && $statusCode ? $statusCode : 1;
161
        }
162
163
        if ($this->originalAutoExit) {
164
            if ($statusCode > 255) {
165
                $statusCode = 255;
166
            }
167
            // @codeCoverageIgnoreStart
168
            exit($statusCode);
0 ignored issues
show
Coding Style Compatibility introduced by
The method run() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
169
            // @codeCoverageIgnoreEnd
170
        }
171
172
        return $statusCode;
173
    }
174
175
    /**
176
     * @inheritdoc
177
     */
178
    public function doRun(InputInterface $input, OutputInterface $output)
179
    {
180
        $io = new ConsoleIO($input, $output, $this->getHelperSet());
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $io. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
181
        $conveyor = $this->getConveyor();
182
183
        // retrieve the config file option really early so we can still make changes before the di container
184
        // gets compiled
185
        $configFile = $input->getParameterOption(array('--configuration', '-c'));
186
        if ($configFile) {
187
            $conveyor->setConfigFile($configFile);
188
        }
189
190
        $conveyor->boot($io);
191
192
        parent::doRun($input, $output);
193
    }
194
195
    /**
196
     * @inheritdoc
197
     */
198
    public function setAutoExit($bool)
199
    {
200
        // parent property is private, so we need to intercept it in a setter
201
        $this->originalAutoExit = (bool) $bool;
202
203
        parent::setAutoExit($bool);
204
    }
205
}
206