Skip to content

Commit

Permalink
Finder: detects static method WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Oct 29, 2023
1 parent 859d675 commit 79f6cb3
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/Utils/Finder.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
* ->from('.')
* ->exclude('temp');
*
* @method static static find(string|array $masks = ['*'])
* @method static static findFiles(string|array $masks = ['*'])
* @method static static findFiles(string|array $masks = ['*'])
* @implements \IteratorAggregate<string, FileInfo>
*/
class Finder implements \IteratorAggregate
Expand Down Expand Up @@ -48,10 +51,19 @@ class Finder implements \IteratorAggregate
private bool $ignoreUnreadableDirs = true;


public static function __callStatic(string $name, array $args): mixed
{
if (in_array($name, ['find', 'findFiles', 'findDirectories'], true)) {

Check failure on line 56 in src/Utils/Finder.php

View workflow job for this annotation

GitHub Actions / PHPStan

Method Nette\Utils\Finder::__callStatic() should return mixed but return statement is missing.
$name = '_' . $name;
return self::$name(...$args);
}
}


/**
* Begins search for files and directories matching mask.
*/
public static function find(string|array $masks = ['*']): static
private static function _find(string|array $masks = ['*']): static
{
$masks = is_array($masks) ? $masks : func_get_args(); // compatibility with variadic
return (new static)->addMask($masks, 'dir')->addMask($masks, 'file');
Expand All @@ -61,7 +73,7 @@ public static function find(string|array $masks = ['*']): static
/**
* Begins search for files matching mask.
*/
public static function findFiles(string|array $masks = ['*']): static
private static function _findFiles(string|array $masks = ['*']): static
{
$masks = is_array($masks) ? $masks : func_get_args(); // compatibility with variadic
return (new static)->addMask($masks, 'file');
Expand All @@ -71,7 +83,7 @@ public static function findFiles(string|array $masks = ['*']): static
/**
* Begins search for directories matching mask.
*/
public static function findDirectories(string|array $masks = ['*']): static
private static function _findDirectories(string|array $masks = ['*']): static
{
$masks = is_array($masks) ? $masks : func_get_args(); // compatibility with variadic
return (new static)->addMask($masks, 'dir');
Expand Down
15 changes: 15 additions & 0 deletions tests/Utils/Finder.errors.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,18 @@ test('globing', function () {
"Directory './fixtures.finder/*/unknown' does not exist.",
);
});


test('wrong method', function () {
Assert::exception(
fn() => (new Finder)->findFiles('*'),
Nette\MemberAccessException::class,
'Call to undefined method Nette\Utils\Finder::findFiles().',
);

/* prevents
($finder = new Finder)
->append()
->findFiles('subdir*')
*/
});

0 comments on commit 79f6cb3

Please sign in to comment.