Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
80.56% covered (warning)
80.56%
29 / 36
50.00% covered (danger)
50.00%
3 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
Dir
80.56% covered (warning)
80.56%
29 / 36
50.00% covered (danger)
50.00%
3 / 6
14.24
0.00% covered (danger)
0.00%
0 / 1
 systemPath
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
2
 parse
80.00% covered (warning)
80.00%
8 / 10
0.00% covered (danger)
0.00%
0 / 1
3.07
 isReadable
85.71% covered (warning)
85.71%
6 / 7
0.00% covered (danger)
0.00%
0 / 1
4.05
 create
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 cleanFileList
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 prependfullDirPath
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5namespace Projom\Util;
6
7use Projom\Util\File;
8
9class Dir
10{
11    public static function systemPath(string $srcDir = 'src'): string
12    {
13        $dir = __DIR__;
14        $srcPos = strpos($dir, $srcDir);
15        if ($srcPos === false)
16            return '';
17
18        $systemDir = rtrim(
19            substr($dir, 0, $srcPos),
20            DIRECTORY_SEPARATOR
21        );
22
23        return $systemDir;
24    }
25
26    public static function parse(string $fullDirPath): array
27    {
28        $readable = Dir::isReadable($fullDirPath);
29        if (!$readable)
30            return [];
31
32        $fileList = scandir($fullDirPath);
33        if (!$fileList)
34            return [];
35
36        $fileList = static::cleanFileList($fileList);
37        $fileList = static::prependfullDirPath($fullDirPath, $fileList);
38
39        $parsedFileList = File::parseList($fileList);
40
41        return $parsedFileList;
42    }
43
44    public static function isReadable(string $fullDirPath): bool
45    {
46        if (!$fullDirPath)
47            return false;
48        if (!is_dir($fullDirPath))
49            return false;
50        if (!is_readable($fullDirPath))
51            return false;
52        return true;
53    }
54
55    public static function create(string $fullDirPath, int $permission = 0777, bool $recursive = false): bool
56    {
57        if (is_dir($fullDirPath))
58            return true;
59
60        $isCreated = mkdir($fullDirPath, $permission, $recursive);
61        return $isCreated;
62    }
63
64    public static function cleanFileList(array $fileList): array
65    {
66        $unwanted = [
67            '.',
68            '..'
69        ];
70        return array_diff($fileList, $unwanted);
71    }
72
73    public static function prependfullDirPath(string $fullDirPath, array $fileList): array 
74    {
75        return array_map(fn ($file) => $fullDirPath . DIRECTORY_SEPARATOR . $file, $fileList);
76    }
77}