From de716485bbdd4e6719fb90958f2491f20be7aa47 Mon Sep 17 00:00:00 2001 From: Pierre du Plessis Date: Mon, 21 Nov 2022 14:17:57 +0200 Subject: [PATCH] Correctly resolve the lock alias --- src/PackageResolver.php | 14 ++++++++++---- tests/PackageResolverTest.php | 15 +++++++++++++-- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/src/PackageResolver.php b/src/PackageResolver.php index 878b5a8f8..c7a7e5eb6 100644 --- a/src/PackageResolver.php +++ b/src/PackageResolver.php @@ -34,11 +34,11 @@ public function resolve(array $arguments = [], bool $isRequire = false): array $packages = []; foreach ($arguments as $i => $argument) { if ((false !== $pos = strpos($argument, ':')) || (false !== $pos = strpos($argument, '='))) { - $package = $this->resolvePackageName(substr($argument, 0, $pos), $i); + $package = $this->resolvePackageName(substr($argument, 0, $pos), $i, $isRequire); $version = substr($argument, $pos + 1); $packages[] = $package.':'.$version; } else { - $packages[] = $this->resolvePackageName($argument, $i); + $packages[] = $this->resolvePackageName($argument, $i, $isRequire); } } @@ -84,9 +84,15 @@ public function parseVersion(string $package, string $version, bool $isRequire): return ':'.$version; } - private function resolvePackageName(string $argument, int $position): string + private function resolvePackageName(string $argument, int $position, bool $isRequire): string { - if (false !== strpos($argument, '/') || preg_match(PlatformRepository::PLATFORM_PACKAGE_REGEX, $argument) || preg_match('{(?<=[a-z0-9_/-])\*|\*(?=[a-z0-9_/-])}i', $argument) || \in_array($argument, ['lock', 'mirrors', 'nothing', ''])) { + $skippedPackages = ['mirrors', 'nothing', '']; + + if (!$isRequire) { + $skippedPackages[] = 'lock'; + } + + if (false !== strpos($argument, '/') || preg_match(PlatformRepository::PLATFORM_PACKAGE_REGEX, $argument) || preg_match('{(?<=[a-z0-9_/-])\*|\*(?=[a-z0-9_/-])}i', $argument) || \in_array($argument, $skippedPackages)) { return $argument; } diff --git a/tests/PackageResolverTest.php b/tests/PackageResolverTest.php index 5ea41413f..31e7af221 100644 --- a/tests/PackageResolverTest.php +++ b/tests/PackageResolverTest.php @@ -20,9 +20,9 @@ class PackageResolverTest extends TestCase /** * @dataProvider getPackages */ - public function testResolve($packages, $resolved) + public function testResolve($packages, $resolved, bool $isRequire = false) { - $this->assertEquals($resolved, $this->getResolver()->resolve($packages)); + $this->assertEquals($resolved, $this->getResolver()->resolve($packages, $isRequire)); } public function getPackages() @@ -32,6 +32,16 @@ public function getPackages() ['cli'], ['symfony/console'], ], + [ + ['lock'], + ['lock'], + false, + ], + [ + ['lock'], + ['symfony/lock'], + true, + ], [ ['cli', 'symfony/workflow'], ['symfony/console', 'symfony/workflow'], @@ -116,6 +126,7 @@ private function getResolver() 'console' => 'symfony/console', 'translation' => 'symfony/translation', 'validator' => 'symfony/validator', + 'lock' => 'symfony/lock', ]); return new PackageResolver($downloader);