From d2d84740139af5afd75d0b9829d8892000c706e1 Mon Sep 17 00:00:00 2001 From: Zan Baldwin Date: Sat, 20 Aug 2022 08:58:17 +0200 Subject: [PATCH] Do not apply non-array package links in ArrayLoader (#11008) --- src/Composer/Package/Loader/ArrayLoader.php | 21 ++++++++------- .../Test/Package/Loader/ArrayLoaderTest.php | 27 +++++++++++++++++++ 2 files changed, 38 insertions(+), 10 deletions(-) diff --git a/src/Composer/Package/Loader/ArrayLoader.php b/src/Composer/Package/Loader/ArrayLoader.php index 9491710b6..72e0df76a 100644 --- a/src/Composer/Package/Loader/ArrayLoader.php +++ b/src/Composer/Package/Loader/ArrayLoader.php @@ -55,17 +55,18 @@ class ArrayLoader implements LoaderInterface $package = $this->createObject($config, $class); foreach (BasePackage::$supportedLinkTypes as $type => $opts) { - if (isset($config[$type])) { - $method = 'set'.ucfirst($opts['method']); - $package->{$method}( - $this->parseLinks( - $package->getName(), - $package->getPrettyVersion(), - $opts['method'], - $config[$type] - ) - ); + if (!isset($config[$type]) || !is_array($config[$type])) { + continue; } + $method = 'set'.ucfirst($opts['method']); + $package->{$method}( + $this->parseLinks( + $package->getName(), + $package->getPrettyVersion(), + $opts['method'], + $config[$type] + ) + ); } $package = $this->configureObject($package, $config); diff --git a/tests/Composer/Test/Package/Loader/ArrayLoaderTest.php b/tests/Composer/Test/Package/Loader/ArrayLoaderTest.php index 6dbc71a59..0ae37b030 100644 --- a/tests/Composer/Test/Package/Loader/ArrayLoaderTest.php +++ b/tests/Composer/Test/Package/Loader/ArrayLoaderTest.php @@ -402,4 +402,31 @@ class ArrayLoaderTest extends TestCase $package = $this->loader->load($config); $this->assertCount(0, $package->getRequires()); } + + public function testPackageLinksReplace(): void + { + $config = array( + 'name' => 'acme/package', + 'version' => 'dev-1', + 'replace' => [ + 'coyote/package' => 'self.version', + ], + ); + + $package = $this->loader->load($config); + $this->assertArrayHasKey('coyote/package', $package->getReplaces()); + $this->assertSame('dev-1', $package->getReplaces()['coyote/package']->getConstraint()->getPrettyString()); + } + + public function testPackageLinksReplaceInvalid(): void + { + $config = array( + 'name' => 'acme/package', + 'version' => 'dev-1', + 'replace' => 'coyote/package', + ); + + $package = $this->loader->load($config); + $this->assertCount(0, $package->getReplaces()); + } }