Refactor ignore platform reqs checks (#10079)
Introduces a `PlatformRequirementFilter` with methods that help to decide if a requirement is ignored or not as discussed in #10045 but without changing behaviour.main
parent
68847ed609
commit
3013674c92
@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace Composer\Filter\PlatformRequirementFilter;
|
||||
|
||||
use Composer\Repository\PlatformRepository;
|
||||
|
||||
final class IgnoreAllPlatformRequirementFilter implements PlatformRequirementFilterInterface
|
||||
{
|
||||
/**
|
||||
* @param string $req
|
||||
* @return bool
|
||||
*/
|
||||
public function isIgnored($req)
|
||||
{
|
||||
return PlatformRepository::isPlatformPackage($req);
|
||||
}
|
||||
}
|
@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
namespace Composer\Filter\PlatformRequirementFilter;
|
||||
|
||||
use Composer\Repository\PlatformRepository;
|
||||
|
||||
final class IgnoreListPlatformRequirementFilter implements PlatformRequirementFilterInterface
|
||||
{
|
||||
/**
|
||||
* @var string[]
|
||||
*/
|
||||
private $reqList;
|
||||
|
||||
/**
|
||||
* @param string[] $reqList
|
||||
*/
|
||||
public function __construct(array $reqList)
|
||||
{
|
||||
$this->reqList = $reqList;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $req
|
||||
* @return bool
|
||||
*/
|
||||
public function isIgnored($req)
|
||||
{
|
||||
if (!PlatformRepository::isPlatformPackage($req)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return in_array($req, $this->reqList, true);
|
||||
}
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
namespace Composer\Filter\PlatformRequirementFilter;
|
||||
|
||||
final class IgnoreNothingPlatformRequirementFilter implements PlatformRequirementFilterInterface
|
||||
{
|
||||
/**
|
||||
* @param string $req
|
||||
* @return false
|
||||
*/
|
||||
public function isIgnored($req)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
namespace Composer\Filter\PlatformRequirementFilter;
|
||||
|
||||
final class PlatformRequirementFilterFactory
|
||||
{
|
||||
/**
|
||||
* @param mixed $boolOrList
|
||||
*
|
||||
* @return PlatformRequirementFilterInterface
|
||||
*/
|
||||
public static function fromBoolOrList($boolOrList)
|
||||
{
|
||||
if (is_bool($boolOrList)) {
|
||||
return $boolOrList ? self::ignoreAll() : self::ignoreNothing();
|
||||
}
|
||||
|
||||
if (is_array($boolOrList)) {
|
||||
return new IgnoreListPlatformRequirementFilter($boolOrList);
|
||||
}
|
||||
|
||||
throw new \InvalidArgumentException(
|
||||
sprintf(
|
||||
'PlatformRequirementFilter: Unknown $boolOrList parameter %s. Please report at https://github.com/composer/composer/issues/new.',
|
||||
gettype($boolOrList)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return PlatformRequirementFilterInterface
|
||||
*/
|
||||
public static function ignoreAll()
|
||||
{
|
||||
return new IgnoreAllPlatformRequirementFilter();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return PlatformRequirementFilterInterface
|
||||
*/
|
||||
public static function ignoreNothing()
|
||||
{
|
||||
return new IgnoreNothingPlatformRequirementFilter();
|
||||
}
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
namespace Composer\Filter\PlatformRequirementFilter;
|
||||
|
||||
interface PlatformRequirementFilterInterface
|
||||
{
|
||||
/**
|
||||
* @param string $req
|
||||
* @return bool
|
||||
*/
|
||||
public function isIgnored($req);
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
namespace Composer\Test\Filter\PlatformRequirementFilter;
|
||||
|
||||
use Composer\Filter\PlatformRequirementFilter\IgnoreAllPlatformRequirementFilter;
|
||||
use Composer\Test\TestCase;
|
||||
|
||||
final class IgnoreAllPlatformRequirementFilterTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider dataIsIgnored
|
||||
*
|
||||
* @param string $req
|
||||
* @param bool $expectIgnored
|
||||
*/
|
||||
public function testIsIgnored($req, $expectIgnored)
|
||||
{
|
||||
$platformRequirementFilter = new IgnoreAllPlatformRequirementFilter();
|
||||
|
||||
$this->assertSame($expectIgnored, $platformRequirementFilter->isIgnored($req));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, mixed[]>
|
||||
*/
|
||||
public function dataIsIgnored()
|
||||
{
|
||||
return array(
|
||||
'php is ignored' => array('php', true),
|
||||
'monolog/monolog is not ignored' => array('monolog/monolog', false),
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
namespace Composer\Test\Filter\PlatformRequirementFilter;
|
||||
|
||||
use Composer\Filter\PlatformRequirementFilter\IgnoreListPlatformRequirementFilter;
|
||||
use Composer\Test\TestCase;
|
||||
|
||||
final class IgnoreListPlatformRequirementFilterTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider dataIsIgnored
|
||||
*
|
||||
* @param string[] $reqList
|
||||
* @param string $req
|
||||
* @param bool $expectIgnored
|
||||
*/
|
||||
public function testIsIgnored(array $reqList, $req, $expectIgnored)
|
||||
{
|
||||
$platformRequirementFilter = new IgnoreListPlatformRequirementFilter($reqList);
|
||||
|
||||
$this->assertSame($expectIgnored, $platformRequirementFilter->isIgnored($req));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, mixed[]>
|
||||
*/
|
||||
public function dataIsIgnored()
|
||||
{
|
||||
return array(
|
||||
'ext-json is ignored if listed' => array(array('ext-json', 'monolog/monolog'), 'ext-json', true),
|
||||
'php is not ignored if not listed' => array(array('ext-json', 'monolog/monolog'), 'php', false),
|
||||
'monolog/monolog is not ignored even if listed' => array(array('ext-json', 'monolog/monolog'), 'monolog/monolog', false),
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
namespace Composer\Test\Filter\PlatformRequirementFilter;
|
||||
|
||||
use Composer\Filter\PlatformRequirementFilter\IgnoreNothingPlatformRequirementFilter;
|
||||
use Composer\Test\TestCase;
|
||||
|
||||
final class IgnoreNothingPlatformRequirementFilterTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider dataIsIgnored
|
||||
*
|
||||
* @param string $req
|
||||
*/
|
||||
public function testIsIgnored($req)
|
||||
{
|
||||
$platformRequirementFilter = new IgnoreNothingPlatformRequirementFilter();
|
||||
|
||||
$this->assertFalse($platformRequirementFilter->isIgnored($req));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, mixed[]>
|
||||
*/
|
||||
public function dataIsIgnored()
|
||||
{
|
||||
return array(
|
||||
'php is not ignored' => array('php'),
|
||||
'monolog/monolog is not ignored' => array('monolog/monolog'),
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
namespace Composer\Test\Filter\PlatformRequirementFilter;
|
||||
|
||||
use Composer\Filter\PlatformRequirementFilter\PlatformRequirementFilterFactory;
|
||||
use Composer\Test\TestCase;
|
||||
|
||||
final class PlatformRequirementFilterFactoryTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider dataFromBoolOrList
|
||||
*
|
||||
* @param mixed $boolOrList
|
||||
* @param class-string $expectedInstance
|
||||
*/
|
||||
public function testFromBoolOrList($boolOrList, $expectedInstance)
|
||||
{
|
||||
$this->assertInstanceOf($expectedInstance, PlatformRequirementFilterFactory::fromBoolOrList($boolOrList));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, mixed[]>
|
||||
*/
|
||||
public function dataFromBoolOrList()
|
||||
{
|
||||
return array(
|
||||
'true creates IgnoreAllFilter' => array(true, 'Composer\Filter\PlatformRequirementFilter\IgnoreAllPlatformRequirementFilter'),
|
||||
'false creates IgnoreNothingFilter' => array(false, 'Composer\Filter\PlatformRequirementFilter\IgnoreNothingPlatformRequirementFilter'),
|
||||
'list creates IgnoreListFilter' => array(array('php', 'ext-json'), 'Composer\Filter\PlatformRequirementFilter\IgnoreListPlatformRequirementFilter'),
|
||||
);
|
||||
}
|
||||
|
||||
public function testFromBoolThrowsExceptionIfTypeIsUnknown()
|
||||
{
|
||||
$this->setExpectedException('InvalidArgumentException', 'PlatformRequirementFilter: Unknown $boolOrList parameter NULL. Please report at https://github.com/composer/composer/issues/new.');
|
||||
|
||||
PlatformRequirementFilterFactory::fromBoolOrList(null);
|
||||
}
|
||||
|
||||
public function testIgnoreAll()
|
||||
{
|
||||
$platformRequirementFilter = PlatformRequirementFilterFactory::ignoreAll();
|
||||
|
||||
$this->assertInstanceOf('Composer\Filter\PlatformRequirementFilter\IgnoreAllPlatformRequirementFilter', $platformRequirementFilter);
|
||||
}
|
||||
|
||||
public function testIgnoreNothing()
|
||||
{
|
||||
$platformRequirementFilter = PlatformRequirementFilterFactory::ignoreNothing();
|
||||
|
||||
$this->assertInstanceOf('Composer\Filter\PlatformRequirementFilter\IgnoreNothingPlatformRequirementFilter', $platformRequirementFilter);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue