Completed
Pull Request — master (#9)
by Matthias
02:27
created

Application::getPackageVersion()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 21
rs 9.0534
cc 4
eloc 14
nc 6
nop 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: matthias
5
 * Date: 28.11.15
6
 * Time: 23:49
7
 */
8
9
namespace ComposerRequireChecker\Cli;
10
use Symfony\Component\Console\Application as AbstractApplication;
11
12
class Application extends AbstractApplication
13
{
14
15
    public function __construct()
16
    {
17
        parent::__construct('ComposerRequireChecker', $this->getPackageVersion());
18
19
        $check = new CheckCommand();
20
        $this->add($check);
21
        $this->setDefaultCommand($check->getName());
22
    }
23
24
    private function getPackageVersion() : string
25
    {
26
        $version = null;
27
        $pharFile = \Phar::running();
28
        if($pharFile) {
29
            $metadata = (new \Phar($pharFile))->getMetadata();
30
            $version = $metadata['version'] ?? null;
31
        }
32
33
        if(!$version) {
34
            $pwd = getcwd();
35
            chdir(realpath(__DIR__ . '/../../../'));
36
            $gitVersion = @exec('git describe --tags --dirty=-dev --always 2>&1', $output, $returnValue);
37
            chdir($pwd);
38
            if($returnValue === 0) {
39
                $version = $gitVersion;
40
            }
41
        }
42
43
        return $version ?? 'unknown-development';
44
    }
45
46
}