Skip to content

Commit 79f6cb3

Browse files
committed
Finder: detects static method WIP
1 parent 859d675 commit 79f6cb3

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

src/Utils/Finder.php

+15-3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
* ->from('.')
2121
* ->exclude('temp');
2222
*
23+
* @method static static find(string|array $masks = ['*'])
24+
* @method static static findFiles(string|array $masks = ['*'])
25+
* @method static static findFiles(string|array $masks = ['*'])
2326
* @implements \IteratorAggregate<string, FileInfo>
2427
*/
2528
class Finder implements \IteratorAggregate
@@ -48,10 +51,19 @@ class Finder implements \IteratorAggregate
4851
private bool $ignoreUnreadableDirs = true;
4952

5053

54+
public static function __callStatic(string $name, array $args): mixed
55+
{
56+
if (in_array($name, ['find', 'findFiles', 'findDirectories'], true)) {
57+
$name = '_' . $name;
58+
return self::$name(...$args);
59+
}
60+
}
61+
62+
5163
/**
5264
* Begins search for files and directories matching mask.
5365
*/
54-
public static function find(string|array $masks = ['*']): static
66+
private static function _find(string|array $masks = ['*']): static
5567
{
5668
$masks = is_array($masks) ? $masks : func_get_args(); // compatibility with variadic
5769
return (new static)->addMask($masks, 'dir')->addMask($masks, 'file');
@@ -61,7 +73,7 @@ public static function find(string|array $masks = ['*']): static
6173
/**
6274
* Begins search for files matching mask.
6375
*/
64-
public static function findFiles(string|array $masks = ['*']): static
76+
private static function _findFiles(string|array $masks = ['*']): static
6577
{
6678
$masks = is_array($masks) ? $masks : func_get_args(); // compatibility with variadic
6779
return (new static)->addMask($masks, 'file');
@@ -71,7 +83,7 @@ public static function findFiles(string|array $masks = ['*']): static
7183
/**
7284
* Begins search for directories matching mask.
7385
*/
74-
public static function findDirectories(string|array $masks = ['*']): static
86+
private static function _findDirectories(string|array $masks = ['*']): static
7587
{
7688
$masks = is_array($masks) ? $masks : func_get_args(); // compatibility with variadic
7789
return (new static)->addMask($masks, 'dir');

tests/Utils/Finder.errors.phpt

+15
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,18 @@ test('globing', function () {
3838
"Directory './fixtures.finder/*/unknown' does not exist.",
3939
);
4040
});
41+
42+
43+
test('wrong method', function () {
44+
Assert::exception(
45+
fn() => (new Finder)->findFiles('*'),
46+
Nette\MemberAccessException::class,
47+
'Call to undefined method Nette\Utils\Finder::findFiles().',
48+
);
49+
50+
/* prevents
51+
($finder = new Finder)
52+
->append()
53+
->findFiles('subdir*')
54+
*/
55+
});

0 commit comments

Comments
 (0)