From 3b4afaa9e35cc6c028600b70b252587b61e82e70 Mon Sep 17 00:00:00 2001 From: Stephan Date: Fri, 21 Jan 2022 13:57:28 +0000 Subject: [PATCH] ArrayLoader/ValidatingArrayLoader: handle non-string values for version/version_normalized (#10470) Co-authored-by: Jordi Boggiano --- src/Composer/Package/Loader/ArrayLoader.php | 7 +++++-- .../Package/Loader/ValidatingArrayLoader.php | 17 ++++++++++++----- .../Test/Package/Loader/ArrayLoaderTest.php | 11 +++++++++++ 3 files changed, 28 insertions(+), 7 deletions(-) diff --git a/src/Composer/Package/Loader/ArrayLoader.php b/src/Composer/Package/Loader/ArrayLoader.php index f7f677e1b..bc4a14139 100644 --- a/src/Composer/Package/Loader/ArrayLoader.php +++ b/src/Composer/Package/Loader/ArrayLoader.php @@ -113,12 +113,15 @@ class ArrayLoader implements LoaderInterface 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 diff --git a/src/Composer/Package/Loader/ValidatingArrayLoader.php b/src/Composer/Package/Loader/ValidatingArrayLoader.php index 3b180139d..7fb2c3b79 100644 --- a/src/Composer/Package/Loader/ValidatingArrayLoader.php +++ b/src/Composer/Package/Loader/ValidatingArrayLoader.php @@ -71,11 +71,18 @@ class ValidatingArrayLoader implements LoaderInterface } 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']); + } } } diff --git a/tests/Composer/Test/Package/Loader/ArrayLoaderTest.php b/tests/Composer/Test/Package/Loader/ArrayLoaderTest.php index 829fc633e..d1f42d11b 100644 --- a/tests/Composer/Test/Package/Loader/ArrayLoaderTest.php +++ b/tests/Composer/Test/Package/Loader/ArrayLoaderTest.php @@ -314,4 +314,15 @@ class ArrayLoaderTest extends TestCase $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()); + } }