Skip to content

Commit

Permalink
ArrayLoader: handle non string values for version/version_normalized
Browse files Browse the repository at this point in the history
  • Loading branch information
glaubinix committed Jan 19, 2022
1 parent 6a70161 commit 119a7de
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/Composer/Package/Loader/ArrayLoader.php
Expand Up @@ -113,12 +113,12 @@ private function createObject(array $config, $class)
if (!isset($config['name'])) {
throw new \UnexpectedValueException('Unknown package has no name defined ('.json_encode($config).').');
}
if (!isset($config['version'])) {
if (!isset($config['version']) || !is_scalar($config['version'])) {
throw new \UnexpectedValueException('Package '.$config['name'].' has no version defined.');
}

// handle already normalized versions
if (isset($config['version_normalized'])) {
if (isset($config['version_normalized']) && is_scalar($config['version_normalized'])) {
$version = $config['version_normalized'];

// handling of existing repos which need to remain composer v1 compatible, in case the version_normalized contained VersionParser::DEFAULT_BRANCH_ALIAS, we renormalize it
Expand All @@ -129,7 +129,7 @@ private function createObject(array $config, $class)
$version = $this->versionParser->normalize($config['version']);
}

return new $class($config['name'], $version, $config['version']);
return new $class($config['name'], (string) $version, (string) $config['version']);
}

/**
Expand Down
11 changes: 11 additions & 0 deletions tests/Composer/Test/Package/Loader/ArrayLoaderTest.php
Expand Up @@ -314,4 +314,15 @@ public function testPluginApiVersionDoesSupportSelfVersion()
$this->assertArrayHasKey('composer-plugin-api', $links);
$this->assertSame('6.6.6', $links['composer-plugin-api']->getConstraint()->getPrettyString());
}

public function testNoneStringVersion()
{
$config = array(
'name' => 'acme/package',
'version' => 1,
);

$package = $this->loader->load($config);
$this->assertSame('1', $package->getPrettyVersion());
}
}

0 comments on commit 119a7de

Please sign in to comment.