Skip to content

Commit

Permalink
Refactor Version class
Browse files Browse the repository at this point in the history
  • Loading branch information
pascalchevrel committed Apr 2, 2024
1 parent 3a1c0a8 commit fcf172d
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 22 deletions.
4 changes: 2 additions & 2 deletions app/classes/ReleaseInsights/Release.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function __construct(
public readonly string $product_details = 'https://product-details.mozilla.org/1.0/',
)
{
$this->version = Version::get($version);
$this->version = (new Version($version))->normalized;
}

/**
Expand Down Expand Up @@ -85,7 +85,7 @@ public function getSchedule(): array

if (! in_array( (int) $this->version, $this->no_planned_dot_releases)) {
$schedule += ['planned_dot_release' => $date($release->modify('+2 weeks 00:00'))];
}
}

// Sort the schedule by date, needed for schedules with a fixup
asort($schedule);
Expand Down
22 changes: 15 additions & 7 deletions app/classes/ReleaseInsights/Version.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,21 @@

class Version
{
public readonly string $short;

/**
* Get the major version number (91) from a string such as 91.0.1
*/
public readonly int $int;
public readonly string $normalized;

public function __construct(public readonly string $version)
{
$this->short = explode('.', $version)[0];
$this->int = (int) $this->short;
$this->normalized = $this->get($version);
}

/**
* Get the version number provided by the user in the query string
* via the $_GET['version'] global and return a sanitized for a major
Expand Down Expand Up @@ -40,13 +55,6 @@ public static function get(?string $version = null): string
return (string) number_format(abs((int) $version), 1, '.', '');
}

/**
* Get the major version number (91) from a string such as 91.0.1
*/
public static function getMajor(string $version): int
{
return (int) explode('.', $version)[0];
}

/**
* Decrement a version number (91) provided as a string such as 91.0
Expand Down
2 changes: 1 addition & 1 deletion app/models/calendar.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

$future += [
$version => [
'version' => Version::getMajor($version),
'version' => (new Version($version))->int,
'release_date' => $date,
'nightly_start' => $version_data['nightly_start'],
'soft_freeze' => $version_data['soft_code_freeze'],
Expand Down
2 changes: 1 addition & 1 deletion app/models/esr_release.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

foreach ($upcoming_releases as $k => $v) {
$esr_calendar[] = [
'release' => Version::getMajor($k),
'release' => (new Version($k))->int,
'esr' => ESR::getMainDotVersion(ESR::getVersion((int) $k)),
'old_esr' => is_null(ESR::getOlderSupportedVersion((int) $k))
? ''
Expand Down
3 changes: 2 additions & 1 deletion app/models/home.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@

$latest_nightly = $latest_nightly['platforms']['WINNT_x86_64-msvc']['locales']['en-US']['buildID'] ?? 'N/A';

$beta_is_the_next_ESR = Version::getMajor(FIREFOX_BETA) == (int) ESR::getVersion(Version::getMajor(FIREFOX_BETA));
$beta_version = (new Version(FIREFOX_BETA))->int;
$beta_is_the_next_ESR = $beta_version == (int) ESR::getVersion($beta_version);

return [
$beta_cycle_dates,
Expand Down
10 changes: 0 additions & 10 deletions tests/Unit/ReleaseInsights/VersionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,6 @@

});

test('Version::getMajor', function ($input, $output) {
expect($output)->toEqual(Version::getMajor($input));
})->with([
['91.1.0', 91],
['100', 100],
['100.5', 100],
['78.0.3', 78],
]);


test('Version::decrement', function ($version, $number, $output) {
expect($output)->toEqual(Version::decrement($version, $number));
})->with([
Expand Down

0 comments on commit fcf172d

Please sign in to comment.