From 0f70c0a9c91b61c4b2ad7bb4f6e92ddf715bc273 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Tue, 6 Feb 2024 12:57:52 +0100 Subject: [PATCH] Add detection of constraints which do not match anything in validate command, fixes #11802 (#11829) --- .../Package/Loader/ValidatingArrayLoader.php | 7 +++++++ .../Package/Loader/ValidatingArrayLoaderTest.php | 14 ++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/src/Composer/Package/Loader/ValidatingArrayLoader.php b/src/Composer/Package/Loader/ValidatingArrayLoader.php index dc50a84d0..9f25ed219 100644 --- a/src/Composer/Package/Loader/ValidatingArrayLoader.php +++ b/src/Composer/Package/Loader/ValidatingArrayLoader.php @@ -17,6 +17,8 @@ use Composer\Pcre\Preg; use Composer\Semver\Constraint\Constraint; use Composer\Package\Version\VersionParser; use Composer\Repository\PlatformRepository; +use Composer\Semver\Constraint\MatchNoneConstraint; +use Composer\Semver\Intervals; use Composer\Spdx\SpdxLicenses; /** @@ -290,6 +292,11 @@ class ValidatingArrayLoader implements LoaderInterface ) { $this->warnings[] = $linkType.'.'.$package.' : exact version constraints ('.$constraint.') should be avoided if the package follows semantic versioning'; } + + $compacted = Intervals::compactConstraint($linkConstraint); + if ($compacted instanceof MatchNoneConstraint) { + $this->warnings[] = $linkType.'.'.$package.' : this version constraint cannot possibly match anything ('.$constraint.')'; + } } if ($linkType === 'conflict' && isset($this->config['replace']) && $keys = array_intersect_key($this->config['replace'], $this->config['conflict'])) { diff --git a/tests/Composer/Test/Package/Loader/ValidatingArrayLoaderTest.php b/tests/Composer/Test/Package/Loader/ValidatingArrayLoaderTest.php index 43a474997..bcd029733 100644 --- a/tests/Composer/Test/Package/Loader/ValidatingArrayLoaderTest.php +++ b/tests/Composer/Test/Package/Loader/ValidatingArrayLoaderTest.php @@ -480,6 +480,20 @@ class ValidatingArrayLoaderTest extends TestCase ], false, ], + [ + [ + 'name' => 'foo/bar', + 'require' => [ + 'foo/baz' => '>1, <0.5', + 'bar/baz' => 'dev-main, >0.5', + ], + ], + [ + 'require.foo/baz : this version constraint cannot possibly match anything (>1, <0.5)', + 'require.bar/baz : this version constraint cannot possibly match anything (dev-main, >0.5)', + ], + false, + ], [ [ 'name' => 'foo/bar',