Skip to content

Commit

Permalink
Finder: variadics are not preferred anymore
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Feb 2, 2023
1 parent 39ce6e5 commit 7828eba
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 33 deletions.
30 changes: 12 additions & 18 deletions src/Utils/Finder.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,33 +43,30 @@ class Finder implements \IteratorAggregate

/**
* Begins search for files and directories matching mask.
* @param string ...$masks
*/
public static function find(...$masks): static
public static function find(string|array $masks): static
{
$masks = is_array($tmp = reset($masks)) ? $tmp : $masks;
$masks = is_array($masks) ? $masks : func_get_args(); // compatibility with variadic
return (new static)->addMask($masks, 'dir')->addMask($masks, 'file');
}


/**
* Begins search for files matching mask.
* @param string ...$masks
*/
public static function findFiles(...$masks): static
public static function findFiles(string|array $masks): static
{
$masks = is_array($tmp = reset($masks)) ? $tmp : $masks;
$masks = is_array($masks) ? $masks : func_get_args(); // compatibility with variadic
return (new static)->addMask($masks, 'file');
}


/**
* Begins search for directories matching mask.
* @param string ...$masks
*/
public static function findDirectories(...$masks): static
public static function findDirectories(string|array $masks): static
{
$masks = is_array($tmp = reset($masks)) ? $tmp : $masks;
$masks = is_array($masks) ? $masks : func_get_args(); // compatibility with variadic
return (new static)->addMask($masks, 'dir');
}

Expand All @@ -93,23 +90,21 @@ private function addMask(array $masks, string $type): static

/**
* Searches in the given directories. Wildcards are allowed.
* @param string ...$paths
*/
public function in(...$paths): static
public function in(string|array $paths): static
{
$paths = is_array($tmp = reset($paths)) ? $tmp : $paths;
$paths = is_array($paths) ? $paths : func_get_args(); // compatibility with variadic
$this->addLocation($paths, '');
return $this;
}


/**
* Searches recursively from the given directories. Wildcards are allowed.
* @param string ...$paths
*/
public function from(...$paths): static
public function from(string|array $paths): static
{
$paths = is_array($tmp = reset($paths)) ? $tmp : $paths;
$paths = is_array($paths) ? $paths : func_get_args(); // compatibility with variadic
$this->addLocation($paths, '/**');
return $this;
}
Expand Down Expand Up @@ -142,11 +137,10 @@ public function childFirst(bool $state = true): static

/**
* Skips entries that matches the given masks relative to the ones defined with the in() or from() methods.
* @param string ...$masks
*/
public function exclude(...$masks): static
public function exclude(string|array $masks): static
{
$masks = is_array($tmp = reset($masks)) ? $tmp : $masks;
$masks = is_array($masks) ? $masks : func_get_args(); // compatibility with variadic
foreach ($masks as $mask) {
$mask = FileSystem::unixSlashes($mask);
if (!preg_match('~^/?(\*\*/)?(.+)(/\*\*|/\*|/|)$~D', $mask, $m)) {
Expand Down
15 changes: 0 additions & 15 deletions tests/Utils/Finder.basic.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -140,18 +140,3 @@ test('absolute path in mask', function () { // will not work if there are charac
FileSystem::unixSlashes(__DIR__),
], export($finder));
});


test('empty args', function () {
$finder = Finder::find()->in('fixtures.finder');
Assert::same([], export($finder));

$finder = Finder::findFiles()->in('fixtures.finder');
Assert::same([], export($finder));

$finder = Finder::findDirectories()->in('fixtures.finder');
Assert::same([], export($finder));

$finder = Finder::find()->exclude()->in('fixtures.finder');
Assert::same([], export($finder));
});

0 comments on commit 7828eba

Please sign in to comment.