Skip to content

Commit

Permalink
ArrayLoader/ValidatingArrayLoader: handle non-string values for versi…
Browse files Browse the repository at this point in the history
…on/version_normalized (#10470)

Co-authored-by: Jordi Boggiano <j.boggiano@seld.be>
  • Loading branch information
glaubinix and Seldaek committed Jan 21, 2022
1 parent 6b8f140 commit 3b4afaa
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
7 changes: 5 additions & 2 deletions src/Composer/Package/Loader/ArrayLoader.php
Expand Up @@ -113,12 +113,15 @@ 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.');
}
if (!is_string($config['version'])) {
$config['version'] = (string) $config['version'];
}

// handle already normalized versions
if (isset($config['version_normalized'])) {
if (isset($config['version_normalized']) && is_string($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 Down
17 changes: 12 additions & 5 deletions src/Composer/Package/Loader/ValidatingArrayLoader.php
Expand Up @@ -71,11 +71,18 @@ public function load(array $config, $class = 'Composer\Package\CompletePackage')
}

if (!empty($this->config['version'])) {
try {
$this->versionParser->normalize($this->config['version']);
} catch (\Exception $e) {
$this->errors[] = 'version : invalid value ('.$this->config['version'].'): '.$e->getMessage();
unset($this->config['version']);
if (!is_scalar($this->config['version'])) {
$this->validateString('version');
} else {
if (!is_string($this->config['version'])) {
$this->config['version'] = (string) $this->config['version'];
}
try {
$this->versionParser->normalize($this->config['version']);
} catch (\Exception $e) {
$this->errors[] = 'version : invalid value ('.$this->config['version'].'): '.$e->getMessage();
unset($this->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 3b4afaa

Please sign in to comment.