diff --git a/classes/ChannelInfo.php b/classes/ChannelInfo.php index c5eaf55aba..9e696e0b74 100644 --- a/classes/ChannelInfo.php +++ b/classes/ChannelInfo.php @@ -46,8 +46,7 @@ public function __construct(Upgrader $upgrader, UpgradeConfiguration $config, st $this->channel = $channel; $publicChannels = ['minor', 'major', 'rc', 'beta', 'alpha']; - preg_match('#([0-9]+\.[0-9]+)(?:\.[0-9]+){1,2}#', _PS_VERSION_, $matches); - $upgrader->branch = $matches[1]; + $upgrader->branch = VersionUtils::splitPrestaShopVersion(_PS_VERSION_)['major']; $upgrader->channel = $channel; if (in_array($channel, $publicChannels)) { diff --git a/classes/TaskRunner/Miscellaneous/CompareReleases.php b/classes/TaskRunner/Miscellaneous/CompareReleases.php index 875db8caa6..018cda4080 100644 --- a/classes/TaskRunner/Miscellaneous/CompareReleases.php +++ b/classes/TaskRunner/Miscellaneous/CompareReleases.php @@ -30,6 +30,7 @@ use Exception; use PrestaShop\Module\AutoUpgrade\Parameters\UpgradeFileNames; use PrestaShop\Module\AutoUpgrade\TaskRunner\AbstractTask; +use PrestaShop\Module\AutoUpgrade\VersionUtils; use PrestaShop\Module\AutoUpgrade\TaskRunner\ExitCode; /** @@ -56,8 +57,7 @@ public function run(): int $version = $this->container->getUpgradeConfiguration()->get('directory.version_num'); break; default: - preg_match('#([0-9]+\.[0-9]+)(?:\.[0-9]+){1,2}#', _PS_VERSION_, $matches); - $upgrader->branch = $matches[1]; + $upgrader->branch = VersionUtils::splitPrestaShopVersion(_PS_VERSION_)['major']; $upgrader->channel = $channel; if ($this->container->getUpgradeConfiguration()->get('channel') == 'private' && !$this->container->getUpgradeConfiguration()->get('private_allow_major')) { $upgrader->checkPSVersion(false, ['private', 'minor']); diff --git a/classes/TaskRunner/Upgrade/Download.php b/classes/TaskRunner/Upgrade/Download.php index de3b0425d2..08581ac2de 100644 --- a/classes/TaskRunner/Upgrade/Download.php +++ b/classes/TaskRunner/Upgrade/Download.php @@ -32,6 +32,7 @@ use PrestaShop\Module\AutoUpgrade\TaskRunner\ExitCode; use PrestaShop\Module\AutoUpgrade\UpgradeContainer; use PrestaShop\Module\AutoUpgrade\UpgradeTools\FilesystemAdapter; +use PrestaShop\Module\AutoUpgrade\VersionUtils; /** * Download PrestaShop archive according to the chosen channel. @@ -51,10 +52,8 @@ public function run(): int } $upgrader = $this->container->getUpgrader(); - // regex optimization - preg_match('#([0-9]+\.[0-9]+)(?:\.[0-9]+){1,2}#', _PS_VERSION_, $matches); $upgrader->channel = $this->container->getUpgradeConfiguration()->get('channel'); - $upgrader->branch = $matches[1]; + $upgrader->branch = VersionUtils::splitPrestaShopVersion(_PS_VERSION_)['major']; if ($this->container->getUpgradeConfiguration()->get('channel') == 'private' && !$this->container->getUpgradeConfiguration()->get('private_allow_major')) { $upgrader->checkPSVersion(false, ['private', 'minor']); } else { diff --git a/classes/TaskRunner/Upgrade/UpgradeNow.php b/classes/TaskRunner/Upgrade/UpgradeNow.php index 694ecf2a1f..9cd50a57fb 100644 --- a/classes/TaskRunner/Upgrade/UpgradeNow.php +++ b/classes/TaskRunner/Upgrade/UpgradeNow.php @@ -30,6 +30,7 @@ use Exception; use PrestaShop\Module\AutoUpgrade\TaskRunner\AbstractTask; use PrestaShop\Module\AutoUpgrade\TaskRunner\ExitCode; +use PrestaShop\Module\AutoUpgrade\VersionUtils; /** * very first step of the upgrade process. The only thing done is the selection @@ -49,8 +50,8 @@ public function run(): int $channel = $this->container->getUpgradeConfiguration()->get('channel'); $upgrader = $this->container->getUpgrader(); $this->next = 'download'; - preg_match('#([0-9]+\.[0-9]+)(?:\.[0-9]+){1,2}#', _PS_VERSION_, $matches); - $upgrader->branch = $matches[1]; + + $upgrader->branch = VersionUtils::splitPrestaShopVersion(_PS_VERSION_)['major']; $upgrader->channel = $channel; if ($this->container->getUpgradeConfiguration()->get('channel') == 'private' && !$this->container->getUpgradeConfiguration()->get('private_allow_major')) { $upgrader->checkPSVersion(false, ['private', 'minor']); diff --git a/classes/UpgradeContainer.php b/classes/UpgradeContainer.php index f937b4db13..b01f5d4a2a 100644 --- a/classes/UpgradeContainer.php +++ b/classes/UpgradeContainer.php @@ -324,8 +324,7 @@ public function getUpgrader(): Upgrader $this->getProperty(self::PS_VERSION), $fileLoader ); - preg_match('#([0-9]+\.[0-9]+)(?:\.[0-9]+){1,2}#', $this->getProperty(self::PS_VERSION), $matches); - $upgrader->branch = $matches[1]; + $upgrader->branch = VersionUtils::splitPrestaShopVersion($this->getProperty(self::PS_VERSION))['major']; $upgradeConfiguration = $this->getUpgradeConfiguration(); $channel = $upgradeConfiguration->get('channel'); switch ($channel) { diff --git a/classes/UpgradeSelfCheck.php b/classes/UpgradeSelfCheck.php index 7820419165..ea18b0cd96 100644 --- a/classes/UpgradeSelfCheck.php +++ b/classes/UpgradeSelfCheck.php @@ -454,13 +454,13 @@ public function getPhpCompatibilityRange(): ?array } if (version_compare($targetVersion, '8', '<')) { - $targetVersion = VersionUtils::getPrestashopMinorVersion($targetVersion); + $targetMinorVersion = VersionUtils::splitPrestaShopVersion($targetVersion)['minor']; - if (!isset($this::PRESTASHOP_17_PHP_REQUIREMENTS[$targetVersion])) { + if (!isset($this::PRESTASHOP_17_PHP_REQUIREMENTS[$targetMinorVersion])) { return null; } - $range = $this::PRESTASHOP_17_PHP_REQUIREMENTS[$targetVersion]; + $range = $this::PRESTASHOP_17_PHP_REQUIREMENTS[$targetMinorVersion]; } else { try { $range = $this->distributionApiService->getPhpVersionRequirements($targetVersion); diff --git a/classes/VersionUtils.php b/classes/VersionUtils.php index f8cd7d4ef4..f321c5edb1 100644 --- a/classes/VersionUtils.php +++ b/classes/VersionUtils.php @@ -115,9 +115,9 @@ public static function isActualPHPVersionCompatible() /** * @param string $version * - * @return string + * @return array{'major': string,'minor': string,'patch': string}|null */ - public static function getPrestashopMinorVersion($version) + public static function splitPrestaShopVersion($version) { if (!is_string($version)) { throw new InvalidArgumentException('Version must be a string.'); @@ -128,18 +128,20 @@ public static function getPrestashopMinorVersion($version) throw new InvalidArgumentException('Version string cannot be empty.'); } - if (!preg_match('/^\d+\.\d+\.\d+(\.\d+)?$/', $version)) { - throw new InvalidArgumentException('Invalid version format. Expected format: X.Y.Z or X.Y.Z.W'); - } + preg_match( + '#^(?(?(?([0-1]{1}\.[0-9]+)|([0-9]+))(?:\.[0-9]+){1})(?:\.[0-9]+){1})$#', + $version, + $matches + ); - $parts = explode('.', $version); - $versionString = implode('.', $parts); - if (strlen($versionString) >= 8) { - $minorVersionParts = array_slice($parts, 0, 3); - } else { - $minorVersionParts = array_slice($parts, 0, 2); + if (empty($matches)) { + throw new InvalidArgumentException('Invalid version format. Expected format: X.Y.Z or X.Y.Z.W'); } - return implode('.', $minorVersionParts); + return [ + 'major' => $matches['major'], + 'minor' => $matches['minor'], + 'patch' => $matches['patch'], + ]; } } diff --git a/tests/unit/VersionUtilsTest.php b/tests/unit/VersionUtilsTest.php index a11ad6d601..50025e5bd4 100644 --- a/tests/unit/VersionUtilsTest.php +++ b/tests/unit/VersionUtilsTest.php @@ -103,23 +103,26 @@ public function testGetPhpMajorMinorVersionId() $this->assertSame(PHP_MAJOR_VERSION * 10000 + PHP_MINOR_VERSION * 100, $version); } - public function testGetPrestashopMinorVersion() + /** + * @dataProvider providerOfPrestaShopVersions + */ + public function testSplitOfPrestaShopVersion(string $inputVersion, array $expected) { - $version = VersionUtils::getPrestashopMinorVersion('1.7.8.11'); + $version = VersionUtils::splitPrestaShopVersion($inputVersion); - $this->assertSame('1.7.8', $version); - - $version = VersionUtils::getPrestashopMinorVersion('8.1.5'); - - $this->assertSame('8.1', $version); - - $version = VersionUtils::getPrestashopMinorVersion('8.1.5'); - - $this->assertSame('8.1', $version); - - $version = VersionUtils::getPrestashopMinorVersion('10.1.5'); + $this->assertEquals($expected, $version); + } - $this->assertSame('10.1', $version); + public function providerOfPrestaShopVersions() + { + return [ + ['1.6.1.12', ['major' => '1.6', 'minor' => '1.6.1', 'patch' => '1.6.1.12']], + ['1.7.8.11', ['major' => '1.7', 'minor' => '1.7.8', 'patch' => '1.7.8.11']], + ['8.1.5', ['major' => '8', 'minor' => '8.1', 'patch' => '8.1.5']], + ['8.1.5', ['major' => '8', 'minor' => '8.1', 'patch' => '8.1.5']], + ['9.0.0', ['major' => '9', 'minor' => '9.0', 'patch' => '9.0.0']], + ['10.1.5', ['major' => '10', 'minor' => '10.1', 'patch' => '10.1.5']], + ]; } public function testGetPrestashopMinorVersionFailForBadType() @@ -127,7 +130,7 @@ public function testGetPrestashopMinorVersionFailForBadType() $this->expectException(InvalidArgumentException::class); $this->expectExceptionMessage('Version must be a string.'); - VersionUtils::getPrestashopMinorVersion(1); + VersionUtils::splitPrestaShopVersion(1)['minor']; } public function testGetPrestashopMinorVersionFailForEmptyString() @@ -135,7 +138,7 @@ public function testGetPrestashopMinorVersionFailForEmptyString() $this->expectException(InvalidArgumentException::class); $this->expectExceptionMessage('Version string cannot be empty.'); - VersionUtils::getPrestashopMinorVersion(''); + VersionUtils::splitPrestaShopVersion('')['minor']; } public function testGetPrestashopMinorVersionFailForIncorrectEntryFormatV1() @@ -143,7 +146,7 @@ public function testGetPrestashopMinorVersionFailForIncorrectEntryFormatV1() $this->expectException(InvalidArgumentException::class); $this->expectExceptionMessage('Invalid version format. Expected format: X.Y.Z or X.Y.Z.W'); - VersionUtils::getPrestashopMinorVersion('1'); + VersionUtils::splitPrestaShopVersion('1')['minor']; } public function testGetPrestashopMinorVersionFailForIncorrectEntryFormatV2() @@ -151,6 +154,6 @@ public function testGetPrestashopMinorVersionFailForIncorrectEntryFormatV2() $this->expectException(InvalidArgumentException::class); $this->expectExceptionMessage('Invalid version format. Expected format: X.Y.Z or X.Y.Z.W'); - VersionUtils::getPrestashopMinorVersion('1.7.7.10.1'); + VersionUtils::splitPrestaShopVersion('1.7.7.10.1')['minor']; } }