1
0
Fork 0

Only check in require and for non-platform packages, add flag to make this warning optional, refs #2320

pull/2892/head
Jordi Boggiano 2014-04-09 15:23:43 +02:00
parent e0004d0656
commit e7b82cdd88
4 changed files with 33 additions and 29 deletions

View File

@ -16,24 +16,30 @@ use Composer\Package;
use Composer\Package\BasePackage; use Composer\Package\BasePackage;
use Composer\Package\LinkConstraint\VersionConstraint; use Composer\Package\LinkConstraint\VersionConstraint;
use Composer\Package\Version\VersionParser; use Composer\Package\Version\VersionParser;
use Composer\Repository\PlatformRepository;
/** /**
* @author Jordi Boggiano <j.boggiano@seld.be> * @author Jordi Boggiano <j.boggiano@seld.be>
*/ */
class ValidatingArrayLoader implements LoaderInterface class ValidatingArrayLoader implements LoaderInterface
{ {
const CHECK_ALL = 1;
const CHECK_UNBOUND_CONSTRAINTS = 1;
private $loader; private $loader;
private $versionParser; private $versionParser;
private $errors; private $errors;
private $warnings; private $warnings;
private $config; private $config;
private $strictName; private $strictName;
private $flags;
public function __construct(LoaderInterface $loader, $strictName = true, VersionParser $parser = null) public function __construct(LoaderInterface $loader, $strictName = true, VersionParser $parser = null, $flags = 0)
{ {
$this->loader = $loader; $this->loader = $loader;
$this->versionParser = $parser ?: new VersionParser(); $this->versionParser = $parser ?: new VersionParser();
$this->strictName = $strictName; $this->strictName = $strictName;
$this->flags = $flags;
} }
public function load(array $config, $class = 'Composer\Package\CompletePackage') public function load(array $config, $class = 'Composer\Package\CompletePackage')
@ -163,20 +169,17 @@ class ValidatingArrayLoader implements LoaderInterface
continue; continue;
} }
if ('conflict' === $linkType || 'require-dev' === $linkType) { // check requires for unbound constraints on non-platform packages
continue; // conflict can be unbound, and require-dev constraints will not impact shared libraries as they are root-only if (
} ($this->flags & self::CHECK_UNBOUND_CONSTRAINTS)
&& 'require' === $linkType
if ($linkConstraint->matches($unboundConstraint)) { && $linkConstraint->matches($unboundConstraint)
$this->warnings[] = $linkType.'.'.$package.' : unbound version constraint detected ('.$constraint.')'; && !preg_match(PlatformRepository::PLATFORM_PACKAGE_REGEX, $package)
unset($this->config[$linkType][$package]); ) {
$this->warnings[] = $linkType.'.'.$package.' : unbound version constraints ('.$constraint.') should be avoided';
} }
} }
} }
if (empty($this->config[$linkType])) {
unset($this->config[$linkType]);
}
} }
} }

View File

@ -21,7 +21,7 @@ use Composer\Plugin\PluginInterface;
*/ */
class PlatformRepository extends ArrayRepository class PlatformRepository extends ArrayRepository
{ {
const PLATFORM_PACKAGE_REGEX = '{^(?:php(?:-64bit)?|(?:ext|lib)-[^/]+)$}i'; const PLATFORM_PACKAGE_REGEX = '{^(?:php(?:-64bit)?|hhvm|(?:ext|lib)-[^/]+)$}i';
protected function initialize() protected function initialize()
{ {

View File

@ -119,7 +119,7 @@ class ConfigValidator
} }
try { try {
$loader = new ValidatingArrayLoader(new ArrayLoader()); $loader = new ValidatingArrayLoader(new ArrayLoader(), true, null, ValidatingArrayLoader::CHECK_ALL);
if (!isset($manifest['version'])) { if (!isset($manifest['version'])) {
$manifest['version'] = '1.0.0'; $manifest['version'] = '1.0.0';
} }

View File

@ -29,7 +29,7 @@ class ValidatingArrayLoaderTest extends \PHPUnit_Framework_TestCase
->method('load') ->method('load')
->with($config); ->with($config);
$loader = new ValidatingArrayLoader($internalLoader); $loader = new ValidatingArrayLoader($internalLoader, true, null, ValidatingArrayLoader::CHECK_ALL);
$loader->load($config); $loader->load($config);
} }
@ -163,7 +163,7 @@ class ValidatingArrayLoaderTest extends \PHPUnit_Framework_TestCase
public function testLoadFailureThrowsException($config, $expectedErrors) public function testLoadFailureThrowsException($config, $expectedErrors)
{ {
$internalLoader = $this->getMock('Composer\Package\Loader\LoaderInterface'); $internalLoader = $this->getMock('Composer\Package\Loader\LoaderInterface');
$loader = new ValidatingArrayLoader($internalLoader); $loader = new ValidatingArrayLoader($internalLoader, true, null, ValidatingArrayLoader::CHECK_ALL);
try { try {
$loader->load($config); $loader->load($config);
$this->fail('Expected exception to be thrown'); $this->fail('Expected exception to be thrown');
@ -181,7 +181,7 @@ class ValidatingArrayLoaderTest extends \PHPUnit_Framework_TestCase
public function testLoadWarnings($config, $expectedWarnings) public function testLoadWarnings($config, $expectedWarnings)
{ {
$internalLoader = $this->getMock('Composer\Package\Loader\LoaderInterface'); $internalLoader = $this->getMock('Composer\Package\Loader\LoaderInterface');
$loader = new ValidatingArrayLoader($internalLoader); $loader = new ValidatingArrayLoader($internalLoader, true, null, ValidatingArrayLoader::CHECK_ALL);
$loader->load($config); $loader->load($config);
$warnings = $loader->getWarnings(); $warnings = $loader->getWarnings();
@ -193,15 +193,19 @@ class ValidatingArrayLoaderTest extends \PHPUnit_Framework_TestCase
/** /**
* @dataProvider warningProvider * @dataProvider warningProvider
*/ */
public function testLoadSkipsWarningDataWhenIgnoringErrors($config) public function testLoadSkipsWarningDataWhenIgnoringErrors($config, $expectedWarnings, $mustCheck = true)
{ {
if (!$mustCheck) {
$this->assertTrue(true);
return;
}
$internalLoader = $this->getMock('Composer\Package\Loader\LoaderInterface'); $internalLoader = $this->getMock('Composer\Package\Loader\LoaderInterface');
$internalLoader $internalLoader
->expects($this->once()) ->expects($this->once())
->method('load') ->method('load')
->with(array('name' => 'a/b')); ->with(array('name' => 'a/b'));
$loader = new ValidatingArrayLoader($internalLoader); $loader = new ValidatingArrayLoader($internalLoader, true, null, ValidatingArrayLoader::CHECK_ALL);
$config['name'] = 'a/b'; $config['name'] = 'a/b';
$loader->load($config); $loader->load($config);
} }
@ -297,20 +301,17 @@ class ValidatingArrayLoaderTest extends \PHPUnit_Framework_TestCase
'require' => array( 'require' => array(
'foo/baz' => '*', 'foo/baz' => '*',
'bar/baz' => '>=1.0', 'bar/baz' => '>=1.0',
),
'provide' => array(
'bar/foo' => 'dev-master', 'bar/foo' => 'dev-master',
),
'replace' => array(
'bar/hacked' => '@stable', 'bar/hacked' => '@stable',
) ),
), ),
array( array(
'require.foo/baz : unbound version constraint detected (*)', 'require.foo/baz : unbound version constraints (*) should be avoided',
'require.bar/baz : unbound version constraint detected (>=1.0)', 'require.bar/baz : unbound version constraints (>=1.0) should be avoided',
'provide.bar/foo : unbound version constraint detected (dev-master)', 'require.bar/foo : unbound version constraints (dev-master) should be avoided',
'replace.bar/hacked : unbound version constraint detected (@stable)', 'require.bar/hacked : unbound version constraints (@stable) should be avoided',
) ),
false
), ),
); );
} }