Skip to content

Commit

Permalink
Fix warnings incorrectly being shown when using require with upper bo…
Browse files Browse the repository at this point in the history
…und ignored on platform requirements, fixes composer#11722 (composer#11786)
  • Loading branch information
Seldaek authored and theoboldalex committed Jan 10, 2024
1 parent 719c4ea commit 3949831
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,9 @@ public function isIgnored(string $req): bool
{
return PlatformRepository::isPlatformPackage($req);
}

public function isUpperBoundIgnored(string $req): bool
{
return $this->isIgnored($req);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,15 @@ public function isIgnored(string $req): bool
return Preg::isMatch($this->ignoreRegex, $req);
}

public function isUpperBoundIgnored(string $req): bool
{
if (!PlatformRepository::isPlatformPackage($req)) {
return false;
}

return $this->isIgnored($req) || Preg::isMatch($this->ignoreUpperBoundRegex, $req);
}

/**
* @param bool $allowUpperBoundOverride For conflicts we do not want the upper bound to be skipped
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,12 @@ public function isIgnored(string $req): bool
{
return false;
}

/**
* @return false
*/
public function isUpperBoundIgnored(string $req): bool
{
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@
interface PlatformRequirementFilterInterface
{
public function isIgnored(string $req): bool;

public function isUpperBoundIgnored(string $req): bool;
}
8 changes: 8 additions & 0 deletions src/Composer/Package/Version/VersionSelector.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
namespace Composer\Package\Version;

use Composer\Filter\PlatformRequirementFilter\IgnoreAllPlatformRequirementFilter;
use Composer\Filter\PlatformRequirementFilter\IgnoreListPlatformRequirementFilter;
use Composer\Filter\PlatformRequirementFilter\PlatformRequirementFilterFactory;
use Composer\Filter\PlatformRequirementFilter\PlatformRequirementFilterInterface;
use Composer\IO\IOInterface;
Expand Down Expand Up @@ -130,6 +131,13 @@ public function findBestCandidate(string $packageName, ?string $targetPackageVer
// constraint satisfied, go to next require
continue 2;
}
if ($platformRequirementFilter instanceof IgnoreListPlatformRequirementFilter && $platformRequirementFilter->isUpperBoundIgnored($name)) {
$filteredConstraint = $platformRequirementFilter->filterConstraint($name, $link->getConstraint());
if ($filteredConstraint->matches($providedConstraint)) {
// constraint satisfied with the upper bound ignored, go to next require
continue 2;
}
}
}

// constraint not satisfied
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public function testIsIgnored(string $req, bool $expectIgnored): void
$platformRequirementFilter = new IgnoreAllPlatformRequirementFilter();

$this->assertSame($expectIgnored, $platformRequirementFilter->isIgnored($req));
$this->assertSame($expectIgnored, $platformRequirementFilter->isUpperBoundIgnored($req));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,37 @@ public static function dataIsIgnored(): array
'list entries are not completing each other' => [['ext-', 'foo'], 'ext-foo', false],
];
}

/**
* @dataProvider dataIsUpperBoundIgnored
*
* @param string[] $reqList
*/
public function testIsUpperBoundIgnored(array $reqList, string $req, bool $expectIgnored): void
{
$platformRequirementFilter = new IgnoreListPlatformRequirementFilter($reqList);

$this->assertSame($expectIgnored, $platformRequirementFilter->isUpperBoundIgnored($req));
}

/**
* @return array<string, mixed[]>
*/
public static function dataIsUpperBoundIgnored(): array
{
return [
'ext-json is ignored if listed and fully ignored' => [['ext-json', 'monolog/monolog'], 'ext-json', true],
'ext-json is ignored if listed and upper bound ignored' => [['ext-json+', 'monolog/monolog'], 'ext-json', true],
'php is not ignored if not listed' => [['ext-json+', 'monolog/monolog'], 'php', false],
'monolog/monolog is not ignored even if listed' => [['monolog/monolog'], 'monolog/monolog', false],
'ext-json is ignored if ext-* is listed' => [['ext-*+'], 'ext-json', true],
'php is ignored if php* is listed' => [['ext-*+', 'php*+'], 'php', true],
'ext-json is ignored if * is listed' => [['foo', '*+'], 'ext-json', true],
'php is ignored if * is listed' => [['*+', 'foo'], 'php', true],
'monolog/monolog is not ignored even if * or monolog/* are listed' => [['*+', 'monolog/*+'], 'monolog/monolog', false],
'empty list entry does not ignore' => [[''], 'ext-foo', false],
'empty array does not ignore' => [[], 'ext-foo', false],
'list entries are not completing each other' => [['ext-', 'foo'], 'ext-foo', false],
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public function testIsIgnored(string $req): void
$platformRequirementFilter = new IgnoreNothingPlatformRequirementFilter();

$this->assertFalse($platformRequirementFilter->isIgnored($req)); // @phpstan-ignore-line
$this->assertFalse($platformRequirementFilter->isUpperBoundIgnored($req)); // @phpstan-ignore-line
}

/**
Expand Down

0 comments on commit 3949831

Please sign in to comment.