wiki.techinc.nl/includes/composer/ComposerVersionNormalizer.php
Thiemo Kreuz 31aeedb98a Add some more missing limit parameters to explode() calls
I benchmarked this again. The runtime of an unlimited explode() can be
quite high. This is not really a DoS attack vector as it would require to
post megabytes worth of input to the code, which will hit many other
limits before. I still consider it good practice to use unlimited explode()
only when it is actually allowed to return an unlimited amount of elements.

Change-Id: I30f8ca5dba7b317bb4a046b9740fd736b4eea291
2019-04-05 14:34:39 +00:00

66 lines
1.5 KiB
PHP

<?php
/**
* @license GPL-2.0-or-later
* @author Jeroen De Dauw < jeroendedauw@gmail.com >
*/
class ComposerVersionNormalizer {
/**
* Ensures there is a dash in between the version and the stability suffix.
*
* Examples:
* - 1.23RC => 1.23-RC
* - 1.23alpha => 1.23-alpha
* - 1.23alpha3 => 1.23-alpha3
* - 1.23-beta => 1.23-beta
*
* @param string $version
*
* @return string
* @throws InvalidArgumentException
*/
public function normalizeSuffix( $version ) {
if ( !is_string( $version ) ) {
throw new InvalidArgumentException( '$version must be a string' );
}
return preg_replace( '/^(\d[\d\.]*)([a-zA-Z]+)(\d*)$/', '$1-$2$3', $version, 1 );
}
/**
* Ensures the version has four levels.
* Version suffixes are supported, as long as they start with a dash.
*
* Examples:
* - 1.19 => 1.19.0.0
* - 1.19.2.3 => 1.19.2.3
* - 1.19-alpha => 1.19.0.0-alpha
* - 1337 => 1337.0.0.0
*
* @param string $version
*
* @return string
* @throws InvalidArgumentException
*/
public function normalizeLevelCount( $version ) {
if ( !is_string( $version ) ) {
throw new InvalidArgumentException( '$version must be a string' );
}
$dashPosition = strpos( $version, '-' );
if ( $dashPosition !== false ) {
$suffix = substr( $version, $dashPosition );
$version = substr( $version, 0, $dashPosition );
}
$version = implode( '.', array_pad( explode( '.', $version, 4 ), 4, '0' ) );
if ( $dashPosition !== false ) {
$version .= $suffix;
}
return $version;
}
}