Skip to content

Commit 5e9be27

Browse files
committed
Use Composer Semver VersionParser to detect exact version constraint
1 parent 3b78505 commit 5e9be27

2 files changed

Lines changed: 9 additions & 4 deletions

File tree

src/Flex.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
use Composer\Plugin\PrePoolCreateEvent;
4040
use Composer\Script\Event;
4141
use Composer\Script\ScriptEvents;
42+
use Composer\Semver\Constraint\Constraint;
4243
use Composer\Semver\VersionParser;
4344
use Symfony\Component\Console\Exception\ExceptionInterface as ConsoleExceptionInterface;
4445
use Symfony\Component\Console\Input\ArgvInput;
@@ -219,10 +220,14 @@ class_exists(__NAMESPACE__.str_replace('/', '\\', substr($file, \strlen(__DIR__)
219220
break;
220221
}
221222

222-
$symfonyRequire = preg_replace('/\.x$/', '.x-dev', getenv('SYMFONY_REQUIRE') ?: ($composer->getPackage()->getExtra()['symfony']['require'] ?? ''));
223+
$rawSymfonyRequire = getenv('SYMFONY_REQUIRE') ?: ($composer->getPackage()->getExtra()['symfony']['require'] ?? '');
224+
$symfonyRequire = preg_replace('/\.x$/', '.x-dev', $rawSymfonyRequire);
223225

224-
if ($symfonyRequire && preg_match('/^\d+(\.\d+)*$/', $symfonyRequire)) {
225-
$io->writeError(\sprintf('<warning>SYMFONY_REQUIRE="%s" is an exact version constraint. Did you mean "%s.*" or "^%s"?</>', $symfonyRequire, $symfonyRequire, $symfonyRequire));
226+
if ($rawSymfonyRequire) {
227+
$parsedConstraint = (new VersionParser())->parseConstraints($rawSymfonyRequire);
228+
if ($parsedConstraint instanceof Constraint && '==' === $parsedConstraint->getOperator()) {
229+
$io->writeError(\sprintf('<warning>SYMFONY_REQUIRE="%s" is an exact version constraint. Did you mean "%s.*" or "^%s"?</>', $rawSymfonyRequire, $rawSymfonyRequire, $rawSymfonyRequire));
230+
}
226231
}
227232

228233
if ($symfonyRequire || $this->ignorePreleases) {

tests/FlexTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public function testPostInstall()
112112
}
113113

114114
#[DataProvider('getSymfonyRequireConstraints')]
115-
public function testSymfonyRequireExactVersionWarning(string $constraint, bool $expectWarning): void
115+
public function testSymfonyRequireExactVersionWarning(string $constraint, bool $expectWarning)
116116
{
117117
$io = new BufferIO('', OutputInterface::VERBOSITY_VERBOSE);
118118

0 commit comments

Comments
 (0)