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; |
|
|
|
|
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
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
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.