Passed
Push — master ( 991ec5...f8d0da )
by Caen
03:23 queued 14s
created

ForwardsIlluminateFilesystem::__callStatic()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 9
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 9
b 0
f 0
nc 1
nop 2
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Hyde\Framework\Concerns\Internal;
6
7
use Illuminate\Filesystem\Filesystem;
8
use Illuminate\Support\Collection;
9
use Illuminate\Support\LazyCollection;
10
use ReflectionMethod;
11
use ReflectionParameter;
12
use Symfony\Component\Finder\SplFileInfo;
13
use function array_map;
14
use function collect;
15
use function in_array;
16
use function is_array;
17
use function is_int;
18
19
/**
20
 * Forwards calls to the Laravel File facade to the HydePHP Filesystem Facade,
21
 * while turning all paths arguments into absolute project paths.
22
 *
23
 * @interal This trait is not covered by the backward compatibility promise.
24
 *
25
 * @see \Hyde\Facades\Filesystem
26
 *
27
 * @method static bool exists(string $path)
28
 * @method static bool missing(string $path)
29
 * @method static string get(string $path, bool $lock = false)
30
 * @method static string sharedGet(string $path)
31
 * @method static mixed getRequire(string $path, array $data = [])
32
 * @method static mixed requireOnce(string $path, array $data = [])
33
 * @method static LazyCollection lines(string $path)
34
 * @method static string hash(string $path, string $algorithm = 'md5')
35
 * @method static int|bool put(string $path, string $contents, bool $lock = false)
36
 * @method static void replace(string $path, string $content)
37
 * @method static void replaceInFile(array|string $search, array|string $replace, string $path)
38
 * @method static int prepend(string $path, string $data)
39
 * @method static int append(string $path, string $data)
40
 * @method static mixed chmod(string $path, int|null $mode = null)
41
 * @method static bool delete(string|array $paths)
42
 * @method static bool move(string $path, string $target)
43
 * @method static bool copy(string $path, string $target)
44
 * @method static void link(string $target, string $link)
45
 * @method static void relativeLink(string $target, string $link)
46
 * @method static string name(string $path)
47
 * @method static string basename(string $path)
48
 * @method static string dirname(string $path)
49
 * @method static string extension(string $path)
50
 * @method static string|null guessExtension(string $path)
51
 * @method static string type(string $path)
52
 * @method static string|false mimeType(string $path)
53
 * @method static int size(string $path)
54
 * @method static int lastModified(string $path)
55
 * @method static bool isDirectory(string $directory)
56
 * @method static bool isEmptyDirectory(string $directory, bool $ignoreDotFiles = false)
57
 * @method static bool isReadable(string $path)
58
 * @method static bool isWritable(string $path)
59
 * @method static bool hasSameHash(string $firstFile, string $secondFile)
60
 * @method static bool isFile(string $file)
61
 * @method static array glob(string $pattern, int $flags = 0)
62
 * @method static SplFileInfo[] files(string $directory, bool $hidden = false)
63
 * @method static SplFileInfo[] allFiles(string $directory, bool $hidden = false)
64
 * @method static array directories(string $directory)
65
 * @method static void ensureDirectoryExists(string $path, int $mode = 0755, bool $recursive = true)
66
 * @method static bool makeDirectory(string $path, int $mode = 0755, bool $recursive = false, bool $force = false)
67
 * @method static bool moveDirectory(string $from, string $to, bool $overwrite = false)
68
 * @method static bool copyDirectory(string $directory, string $destination, int|null $options = null)
69
 * @method static bool deleteDirectory(string $directory, bool $preserve = false)
70
 * @method static bool deleteDirectories(string $directory)
71
 * @method static bool cleanDirectory(string $directory)
72
 */
73
trait ForwardsIlluminateFilesystem
74
{
75
    public static function __callStatic(string $name, array $arguments): string|array|int|bool|null|LazyCollection
76
    {
77
        return self::filesystem()->{$name}(...self::qualifyArguments(self::getParameterNames($name), $arguments));
0 ignored issues
show
Bug introduced by
The method filesystem() does not exist on Hyde\Framework\Concerns\...rdsIlluminateFilesystem. 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

77
        return self::/** @scrutinizer ignore-call */ filesystem()->{$name}(...self::qualifyArguments(self::getParameterNames($name), $arguments));
Loading history...
78
    }
79
80
    protected static function getParameterNames(string $name): array
81
    {
82
        return array_map(fn (ReflectionParameter $parameter): string => $parameter->getName(),
83
            (new ReflectionMethod(Filesystem::class, $name))->getParameters()
84
        );
85
    }
86
87
    protected static function qualifyArguments(array $parameterNames, array $arguments): Collection
88
    {
89
        return collect($arguments)->mapWithKeys(function (string|array|int|bool $argumentValue, int|string $key) use ($parameterNames): string|array|int|bool {
0 ignored issues
show
Bug introduced by
$arguments of type array 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

89
        return collect(/** @scrutinizer ignore-type */ $arguments)->mapWithKeys(function (string|array|int|bool $argumentValue, int|string $key) use ($parameterNames): string|array|int|bool {
Loading history...
90
            $argumentsToQualify = [
91
                'path', 'paths', 'file', 'target', 'directory', 'destination', 'firstFile', 'secondFile',
92
                'pattern', 'link', 'from', 'to',
93
            ];
94
95
            if (is_int($key)) {
96
                // If the argument is not named, we'll retrieve it from the reflection data.
97
                $key = $parameterNames[$key];
98
            }
99
100
            if (in_array($key, $argumentsToQualify)) {
101
                $argumentValue = self::qualifyPathArgument($argumentValue);
102
            }
103
104
            return [$key => $argumentValue];
105
        });
106
    }
107
108
    protected static function qualifyPathArgument(array|string $path): string|array
109
    {
110
        return is_array($path)
0 ignored issues
show
introduced by
The condition is_array($path) is always true.
Loading history...
111
            ? array_map(fn (string $path): string => self::qualifyPathArgument($path), $path)
112
            : self::absolutePath($path);
113
    }
114
}
115