diff --git a/core/modules/update/src/ModuleVersion.php b/core/modules/update/src/ModuleVersion.php index 84d372f3c5..2982ca14ef 100644 --- a/core/modules/update/src/ModuleVersion.php +++ b/core/modules/update/src/ModuleVersion.php @@ -17,11 +17,32 @@ class ModuleVersion { protected $version; /** - * The version, without the core compatibility prefix, split apart by commas. + * The major version. * - * @var array + * @var string + */ + protected $majorVersion; + + /** + * The minor version. + * + * @var string|null */ - protected $versionParts; + protected $minorVersion; + + /** + * The patch version. + * + * @var string|null + */ + protected $patchVersion; + + /** + * The version extra string. + * + * @var string|null + */ + protected $versionExtra; /** * Constructs a ModuleVersion object. @@ -31,7 +52,21 @@ class ModuleVersion { */ public function __construct($version) { $this->version = $version; - $this->versionParts = explode('.', $this->getVersionStringWithoutCoreCompatibility()); + $version_parts = explode('.', $this->getVersionStringWithoutCoreCompatibility()); + $this->majorVersion = $version_parts[0]; + if (count($version_parts) === 2) { + $last_version_part = $version_parts[1]; + $this->minorVersion = NULL; + } + else { + $last_version_part = $version_parts[2]; + $this->minorVersion = $version_parts[1]; + } + $last_version_split = explode('-', $last_version_part); + // If patch equals 'x' this instance was created from a branch and the patch + // version cannot be determined. + $this->patchVersion = $last_version_split[0] === 'x' ? NULL : $last_version_split[0]; + $this->versionExtra = count($last_version_split) === 1 ? NULL : $last_version_split[1]; } /** @@ -47,7 +82,7 @@ public function __construct($version) { * The module version instance. */ public static function createFromSupportBranch($branch) { - return new static ($branch . 'x'); + return new static($branch . 'x'); } /** @@ -57,7 +92,7 @@ public static function createFromSupportBranch($branch) { * The major version. */ public function getMajorVersion() { - return $this->versionParts[0]; + return $this->majorVersion; } /** @@ -67,7 +102,7 @@ public function getMajorVersion() { * The minor version if available otherwise NULL. */ public function getMinorVersion() { - return count($this->versionParts) === 2 ? NULL : $this->versionParts[1]; + return $this->minorVersion; } /** @@ -77,11 +112,7 @@ public function getMinorVersion() { * The patch version. */ public function getPatchVersion() { - $last_version_part = count($this->versionParts) === 2 ? $this->versionParts[1] : $this->versionParts[2]; - $patch = explode('-', $last_version_part)[0]; - // If patch equals 'x' this instance was created from a branch and the patch - // version cannot be determined. - return $patch === 'x' ? NULL : $patch; + return $this->patchVersion; } /** @@ -91,8 +122,7 @@ public function getPatchVersion() { * The version string. */ private function getVersionStringWithoutCoreCompatibility() { - $version = strpos($this->version, \Drupal::CORE_COMPATIBILITY) === 0 ? str_replace('8.x-', '', $this->version) : $this->version; - return $version; + return strpos($this->version, \Drupal::CORE_COMPATIBILITY) === 0 ? str_replace('8.x-', '', $this->version) : $this->version; } /** @@ -102,8 +132,7 @@ private function getVersionStringWithoutCoreCompatibility() { * The version extra string if available otherwise NULL. */ public function getVersionExtra() { - $last_version_parts = explode('-', count($this->versionParts) === 2 ? $this->versionParts[1] : $this->versionParts[2]); - return count($last_version_parts) === 1 ? NULL : $last_version_parts[1]; + return $this->versionExtra; } /** diff --git a/core/modules/update/tests/src/Unit/ModuleVersionTest.php b/core/modules/update/tests/src/Unit/ModuleVersionTest.php index 75d427ffb5..dfe0ca8a26 100644 --- a/core/modules/update/tests/src/Unit/ModuleVersionTest.php +++ b/core/modules/update/tests/src/Unit/ModuleVersionTest.php @@ -17,9 +17,9 @@ class ModuleVersionTest extends UnitTestCase { * * @dataProvider providerVersionInfos */ - public function testGetMajorVersion($version, $excepted_version_info) { + public function testGetMajorVersion($version, $expected_version_info) { $version = new ModuleVersion($version); - $this->assertSame($excepted_version_info['major'], $version->getMajorVersion()); + $this->assertSame($expected_version_info['major'], $version->getMajorVersion()); } /** @@ -27,9 +27,9 @@ public function testGetMajorVersion($version, $excepted_version_info) { * * @dataProvider providerVersionInfos */ - public function testGetMinorVersion($version, $excepted_version_info) { + public function testGetMinorVersion($version, $expected_version_info) { $version = new ModuleVersion($version); - $this->assertSame($excepted_version_info['minor'], $version->getMinorVersion()); + $this->assertSame($expected_version_info['minor'], $version->getMinorVersion()); } /** @@ -37,9 +37,9 @@ public function testGetMinorVersion($version, $excepted_version_info) { * * @dataProvider providerVersionInfos */ - public function testGetPatchVersion($version, $excepted_version_info) { + public function testGetPatchVersion($version, $expected_version_info) { $version = new ModuleVersion($version); - $this->assertSame($excepted_version_info['patch'], $version->getPatchVersion()); + $this->assertSame($expected_version_info['patch'], $version->getPatchVersion()); } /** @@ -47,9 +47,9 @@ public function testGetPatchVersion($version, $excepted_version_info) { * * @dataProvider providerVersionInfos */ - public function testGetVersionExtra($version, $excepted_version_info) { + public function testGetVersionExtra($version, $expected_version_info) { $version = new ModuleVersion($version); - $this->assertSame($excepted_version_info['extra'], $version->getVersionExtra()); + $this->assertSame($expected_version_info['extra'], $version->getVersionExtra()); } /** @@ -57,9 +57,9 @@ public function testGetVersionExtra($version, $excepted_version_info) { * * @dataProvider providerVersionInfos */ - public function testGetSupportBranch($version, $excepted_version_info) { + public function testGetSupportBranch($version, $expected_version_info) { $version = new ModuleVersion($version); - $this->assertSame($excepted_version_info['branch'], $version->getSupportBranch()); + $this->assertSame($expected_version_info['branch'], $version->getSupportBranch()); } /** @@ -67,11 +67,11 @@ public function testGetSupportBranch($version, $excepted_version_info) { * * @dataProvider providerVersionInfos */ - public function testCreateFromSupportBranch($version, $excepted_version_info) { - $version = ModuleVersion::createFromSupportBranch($excepted_version_info['branch']); + public function testCreateFromSupportBranch($version, $expected_version_info) { + $version = ModuleVersion::createFromSupportBranch($expected_version_info['branch']); $this->assertInstanceOf(ModuleVersion::class, $version); - $this->assertSame($excepted_version_info['major'], $version->getMajorVersion()); - $this->assertSame($excepted_version_info['minor'], $version->getMinorVersion()); + $this->assertSame($expected_version_info['major'], $version->getMajorVersion()); + $this->assertSame($expected_version_info['minor'], $version->getMinorVersion()); // Version extra and Patch version can't be determined from a branch. $this->assertSame(NULL, $version->getVersionExtra()); $this->assertSame(NULL, $version->getPatchVersion()); @@ -145,6 +145,16 @@ public function providerVersionInfos() { 'branch' => '1.2.', ], ], + '1.2.x' => [ + '1.2.x', + [ + 'major' => '1', + 'minor' => '2', + 'patch' => NULL, + 'extra' => NULL, + 'branch' => '1.2.', + ], + ], ]; }