Skip to content

Commit 9342c8a

Browse files
committed
- use getcwd() for gitlog
- refactor get_complete_version() for VERSION file and $version argument - simplify FQN
1 parent b292c27 commit 9342c8a

1 file changed

Lines changed: 17 additions & 15 deletions

File tree

semver.php

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ function get_version(array $version = []): string
4242
*/
4343
function get_version_array(string $version): array
4444
{
45-
if (!preg_match(SEMVER_REGEX, \trim($version), $match)) {
45+
if (!preg_match(SEMVER_REGEX, trim($version), $match)) {
4646
return INVALID_VERSION_ARRAY;
4747
}
4848
array_shift($match);
@@ -62,18 +62,19 @@ function get_version_array(string $version): array
6262
function get_complete_version(array $version): array
6363
{
6464
if (false === empty($version)) {
65-
assert(3 === \count($version), 'version array should have exactly 3 parts');
66-
assert('' !== $version[1], 'pre-release is empty, should be zero or valid identifier');
67-
assert('' !== $version[2], 'build-metadata is empty, should be zero or valid identifier');
68-
return $version;
65+
goto assert;
6966
}
70-
if (defined('VERSION') && is_array(VERSION)) {
71-
return get_version_array(join('-', array_filter(VERSION)));
72-
}
73-
if ($version = @file_get_contents(getcwd() . '/VERSION')) {
74-
return get_version_array($version);
75-
}
76-
return INVALID_VERSION_ARRAY;
67+
$version = match (true) {
68+
defined('VERSION') && is_array(VERSION) => get_version_array(join('-', array_filter(VERSION))),
69+
file_exists($version = __DIR__ . '/../../../VERSION'), // project dir relative to vendor
70+
file_exists($version = getcwd() . '/VERSION') => get_version_array(@file_get_contents($version)),
71+
default => INVALID_VERSION_ARRAY,
72+
};
73+
assert:
74+
assert(3 === count($version), 'version array should have exactly 3 parts');
75+
assert('' !== $version[1], 'pre-release is empty, should be zero or valid identifier');
76+
assert('' !== $version[2], 'build-metadata is empty, should be zero or valid identifier');
77+
return $version;
7778
}
7879

7980
/**
@@ -101,14 +102,15 @@ function get_major_version(array $version): int
101102
*/
102103
function get_git_changeset(): string
103104
{
105+
$cwd = getcwd() . '/.';
104106
$format = 'YmdHis';
105107
$gitlog = proc_open('git log --pretty=format:%ct --quiet -l HEAD', [
106108
['pipe', 'r'],
107109
['pipe', 'w'],
108110
['pipe', 'w'],
109-
], $pipes, __DIR__);
111+
], $pipes, $cwd);
110112
if (false === is_resource($gitlog)) {
111-
return date($format, filemtime(__DIR__ . '/.'));
113+
return date($format, filemtime($cwd));
112114
}
113115
stream_set_blocking($pipes[2], false);
114116
$timestamp = stream_get_contents($pipes[1]);
@@ -119,7 +121,7 @@ function get_git_changeset(): string
119121
fclose($pipes[2]);
120122
proc_close($gitlog);
121123
if (empty($timestamp)) {
122-
return date($format, filemtime(__DIR__ . '/.'));
124+
return date($format, filemtime($cwd));
123125
}
124126
// UNIX timestamps are stored in UTC
125127
return date_create_immutable('@' . $timestamp)->format($format);

0 commit comments

Comments
 (0)