Skip to content

Commit d208afd

Browse files
committed
Tests/PHPCSVersions: bug fix
The PHP native `min()` and `max()` versions were used to retrieve the lowest/highest PHPCS version from arrays of PHPCS versions. This worked fine at the time this class was created as none of the supported PHPCS versions had a `*.10` minor (or higher) at the time. However, this logic was broken as soon as the `3.10.0` release was tagged - see https://3v4l.org/XbHOY - , we just never realized until now. Fixed now by adding custom `min`/`max` methods to the class.
1 parent 0bfb2b8 commit d208afd

File tree

1 file changed

+50
-6
lines changed

1 file changed

+50
-6
lines changed

tests/PHPCSVersions.php

Lines changed: 50 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,8 @@ public static function getHighLow($addMaster = false, $addNextMajor = false)
153153
$selection = array();
154154

155155
if (empty($versions) === false) {
156-
$selection[] = min($versions);
157-
$selection[] = max($versions);
156+
$selection[] = self::min($versions);
157+
$selection[] = self::max($versions);
158158
}
159159

160160
if ($addMaster === true && self::isDevSupported()) {
@@ -205,13 +205,13 @@ static function ($v) {
205205

206206
$selection = array();
207207
if (empty($versions3) === false) {
208-
$selection[] = min($versions3);
209-
$selection[] = max($versions3);
208+
$selection[] = self::min($versions3);
209+
$selection[] = self::max($versions3);
210210
}
211211

212212
if (empty($versions4) === false) {
213-
$selection[] = min($versions4);
214-
$selection[] = max($versions4);
213+
$selection[] = self::min($versions4);
214+
$selection[] = self::max($versions4);
215215
}
216216

217217
if ($addMaster === true && self::isDevSupported()) {
@@ -225,6 +225,50 @@ static function ($v) {
225225
return $selection;
226226
}
227227

228+
/**
229+
* Find the lowest version in an array of PHPCS versions.
230+
*
231+
* @param array<string> List of PHPCS version identifiers.
232+
*
233+
* @return string The version identifier of the lowest PHPCS version in the list.
234+
*/
235+
public static function min($versions)
236+
{
237+
return array_reduce(
238+
$versions,
239+
static function ($carry, $item) {
240+
if ($carry === null) {
241+
// First iteration.
242+
return $item;
243+
}
244+
245+
return version_compare($carry, $item, '<') ? $carry : $item;
246+
}
247+
);
248+
}
249+
250+
/**
251+
* Find the highest version in an array of PHPCS versions.
252+
*
253+
* @param array<string> List of PHPCS version identifiers.
254+
*
255+
* @return string The version identifier of the highest PHPCS version in the list.
256+
*/
257+
public static function max($versions)
258+
{
259+
return array_reduce(
260+
$versions,
261+
static function ($carry, $item) {
262+
if ($carry === null) {
263+
// First iteration.
264+
return $item;
265+
}
266+
267+
return version_compare($carry, $item, '>') ? $carry : $item;
268+
}
269+
);
270+
}
271+
228272
/**
229273
* Get a random PHPCS version which is valid for the current PHP version.
230274
*

0 commit comments

Comments
 (0)