Completed
Push — add/changelog-tooling ( 86359e...64ad4d )
by
unknown
40:44 queued 31:04
created

SemverVersioning::compareVersions()   C

Complexity

Conditions 14
Paths 13

Size

Total Lines 46

Duplication

Lines 3
Ratio 6.52 %

Importance

Changes 0
Metric Value
cc 14
nc 13
nop 2
dl 3
loc 46
rs 6.2666
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php // phpcs:ignore WordPress.Files.FileName.NotHyphenatedLowercase
2
/**
3
 * Semver versioning plugin.
4
 *
5
 * @package automattic/jetpack-changelogger
6
 */
7
8
namespace Automattic\Jetpack\Changelogger\Plugins;
9
10
use Automattic\Jetpack\Changelogger\PluginTrait;
11
use Automattic\Jetpack\Changelogger\VersioningPlugin;
12
use InvalidArgumentException;
13
14
/**
15
 * Semver versioning plugin.
16
 */
17
class SemverVersioning implements VersioningPlugin {
18
	use PluginTrait;
19
20
	/**
21
	 * Parse a semver version.
22
	 *
23
	 * @param string $version Version.
24
	 * @return array With components:
25
	 *  - major: (int) Major version.
26
	 *  - minor: (int) Minor version.
27
	 *  - patch: (int) Patch version.
28
	 *  - prerelease: (string|null) Pre-release string.
29
	 *  - buildinfo: (string|null) Build metadata string.
30
	 * @throws InvalidArgumentException If the version number is not in a recognized format.
31
	 */
32
	public function parseVersion( $version ) {
33
		// This is slightly looser than the official version from semver.org, in that leading zeros are allowed.
34
		if ( ! preg_match( '/^(?P<major>\d+)\.(?P<minor>\d+)\.(?P<patch>\d+)(?:-(?P<prerelease>(?:[0-9a-zA-Z-]+)(?:\.(?:[0-9a-zA-Z-]+))*))?(?:\+(?P<buildinfo>[0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$/', $version, $m ) ) {
35
			throw new InvalidArgumentException( "Version number \"$version\" is not in a recognized format." );
36
		}
37
		return array(
38
			'major'      => (int) $m['major'],
39
			'minor'      => (int) $m['minor'],
40
			'patch'      => (int) $m['patch'],
41
			'prerelease' => isset( $m['prerelease'] ) && '' !== $m['prerelease'] ? $m['prerelease'] : null,
42
			'buildinfo'  => isset( $m['buildinfo'] ) && '' !== $m['buildinfo'] ? $m['buildinfo'] : null,
43
		);
44
	}
45
46
	/**
47
	 * Check and normalize a version number.
48
	 *
49
	 * @param string|array $version Version string, or array as from `parseVersion()`.
50
	 * @return string Normalized version.
51
	 * @throws InvalidArgumentException If the version number is not in a recognized format.
52
	 */
53
	public function normalizeVersion( $version ) {
54 View Code Duplication
		if ( is_array( $version ) ) {
55
			$info = $version + array(
56
				'prerelease' => null,
57
				'buildinfo'  => null,
58
			);
59
			$test = $this->parseVersion( '0.0.0' );
60
			if ( array_intersect_key( $test, $info ) !== $test ) {
61
				throw new InvalidArgumentException( 'Version array is not in a recognized format.' );
62
			}
63
		} else {
64
			$info = $this->parseVersion( $version );
65
		}
66
67
		$ret = sprintf( '%d.%d.%d', $info['major'], $info['minor'], $info['patch'] );
68
		if ( null !== $info['prerelease'] ) {
69
			$sep = '-';
70
			foreach ( explode( '.', $info['prerelease'] ) as $part ) {
71
				if ( ctype_digit( $part ) ) {
72
					$part = (int) $part;
73
				}
74
				$ret .= $sep . $part;
75
				$sep  = '.';
76
			}
77
		}
78
		if ( null !== $info['buildinfo'] ) {
79
			$ret .= '+' . $info['buildinfo'];
80
		}
81
		return $ret;
82
	}
83
84
	/**
85
	 * Determine the next version given a current version and a set of changes.
86
	 *
87
	 * @param string        $version Current version.
88
	 * @param ChangeEntry[] $changes Changes.
89
	 * @param array         $extra Extra components for the version.
90
	 *  - prerelease: (string|null) Prerelease version, e.g. "dev", "alpha", or "beta", if any. See semver docs for accepted values.
91
	 *  - buildinfo: (string|null) Build info, if any. See semver docs for accepted values.
92
	 * @return string
93
	 * @throws InvalidArgumentException If the version number is not in a recognized format, or other arguments are invalid.
94
	 */
95
	public function nextVersion( $version, array $changes, array $extra = array() ) {
96
		$info = $this->parseVersion( $version );
97
98 View Code Duplication
		if ( isset( $extra['prerelease'] ) ) {
99
			try {
100
				$info['prerelease'] = $this->parseVersion( '0.0.0-' . $extra['prerelease'] )['prerelease'];
101
			} catch ( InvalidArgumentException $ex ) {
102
				throw new InvalidArgumentException( 'Invalid prerelease data' );
103
			}
104
		} else {
105
			$info['prerelease'] = null;
106
		}
107 View Code Duplication
		if ( isset( $extra['buildinfo'] ) ) {
108
			try {
109
				$info['buildinfo'] = $this->parseVersion( '0.0.0+' . $extra['buildinfo'] )['buildinfo'];
110
			} catch ( InvalidArgumentException $ex ) {
111
				throw new InvalidArgumentException( 'Invalid buildinfo data' );
112
			}
113
		} else {
114
			$info['buildinfo'] = null;
115
		}
116
117
		$significances = array();
118
		foreach ( $changes as $change ) {
119
			$significances[ (string) $change->getSignificance() ] = true;
120
		}
121
		if ( isset( $significances['major'] ) ) {
122
			$info['patch'] = 0;
123
			if ( 0 === (int) $info['major'] ) {
124
				if ( is_callable( array( $this->output, 'getErrorOutput' ) ) ) {
125
					$out = $this->output->getErrorOutput();
126
					$out->writeln( '<warning>Semver does not automatically move version 0.y.z to 1.0.0.</>' );
127
					$out->writeln( '<warning>You will have to do that manually when you\'re ready for the first release.</>' );
128
				}
129
				$info['minor']++;
130
			} else {
131
				$info['minor'] = 0;
132
				$info['major']++;
133
			}
134
		} elseif ( isset( $significances['minor'] ) ) {
135
			$info['patch'] = 0;
136
			$info['minor']++;
137
		} else {
138
			$info['patch']++;
139
		}
140
141
		return $this->normalizeVersion( $info );
142
	}
143
144
	/**
145
	 * Compare two version numbers.
146
	 *
147
	 * @param string $a First version.
148
	 * @param string $b Second version.
149
	 * @return int Less than, equal to, or greater than 0 depending on whether `$a` is less than, equal to, or greater than `$b`.
150
	 * @throws InvalidArgumentException If the version numbers are not in a recognized format.
151
	 */
152
	public function compareVersions( $a, $b ) {
153
		$aa = $this->parseVersion( $a );
154
		$bb = $this->parseVersion( $b );
155
		if ( $aa['major'] !== $bb['major'] ) {
156
			return $aa['major'] - $bb['major'];
157
		}
158
		if ( $aa['minor'] !== $bb['minor'] ) {
159
			return $aa['minor'] - $bb['minor'];
160
		}
161 View Code Duplication
		if ( $aa['patch'] !== $bb['patch'] ) {
162
			return $aa['patch'] - $bb['patch'];
163
		}
164
165
		if ( null === $aa['prerelease'] ) {
166
			return null === $bb['prerelease'] ? 0 : 1;
167
		}
168
		if ( null === $bb['prerelease'] ) {
169
			return -1;
170
		}
171
172
		$aaa = explode( '.', $aa['prerelease'] );
173
		$bbb = explode( '.', $bb['prerelease'] );
174
		$al  = count( $aaa );
175
		$bl  = count( $bbb );
176
		for ( $i = 0; $i < $al && $i < $bl; $i++ ) {
177
			$a = $aaa[ $i ];
178
			$b = $bbb[ $i ];
179
			if ( ctype_digit( $a ) ) {
180
				if ( ctype_digit( $b ) ) {
181
					if ( (int) $a !== (int) $b ) {
182
						return $a - $b;
183
					}
184
				} else {
185
					return -1;
186
				}
187
			} elseif ( ctype_digit( $b ) ) {
188
				return 1;
189
			} else {
190
				$tmp = strcmp( $a, $b );
191
				if ( 0 !== $tmp ) {
192
					return $tmp;
193
				}
194
			}
195
		}
196
		return $al - $bl;
197
	}
198
199
}
200