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

wp-graphql / wp-graphql / #887

16 Jan 2025 10:08PM UTC coverage: 83.189% (-0.8%) from 83.968%
#887

push

php-coveralls

web-flow
Merge pull request #3272 from wp-graphql/release/v1.30.0

release: v1.30.0

473 of 718 new or added lines in 23 files covered. (65.88%)

2 existing lines in 2 files now uncovered.

12995 of 15621 relevant lines covered (83.19%)

298.21 hits per line

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

97.06
/src/Admin/Updates/SemVer.php
1
<?php
2
/**
3
 * Basic SemVer implementation.
4
 *
5
 * We don't need to do anything fancy here, just enough to compare versions.
6
 *
7
 * @package WPGraphQL\Admin\Updates
8
 * @since 1.30.0
9
 */
10

11
namespace WPGraphQL\Admin\Updates;
12

13
/**
14
 * Class SemVer
15
 */
16
final class SemVer {
17
        /**
18
         * Get the release type of the current version of WPGraphQL.
19
         *
20
         * @param string $old_version The old version of WPGraphQL.
21
         * @param string $new_version The new version of WPGraphQL.
22
         *
23
         * @return 'major'|'minor'|'patch'|'prerelease'|'unknown' The release type.
24
         */
25
        public static function get_release_type( string $old_version, string $new_version ): string {
26
                $old_version = self::parse( $old_version );
9✔
27
                $new_version = self::parse( $new_version );
9✔
28

29
                if ( null === $old_version || null === $new_version ) {
9✔
30
                        return 'unknown';
2✔
31
                }
32

33
                if ( $old_version['major'] < $new_version['major'] ) {
8✔
34
                        return 'major';
8✔
35
                }
36

37
                if ( $old_version['minor'] < $new_version['minor'] ) {
3✔
38
                        return 'minor';
3✔
39
                }
40

41
                if ( $old_version['patch'] < $new_version['patch'] ) {
2✔
42
                        return 'patch';
2✔
43
                }
44

45
                return 'prerelease';
1✔
46
        }
47

48
        /**
49
         * Parse the version string.
50
         *
51
         * @param string $version The version string.
52
         *
53
         * @return ?array{major:int,minor:int,patch:int,prerelease:?string,buildmetadata:?string,version:string}
54
         */
55
        public static function parse( string $version ): ?array {
56
                /**
57
                 * Semantic Versioning regex.
58
                 *
59
                 * @see https://semver.org/#is-there-a-suggested-regular-expression-regex-to-check-a-semver-string
60
                 */
61
                $regex = '/^(?P<major>0|[1-9]\d*)\.(?P<minor>0|[1-9]\d*)\.(?P<patch>0|[1-9]\d*)(?:-(?P<prerelease>(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+(?P<buildmetadata>[0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$/';
10✔
62

63
                if ( ! preg_match( $regex, $version, $matches ) ) {
10✔
64
                        return null;
3✔
65
                }
66

67
                $prerelease = ! empty( $matches['prerelease'] ) ? self::parse_prerelease_version( $matches['prerelease'] ) : null;
10✔
68

69
                return [
10✔
70
                        'major'         => (int) $matches['major'],
10✔
71
                        'minor'         => (int) $matches['minor'],
10✔
72
                        'patch'         => (int) $matches['patch'],
10✔
73
                        'prerelease'    => $prerelease,
10✔
74
                        'buildmetadata' => ! empty( $matches['buildmetadata'] ) ? $matches['buildmetadata'] : null,
10✔
75
                        'version'       => sprintf( '%d.%d.%d', $matches['major'], $matches['minor'], $matches['patch'] ),
10✔
76
                ];
10✔
77
        }
78

79
        /**
80
         * Parse the prerelease version string.
81
         *
82
         * @param string $version The version string.
83
         */
84
        protected static function parse_prerelease_version( string $version ): ?string {
85
                $separator      = '';
2✔
86
                $release_string = '';
2✔
87

88
                $version_parts = explode( '.', $version );
2✔
89

90
                if ( empty( $version_parts ) ) {
2✔
NEW
91
                        return null;
×
92
                }
93

94
                foreach ( $version_parts as $part ) {
2✔
95
                        // Leading zero is invalid.
96
                        if ( ctype_digit( $part ) ) {
2✔
97
                                $part = (int) $part;
2✔
98
                        }
99

100
                        $release_string .= $separator . (string) $part;
2✔
101
                        // If this isnt the first round, the separator is a dot.
102
                        $separator = '.';
2✔
103
                }
104

105
                return $release_string;
2✔
106
        }
107
}
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