Completed
Push — develop ( d73a05...7ce957 )
by Mike
07:24
created

Application   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 6

Importance

Changes 0
Metric Value
dl 0
loc 71
rs 10
c 0
b 0
f 0
wmc 9
lcom 0
cbo 6

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A getCommandName() 0 13 3
A getDefaultInputDefinition() 0 18 1
A getLongVersion() 0 4 1
A detectVersion() 0 15 3
1
<?php
2
/**
3
 * This file is part of phpDocumentor.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 *
8
 * @author    Mike van Riel <[email protected]>
9
 * @copyright 2010-2018 Mike van Riel / Naenius (http://www.naenius.com)
10
 * @license   http://www.opensource.org/licenses/mit-license.php MIT
11
 * @link      http://phpdoc.org
12
 */
13
14
namespace phpDocumentor\Console;
15
16
use PackageVersions\Versions;
17
use Symfony\Bundle\FrameworkBundle\Console\Application as BaseApplication;
18
use Symfony\Component\Console\Input\InputInterface;
19
use Symfony\Component\Console\Input\InputOption;
20
use Symfony\Component\HttpKernel\KernelInterface;
21
22
final class Application extends BaseApplication
23
{
24
    const VERSION = '@package_version@';
25
26
    public function __construct(KernelInterface $kernel)
27
    {
28
        parent::__construct($kernel);
29
30
        $this->setName('phpDocumentor');
31
        $this->setVersion($this->detectVersion());
32
    }
33
34
    protected function getCommandName(InputInterface $input)
35
    {
36
        // the regular setDefaultCommand option does not allow for options and arguments; with this workaround
37
        // we can have options and arguments when the first element in the argv options is not a recognized
38
        // command name.
39
        // We explicitly do not use the getFirstArgument of the $input variable since that skips options; we
40
        // explicitly want to know if the first thing after the php filename is a known command!
41
        if ((isset($_SERVER['argv'][1]) === false || $this->has($_SERVER['argv'][1]) === false)) {
42
            return 'project:run';
43
        }
44
45
        return $input->getFirstArgument();
46
    }
47
48
    protected function getDefaultInputDefinition()
49
    {
50
        $inputDefinition = parent::getDefaultInputDefinition();
51
52
        $inputDefinition->addOption(
53
            new InputOption(
54
                'config',
55
                'c',
56
                InputOption::VALUE_OPTIONAL,
57
                'Location of a custom configuration file'
58
            )
59
        );
60
        $inputDefinition->addOption(
61
            new InputOption('log', null, InputOption::VALUE_OPTIONAL, 'Log file to write to')
62
        );
63
64
        return $inputDefinition;
65
    }
66
67
    /**
68
     * Returns the long version of the application.
69
     *
70
     * @return string The long application version
71
     */
72
    public function getLongVersion(): string
73
    {
74
        return sprintf('%s <info>%s</info>', $this->getName(), $this->getVersion());
75
    }
76
77
    private function detectVersion(): string
78
    {
79
        $version = static::VERSION;
80
        if (static::VERSION === '@' . 'package_version' . '@') { //prevent replacing the version.
81
            $version = trim(file_get_contents(__DIR__ . '/../../../VERSION'));
82
            try {
83
                $version = 'v' . ltrim(
84
                    \Jean85\PrettyVersions::getVersion(Versions::ROOT_PACKAGE_NAME)->getPrettyVersion(),
85
                    'v'
86
                );
87
            } catch (\OutOfBoundsException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
88
            }
89
        }
90
        return $version;
91
    }
92
}
93