Passed
Push — master ( ab30be...0d1280 )
by Caen
03:32 queued 14s
created

FileFinder::expandCommaSeparatedValues()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 5
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Hyde\Framework\Actions\Internal;
6
7
use Hyde\Facades\Filesystem;
8
use Hyde\Hyde;
9
use Illuminate\Support\Collection;
10
use SplFileInfo;
11
use Symfony\Component\Finder\Finder;
12
13
/**
14
 * @interal This class is used internally by the framework and is not part of the public API, unless that is requested on GitHub with a valid use case.
15
 */
16
class FileFinder
17
{
18
    /**
19
     * @param  array<string>|string|false  $matchExtensions
20
     * @return \Illuminate\Support\Collection<int, string>
21
     */
22
    public static function handle(string $directory, array|string|false $matchExtensions = false, bool $recursive = false): Collection
23
    {
24
        if (! Filesystem::isDirectory($directory)) {
25
            return collect();
26
        }
27
28
        $finder = Finder::create()->files()->in(Hyde::path($directory));
0 ignored issues
show
Bug introduced by
The method path() does not exist on Hyde\Hyde. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

28
        $finder = Finder::create()->files()->in(Hyde::/** @scrutinizer ignore-call */ path($directory));
Loading history...
29
30
        if ($recursive === false) {
31
            $finder->depth('== 0');
32
        }
33
34
        if ($matchExtensions !== false) {
35
            $finder->name(static::buildFileExtensionPattern((array) $matchExtensions));
36
        }
37
38
        return collect($finder)->map(function (SplFileInfo $file): string {
0 ignored issues
show
Bug introduced by
$finder of type Symfony\Component\Finder\Finder is incompatible with the type Illuminate\Contracts\Support\Arrayable expected by parameter $value of collect(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

38
        return collect(/** @scrutinizer ignore-type */ $finder)->map(function (SplFileInfo $file): string {
Loading history...
39
            return Hyde::pathToRelative($file->getPathname());
0 ignored issues
show
Bug introduced by
The method pathToRelative() does not exist on Hyde\Hyde. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

39
            return Hyde::/** @scrutinizer ignore-call */ pathToRelative($file->getPathname());
Loading history...
40
        })->sort()->values();
41
    }
42
43
    /** @param array<string> $extensions */
44
    protected static function buildFileExtensionPattern(array $extensions): string
45
    {
46
        $extensions = self::expandCommaSeparatedValues($extensions);
47
48
        return '/\.('.self::normalizeExtensionForRegexPattern($extensions).')$/i';
49
    }
50
51
    /** @param array<string> $extensions */
52
    private static function expandCommaSeparatedValues(array $extensions): array
53
    {
54
        return array_merge(...array_map(function (string $item): array {
55
            return array_map(fn (string $item): string => trim($item), explode(',', $item));
56
        }, $extensions));
57
    }
58
59
    /** @param array<string> $extensions */
60
    private static function normalizeExtensionForRegexPattern(array $extensions): string
61
    {
62
        return implode('|', array_map(function (string $extension): string {
63
            return preg_quote(ltrim($extension, '.'), '/');
64
        }, $extensions));
65
    }
66
}
67