Skip to content

Commit

Permalink
Fix handling of non-string values, fixes #134
Browse files Browse the repository at this point in the history
  • Loading branch information
Seldaek committed Apr 1, 2022
1 parent 7b62ddc commit 9b2d75f
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions src/VersionParser.php
Expand Up @@ -51,7 +51,7 @@ class VersionParser
*/
public static function parseStability($version)
{
$version = (string) preg_replace('{#.+$}', '', $version);
$version = (string) preg_replace('{#.+$}', '', (string) $version);

if (strpos($version, 'dev-') === 0 || '-dev' === substr($version, -4)) {
return 'dev';
Expand Down Expand Up @@ -85,7 +85,7 @@ public static function parseStability($version)
*/
public static function normalizeStability($stability)
{
$stability = strtolower($stability);
$stability = strtolower((string) $stability);

return $stability === 'rc' ? 'RC' : $stability;
}
Expand All @@ -94,15 +94,15 @@ public static function normalizeStability($stability)
* Normalizes a version string to be able to perform comparisons on it.
*
* @param string $version
* @param string $fullVersion optional complete version string to give more context
* @param ?string $fullVersion optional complete version string to give more context
*
* @throws \UnexpectedValueException
*
* @return string
*/
public function normalize($version, $fullVersion = null)
{
$version = trim($version);
$version = trim((string) $version);
$origVersion = $version;
if (null === $fullVersion) {
$fullVersion = $version;
Expand Down Expand Up @@ -195,7 +195,7 @@ public function normalize($version, $fullVersion = null)
*/
public function parseNumericAliasPrefix($branch)
{
if (preg_match('{^(?P<version>(\d++\\.)*\d++)(?:\.x)?-dev$}i', $branch, $matches)) {
if (preg_match('{^(?P<version>(\d++\\.)*\d++)(?:\.x)?-dev$}i', (string) $branch, $matches)) {
return $matches['version'] . '.';
}

Expand All @@ -211,7 +211,7 @@ public function parseNumericAliasPrefix($branch)
*/
public function normalizeBranch($name)
{
$name = trim($name);
$name = trim((string) $name);

if (preg_match('{^v?(\d++)(\.(?:\d++|[xX*]))?(\.(?:\d++|[xX*]))?(\.(?:\d++|[xX*]))?$}i', $name, $matches)) {
$version = '';
Expand All @@ -231,14 +231,16 @@ public function normalizeBranch($name)
* @param string $name
*
* @return string
*
* @deprecated No need to use this anymore in theory, Composer 2 does not normalize any branch names to 9999999-dev anymore
*/
public function normalizeDefaultBranch($name)
{
if ($name === 'dev-master' || $name === 'dev-default' || $name === 'dev-trunk') {
return '9999999-dev';
}

return $name;
return (string) $name;
}

/**
Expand All @@ -250,9 +252,9 @@ public function normalizeDefaultBranch($name)
*/
public function parseConstraints($constraints)
{
$prettyConstraint = $constraints;
$prettyConstraint = (string) $constraints;

$orConstraints = preg_split('{\s*\|\|?\s*}', trim($constraints));
$orConstraints = preg_split('{\s*\|\|?\s*}', trim((string) $constraints));
if (false === $orConstraints) {
throw new \RuntimeException('Failed to preg_split string: '.$constraints);
}
Expand Down

0 comments on commit 9b2d75f

Please sign in to comment.