From e868996bdd34a45c769498658a155aa7b3257482 Mon Sep 17 00:00:00 2001 From: Nils Adermann Date: Thu, 22 Oct 2020 11:28:19 +0200 Subject: [PATCH] Validate: Warn about providing or replacing packages you require --- src/Composer/Util/ConfigValidator.php | 16 ++++++++++++++++ .../Test/Util/ConfigValidatorTest.php | 19 +++++++++++++++++++ ...composer_provide-replace-requirements.json | 17 +++++++++++++++++ 3 files changed, 52 insertions(+) create mode 100644 tests/Composer/Test/Util/Fixtures/composer_provide-replace-requirements.json diff --git a/src/Composer/Util/ConfigValidator.php b/src/Composer/Util/ConfigValidator.php index e1200fee1..8790edd09 100644 --- a/src/Composer/Util/ConfigValidator.php +++ b/src/Composer/Util/ConfigValidator.php @@ -138,6 +138,22 @@ class ConfigValidator } } + + // check for meaningless provide/replace satisfying requirements + foreach (array('provide', 'replace') as $linkType) { + if (isset($manifest[$linkType])) { + foreach (array('require', 'require-dev') as $requireType) { + if (isset($manifest[$requireType])) { + foreach ($manifest[$linkType] as $provide => $constraint) { + if (isset($manifest[$requireType][$provide])) { + $warnings[] = 'The package ' . $provide . ' in '.$requireType.' is also listed in '.$linkType.' which satisfies the requirement. Remove it from '.$linkType.' if you wish to install it.'; + } + } + } + } + } + } + // check for commit references $require = isset($manifest['require']) ? $manifest['require'] : array(); $requireDev = isset($manifest['require-dev']) ? $manifest['require-dev'] : array(); diff --git a/tests/Composer/Test/Util/ConfigValidatorTest.php b/tests/Composer/Test/Util/ConfigValidatorTest.php index 8b6ac5132..0e53e1c54 100644 --- a/tests/Composer/Test/Util/ConfigValidatorTest.php +++ b/tests/Composer/Test/Util/ConfigValidatorTest.php @@ -45,4 +45,23 @@ class ConfigValidatorTest extends TestCase $warnings ); } + + public function testConfigValidatorWarnsOnUnnecessaryProvideReplace() + { + $configValidator = new ConfigValidator(new NullIO()); + list(, , $warnings) = $configValidator->validate(__DIR__ . '/Fixtures/composer_provide-replace-requirements.json'); + + $this->assertContains( + 'The package a/a in require is also listed in provide which satisfies the requirement. Remove it from provide if you wish to install it.', + $warnings + ); + $this->assertContains( + 'The package b/b in require is also listed in replace which satisfies the requirement. Remove it from replace if you wish to install it.', + $warnings + ); + $this->assertContains( + 'The package c/c in require-dev is also listed in provide which satisfies the requirement. Remove it from provide if you wish to install it.', + $warnings + ); + } } diff --git a/tests/Composer/Test/Util/Fixtures/composer_provide-replace-requirements.json b/tests/Composer/Test/Util/Fixtures/composer_provide-replace-requirements.json new file mode 100644 index 000000000..6a18ebcf6 --- /dev/null +++ b/tests/Composer/Test/Util/Fixtures/composer_provide-replace-requirements.json @@ -0,0 +1,17 @@ +{ + "license": "MIT", + "require": { + "a/a": "^1.0", + "b/b": "^2.0" + }, + "require-dev": { + "c/c": "^2.0" + }, + "provide": { + "a/a": "1.0.0", + "c/c": "1.0.0" + }, + "replace": { + "b/b": "3.0.0" + } +}