-
-
Notifications
You must be signed in to change notification settings - Fork 60
improve versionCompare() #1595
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
improve versionCompare() #1595
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR improves the versionCompare() method in the module class to correctly handle version strings with suffixes like -beta, -alpha, -rc, and -stable. The key improvement is that versions with the same base number are now treated as equal regardless of their suffix, which fixes the issue where 2.5.12-beta was incorrectly rejected when the minimum version was set to 2.5.12.
Key Changes:
- Refactored version comparison to extract and compare only base version numbers (stripping suffixes)
- Versions with identical base numbers are now treated as equal regardless of suffix
- Simplifies the logic by normalizing both versions consistently before comparison
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
htdocs/kernel/module.php
Outdated
| public function versionCompare($version1 = '', $version2 = '', $operator = '<') | ||
| { | ||
| $version1 = strtolower($version1); | ||
| $version2 = strtolower($version2); | ||
| if (false !== strpos($version2, '-stable')) { | ||
| $version2 = substr($version2, 0, strpos($version2, '-stable')); | ||
| } | ||
| if (false !== strpos($version1, '-stable')) { | ||
| $version1 = substr($version1, 0, strpos($version1, '-stable')); | ||
| $normalize = static function ($ver) { | ||
| $ver = strtolower($ver); | ||
| // strip suffix like -beta, -alpha, -rc, -stable | ||
| if (false !== ($pos = strpos($ver, '-'))) { | ||
| $ver = substr($ver, 0, $pos); | ||
| } | ||
| return $ver; | ||
| }; | ||
|
|
||
| $base1 = $normalize($version1); | ||
| $base2 = $normalize($version2); | ||
|
|
||
| // Compare only the base versions first | ||
| $cmp = version_compare($base1, $base2); | ||
|
|
||
| if ($cmp === 0) { | ||
| // Same base version → treat as equal, regardless of suffix | ||
| return version_compare('0', '0', $operator); | ||
| } | ||
| return version_compare($version1, $version2, $operator); | ||
|
|
||
| // Otherwise, just use the base numeric comparison | ||
| return version_compare($base1, $base2, $operator); | ||
| } |
Copilot
AI
Jan 1, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The updated version comparison logic lacks test coverage. Since this repository has existing PHPUnit test infrastructure (tests/unit/), and this is a critical function used for version validation across the system, unit tests should be added to verify the new behavior handles all edge cases correctly, including:
- Same base version with different suffixes (e.g., 2.5.12 vs 2.5.12-beta)
- Different base versions with suffixes (e.g., 2.5.11-stable vs 2.5.12-beta)
- All comparison operators (<, <=, >, >=, ==, !=)
- Edge cases like empty strings or malformed version strings
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
Copilot reviewed 1 out of 1 changed files in this pull request and generated 2 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
htdocs/kernel/module.php
Outdated
| // Normalize legacy "-stable" (keep behavior consistent) | ||
| if (($pos = strpos($ver, '-stable')) !== false) { | ||
| $ver = substr($ver, 0, $pos); | ||
| $ver = rtrim($ver); |
Copilot
AI
Jan 1, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The rtrim($ver) call on line 203 is redundant because line 211 already trims the entire string at the end of the normalization function. Additionally, the initial trim() on line 199 removes leading and trailing whitespace. This extra trim operation has no effect and can be removed.
| $ver = rtrim($ver); |
htdocs/kernel/module.php
Outdated
| // Normalize legacy "-stable" (keep behavior consistent) | ||
| if (($pos = strpos($ver, '-stable')) !== false) { | ||
| $ver = substr($ver, 0, $pos); | ||
| $ver = rtrim($ver); | ||
| } | ||
|
|
||
| // Strip any suffix after first '-': -beta, -alpha, -rc, -dev, etc. |
Copilot
AI
Jan 1, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The special handling for -stable on lines 201-204 is redundant since lines 207-209 already strip any suffix starting with -, which would include -stable. The comment mentions "keep behavior consistent" but the behavior would be identical without this block. Consider removing lines 201-204 to simplify the logic and avoid confusion.
| // Normalize legacy "-stable" (keep behavior consistent) | |
| if (($pos = strpos($ver, '-stable')) !== false) { | |
| $ver = substr($ver, 0, $pos); | |
| $ver = rtrim($ver); | |
| } | |
| // Strip any suffix after first '-': -beta, -alpha, -rc, -dev, etc. | |
| // Strip any suffix after first '-': -beta, -alpha, -rc, -dev, -stable, etc. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
Copilot reviewed 1 out of 1 changed files in this pull request and generated 1 comment.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| } | ||
| return version_compare($version1, $version2, $operator); | ||
|
|
||
| return (bool)version_compare($n1, $n2, $op); |
Copilot
AI
Jan 1, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The explicit cast to (bool) on line 215 is unnecessary. The version_compare() function already returns a boolean when an operator is provided, so the cast is redundant.
| return (bool)version_compare($n1, $n2, $op); | |
| return version_compare($n1, $n2, $op); |
Solves the issue of XOOPS reporting that
2.5.12-betais wrong version when the min. was set for2.5.12