Completed
Push — master ( 9fc7de...878c9d )
by Julian
01:42
created

VersionProvider::getGitVersion()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2.0116

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 12
ccs 6
cts 7
cp 0.8571
rs 9.4285
c 1
b 0
f 0
cc 2
eloc 7
nc 2
nop 0
crap 2.0116
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ParaTest\Console;
6
7
use Symfony\Component\Process\Process;
8
9
/**
10
 * Class VersionProvider.
11
 *
12
 * Obtain version information of the ParaTest application itself based on
13
 * it's current installment (composer; git; default passed)
14
 */
15
final class VersionProvider
16
{
17
    const PACKAGE = 'brianium/paratest';
18
    /**
19
     * @var null
20
     */
21
    private $default;
22
23 4
    public function __construct($default = null)
24
    {
25 4
        $this->default = $default;
26 4
    }
27
28 1
    public static function getVersion($default = null)
29
    {
30 1
        $provider = new self($default);
31
32 1
        return $provider->getParaTestVersion();
33
    }
34
35 1
    public function getParaTestVersion()
36
    {
37 1
        return $this->getComposerInstalledVersion(self::PACKAGE)
38 1
            ?? $this->getGitVersion()
39 1
            ?? $this->default;
40
    }
41
42 2
    public function getGitVersion()
43
    {
44 2
        $version = null;
0 ignored issues
show
Unused Code introduced by
$version is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
45
46 2
        $process = new Process('git describe --tags --always --first-parent', __DIR__);
47 2
        if ($process->run() !== 0) {
48
            return;
49
        }
50 2
        $version = trim($process->getOutput());
51
52 2
        return $version;
53
    }
54
55 2
    public function getComposerInstalledVersion($package)
56
    {
57 2
        if (null === $path = $this->getComposerInstalledJsonPath()) {
58
            return;
59
        }
60
61 2
        $result = file_get_contents($path);
62 2
        if (false === $result) {
63
            return;
64
        }
65
66 2
        $struct = json_decode($result, true, 16);
67 2
        if (!is_array($struct)) {
68
            return;
69
        }
70
71 2
        foreach ($struct as $entry) {
72 2
            if (!is_array($entry)) {
73
                continue;
74
            }
75 2
            $name = $entry['name'] ?? null;
76 2
            if (null === $name || $name !== $package) {
77 2
                continue;
78
            }
79 1
            $version = $entry['version'] ?? null;
80 1
            if (null === $version) {
81
                continue;
82
            }
83
84 1
            return $version;
85
        }
86 2
    }
87
88
    /**
89
     * @return string|null path to composer/installed.json
90
     */
91 2
    private function getComposerInstalledJsonPath()
92
    {
93
        $paths = [
94
            // path in the installed version
95 2
            __DIR__ . '/../../../../composer/installed.json',
96
            // path in the source version
97
            __DIR__ . '/../../vendor/composer/installed.json',
98
        ];
99
100
        // first hit makes it
101 2
        foreach ($paths as $path) {
102 2
            if (file_exists($path) && is_readable($path)) {
103 2
                return $path;
104
            }
105
        }
106
    }
107
}
108