• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

eliashaeussler / version-bumper / 26179712348

20 May 2026 05:44PM UTC coverage: 78.161% (-10.5%) from 88.618%
26179712348

Pull #144

github

eliashaeussler
[FEATURE] Introduce `next-version` command
Pull Request #144: [FEATURE] Introduce `next-version` command

7 of 176 new or added lines in 5 files covered. (3.98%)

12 existing lines in 2 files now uncovered.

1156 of 1479 relevant lines covered (78.16%)

4.69 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

27.59
/src/Helper/VersionHelper.php
1
<?php
2

3
declare(strict_types=1);
4

5
/*
6
 * This file is part of the Composer package "eliashaeussler/version-bumper".
7
 *
8
 * Copyright (C) 2024-2026 Elias Häußler <elias@haeussler.dev>
9
 *
10
 * This program is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU General Public License as published by
12
 * the Free Software Foundation, either version 3 of the License, or
13
 * (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
 * GNU General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU General Public License
21
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22
 */
23

24
namespace EliasHaeussler\VersionBumper\Helper;
25

26
use EliasHaeussler\VersionBumper\Enum;
27
use EliasHaeussler\VersionBumper\Exception;
28
use EliasHaeussler\VersionBumper\Result;
29
use EliasHaeussler\VersionBumper\Version;
30
use GitElephant\Repository;
31

32
use function addcslashes;
33
use function preg_match;
34
use function str_contains;
35
use function str_replace;
36

37
/**
38
 * VersionHelper.
39
 *
40
 * @author Elias Häußler <elias@haeussler.dev>
41
 * @license GPL-3.0-or-later
42
 *
43
 * @internal
44
 */
45
final readonly class VersionHelper
46
{
47
    private const VERSION_PLACEHOLDER = '{%version%}';
48
    private const VERSION_REGEX = '(?P<version>v?\\d+\\.\\d+\\.\\d+)';
49

50
    public static function isValidVersion(string $version): bool
3✔
51
    {
52
        return 1 === preg_match('/^'.self::VERSION_REGEX.'$/', $version);
3✔
53
    }
54

55
    public static function isValidVersionPattern(string $pattern): bool
1✔
56
    {
57
        return str_contains($pattern, self::VERSION_PLACEHOLDER);
1✔
58
    }
59

60
    public static function convertPatternToRegularExpression(string $pattern): string
1✔
61
    {
62
        return '/'.str_replace(self::VERSION_PLACEHOLDER, self::VERSION_REGEX, addcslashes($pattern, '/')).'/';
1✔
63
    }
64

65
    public static function replaceVersionInPattern(string $pattern, Version\Version $version): string
1✔
66
    {
67
        return str_replace(self::VERSION_PLACEHOLDER, $version->full(), $pattern);
1✔
68
    }
69

70
    /**
71
     * @param list<Result\VersionBumpResult> $results
72
     *
73
     * @throws Exception\AmbiguousVersionsDetected
74
     */
NEW
75
    public static function extractVersionFromResults(array $results): ?Version\Version
×
76
    {
NEW
77
        $version = null;
×
78

NEW
79
        foreach ($results as $result) {
×
NEW
80
            foreach ($result->operations() as $operation) {
×
NEW
81
                $targetVersion = $operation->target();
×
82

NEW
83
                if (null === $targetVersion) {
×
NEW
84
                    continue;
×
85
                }
86

NEW
87
                if (null === $version) {
×
NEW
88
                    $version = $targetVersion;
×
89
                }
90

NEW
91
                if ($targetVersion->full() !== $version->full()) {
×
NEW
92
                    throw new Exception\AmbiguousVersionsDetected();
×
93
                }
94
            }
95
        }
96

NEW
97
        return $version;
×
98
    }
99

100
    /**
101
     * @throws Exception\CannotFetchLatestGitTag
102
     * @throws Exception\VersionIsNotSupported
103
     */
NEW
104
    public static function detectVersionFromVersionRange(
×
105
        Enum\VersionRange|string|null $versionRange,
106
        Repository $repository,
107
    ): ?Version\Version {
NEW
108
        if (null === $versionRange) {
×
NEW
109
            return null;
×
110
        }
111

NEW
112
        if (is_string($versionRange)) {
×
NEW
113
            return Version\Version::fromFullVersion($versionRange);
×
114
        }
115

NEW
116
        $tag = GitHelper::fetchLatestVersionTag($repository);
×
117

NEW
118
        if (null === $tag) {
×
NEW
119
            return null;
×
120
        }
121

NEW
122
        return Version\Version::fromFullVersion($tag->getName())->increase($versionRange);
×
123
    }
124
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc