Skip to content

Commit abcd28f

Browse files
authored
Merge pull request #262 from PHPCSStandards/feature/tests-phpcsversion-fix-min-max-bug
Tests/PHPCSVersions: bug fix
2 parents 99d3567 + d208afd commit abcd28f

File tree

1 file changed

+68
-23
lines changed

1 file changed

+68
-23
lines changed

tests/PHPCSVersions.php

Lines changed: 68 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ final class PHPCSVersions
3838
* This matches the version constraint in the `composer.json` file of this package.
3939
* {@link https://github.com/PHPCSStandards/composer-installer/pull/152}
4040
*
41-
* @var array
41+
* @var array<string, string>
4242
*/
4343
private static $allPhpcsVersions = array(
4444
'3.1.0' => '3.1.0',
@@ -107,7 +107,7 @@ final class PHPCSVersions
107107
* Defaults to `false`.
108108
* Note: if `true`, the version will be returned in a Composer usable format.
109109
*
110-
* @return array Numerically indexed array with PHPCS version identifiers as values.
110+
* @return array<string> Numerically indexed array with PHPCS version identifiers as values.
111111
*/
112112
public static function get($number = 0, $addMaster = false, $addNextMajor = false)
113113
{
@@ -145,16 +145,16 @@ public static function get($number = 0, $addMaster = false, $addNextMajor = fals
145145
* Defaults to `false`.
146146
* Note: if `true`, the version will be returned in a Composer usable format.
147147
*
148-
* @return array Numerically indexed array with PHPCS version identifiers as values.
148+
* @return array<string> Numerically indexed array with PHPCS version identifiers as values.
149149
*/
150150
public static function getHighLow($addMaster = false, $addNextMajor = false)
151151
{
152152
$versions = self::getSupportedVersions();
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()) {
@@ -180,7 +180,7 @@ public static function getHighLow($addMaster = false, $addNextMajor = false)
180180
* Defaults to `false`.
181181
* Note: if `true`, the version will be returned in a Composer usable format.
182182
*
183-
* @return array Numerically indexed array with PHPCS version identifiers as values.
183+
* @return array<string> Numerically indexed array with PHPCS version identifiers as values.
184184
*/
185185
public static function getHighLowEachMajor($addMaster = false, $addNextMajor = false)
186186
{
@@ -191,27 +191,27 @@ public static function getHighLowEachMajor($addMaster = false, $addNextMajor = f
191191
if (empty($versions) === false) {
192192
$versions3 = array_filter(
193193
$versions,
194-
function ($v) {
194+
static function ($v) {
195195
return $v[0] === '3';
196196
}
197197
);
198198
$versions4 = array_filter(
199199
$versions,
200-
function ($v) {
200+
static function ($v) {
201201
return $v[0] === '4';
202202
}
203203
);
204204
}
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 @@ 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
*
@@ -256,9 +300,10 @@ public static function getRandom($inclMaster = false, $inclNextMajor = false)
256300
/**
257301
* Convert a versions array to an array suitable for use as a PHPUnit dataprovider.
258302
*
259-
* @param array $versions Array with PHPCS version numbers as values.
303+
* @param array<string> $versions Array with PHPCS version numbers as values.
260304
*
261-
* @return array Array of PHPCS version identifiers in a format usable for a test data provider.
305+
* @return array<string, array<string, string>> Array of PHPCS version identifiers in a format usable
306+
* for a test data provider.
262307
*/
263308
public static function toDataprovider($versions)
264309
{
@@ -279,7 +324,7 @@ public static function toDataprovider($versions)
279324
/**
280325
* Retrieve an array with PHPCS versions valid for the current PHP version.
281326
*
282-
* @return array Array with PHPCS version identifiers as both keys and values.
327+
* @return array<string, string> Array with PHPCS version identifiers as both keys and values.
283328
*/
284329
public static function getSupportedVersions()
285330
{
@@ -294,7 +339,7 @@ public static function getSupportedVersions()
294339
case '7.1':
295340
$versions = array_filter(
296341
self::$allPhpcsVersions,
297-
function ($version) {
342+
static function ($version) {
298343
// PHPCS 3.x is the last version still supporting PHP < 7.2.
299344
return version_compare($version, '3.99.99', '<=');
300345
}
@@ -308,7 +353,7 @@ function ($version) {
308353
case '7.3':
309354
$versions = array_filter(
310355
self::$allPhpcsVersions,
311-
function ($version) {
356+
static function ($version) {
312357
// PHPCS 3.3.1 is the first PHPCS version with runtime support for PHP 7.3.
313358
return version_compare($version, '3.3.1', '>=');
314359
}
@@ -318,7 +363,7 @@ function ($version) {
318363
case '7.4':
319364
$versions = array_filter(
320365
self::$allPhpcsVersions,
321-
function ($version) {
366+
static function ($version) {
322367
// PHPCS 3.5.0 is the first PHPCS version with runtime support for PHP 7.4.
323368
return version_compare($version, '3.5.0', '>=');
324369
}
@@ -328,7 +373,7 @@ function ($version) {
328373
case '8.0':
329374
$versions = array_filter(
330375
self::$allPhpcsVersions,
331-
function ($version) {
376+
static function ($version) {
332377
// PHPCS 3.5.7 is the first PHPCS version with runtime support for PHP 8.0.
333378
return version_compare($version, '3.5.7', '>=');
334379
}
@@ -338,7 +383,7 @@ function ($version) {
338383
case '8.1':
339384
$versions = array_filter(
340385
self::$allPhpcsVersions,
341-
function ($version) {
386+
static function ($version) {
342387
// PHPCS 3.6.1 is the first PHPCS version with runtime support for PHP 8.1.
343388
return version_compare($version, '3.6.1', '>=');
344389
}
@@ -348,7 +393,7 @@ function ($version) {
348393
case '8.2':
349394
$versions = array_filter(
350395
self::$allPhpcsVersions,
351-
function ($version) {
396+
static function ($version) {
352397
// PHPCS 3.6.1 is the first PHPCS version with runtime support for PHP 8.2.
353398
return version_compare($version, '3.6.1', '>=');
354399
}
@@ -359,7 +404,7 @@ function ($version) {
359404
case '8.4':
360405
$versions = array_filter(
361406
self::$allPhpcsVersions,
362-
function ($version) {
407+
static function ($version) {
363408
// PHPCS 3.8.0 is the first PHPCS version with runtime support for PHP 8.3.
364409
// And while officially, PHPCS 3.11.0 is the first PHPCS version with runtime support
365410
// for PHP 8.4, for our purposes, we should be fine with PHPCS 3.8.0 or higher.
@@ -412,7 +457,7 @@ public static function isNextMajorSupported()
412457
*
413458
* @param string $version PHPCS version number.
414459
*
415-
* @return array Numerically indexed array of standards, natural sort applied.
460+
* @return array<string> Numerically indexed array of standards, natural sort applied.
416461
*/
417462
public static function getStandards($version)
418463
{

0 commit comments

Comments
 (0)