forked from vimeo/psalm
/
VersionUtils.php
99 lines (81 loc) · 2.56 KB
/
VersionUtils.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?php
namespace Psalm\Internal;
use OutOfBoundsException;
use PackageVersions\Versions;
use Phar;
use function class_exists;
use function dirname;
use function file_put_contents;
use function var_export;
/**
* @internal
* @psalm-type _VersionData=array{"vimeo/psalm": string, "nikic/php-parser": string}
*/
final class VersionUtils
{
private const PSALM_PACKAGE = 'vimeo/psalm';
private const PHP_PARSER_PACKAGE = 'nikic/php-parser';
/** @var null|_VersionData */
private static $versions = null;
/** @psalm-suppress UnusedConstructor it's here to prevent instantiations */
private function __construct()
{
}
public static function getPsalmVersion(): string
{
return self::getVersions()[self::PSALM_PACKAGE];
}
public static function getPhpParserVersion(): string
{
return self::getVersions()[self::PHP_PARSER_PACKAGE];
}
/** @psalm-suppress UnusedMethod called from bin/build-phar.sh */
public static function dump(): void
{
$versions = self::loadComposerVersions();
$exported = '<?php return ' . var_export($versions, true) . ';';
file_put_contents(dirname(__DIR__, 3) . '/build/phar-versions.php', $exported);
}
/** @return _VersionData */
private static function getVersions(): array
{
if (self::$versions !== null) {
return self::$versions;
}
if ($versions = self::loadPharVersions()) {
return self::$versions = $versions;
}
if ($versions = self::loadComposerVersions()) {
return self::$versions = $versions;
}
return self::$versions = [self::PSALM_PACKAGE => 'unknown', self::PHP_PARSER_PACKAGE => 'unknown'];
}
/** @return _VersionData|null */
private static function loadPharVersions(): ?array
{
if (!class_exists(Phar::class)) {
return null;
}
$phar_filename = Phar::running(true);
if (!$phar_filename) {
return null;
}
/**
* @psalm-suppress UnresolvableInclude
* @var _VersionData
*/
return require($phar_filename . '/phar-versions.php');
}
/** @return _VersionData|null */
private static function loadComposerVersions(): ?array
{
try {
return [
self::PSALM_PACKAGE => Versions::getVersion(self::PSALM_PACKAGE),
self::PHP_PARSER_PACKAGE => Versions::getVersion(self::PHP_PARSER_PACKAGE),
];
} catch (OutOfBoundsException $ex) {
}
return null;
}
}