1
0
Fork 0

Fix only/exclude to avoid matching names as sub-strings of full package names, fixes #10001

pull/10017/head
Jordi Boggiano 2021-07-22 13:47:31 +02:00
parent 29a52ff463
commit 24f5e54fbe
No known key found for this signature in database
GPG Key ID: 7BBD42C429EC80BC
2 changed files with 7 additions and 4 deletions

View File

@ -33,17 +33,17 @@ class FilterRepository implements RepositoryInterface
if (!is_array($options['only'])) {
throw new \InvalidArgumentException('"only" key for repository '.$repo->getRepoName().' should be an array');
}
$this->only = '{^'.implode('|', array_map(function ($val) {
$this->only = '{^(?:'.implode('|', array_map(function ($val) {
return BasePackage::packageNameToRegexp($val, '%s');
}, $options['only'])) .'$}iD';
}, $options['only'])) .')$}iD';
}
if (isset($options['exclude'])) {
if (!is_array($options['exclude'])) {
throw new \InvalidArgumentException('"exclude" key for repository '.$repo->getRepoName().' should be an array');
}
$this->exclude = '{^'.implode('|', array_map(function ($val) {
$this->exclude = '{^(?:'.implode('|', array_map(function ($val) {
return BasePackage::packageNameToRegexp($val, '%s');
}, $options['exclude'])) .'$}iD';
}, $options['exclude'])) .')$}iD';
}
if ($this->exclude && $this->only) {
throw new \InvalidArgumentException('Only one of "only" and "exclude" can be specified for repository '.$repo->getRepoName());

View File

@ -50,6 +50,9 @@ class FilterRepositoryTest extends TestCase
array(array('foo/aaa', 'foo/bbb'), array('only' => array('foo/*'))),
array(array('foo/aaa', 'baz/yyy'), array('only' => array('foo/aaa', 'baz/yyy'))),
array(array('bar/xxx'), array('exclude' => array('foo/*', 'baz/yyy'))),
// make sure sub-patterns are not matched without wildcard
array(array('foo/aaa', 'foo/bbb', 'bar/xxx', 'baz/yyy'), array('exclude' => array('foo/aa', 'az/yyy'))),
array(array(), array('only' => array('foo/aa', 'az/yyy'))),
);
}