FileSystemHelper   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Test Coverage

Coverage 88.89%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
eloc 15
c 1
b 0
f 0
dl 0
loc 44
ccs 16
cts 18
cp 0.8889
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A exec() 0 3 1
A fileSize() 0 3 1
A fileExists() 0 3 1
B deleteDirectory() 0 21 7
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpEpub\Util;
6
7
class FileSystemHelper
8
{
9 1
    public function fileExists(string $path): bool
10
    {
11 1
        return file_exists($path);
12
    }
13
14
    /**
15
     * @param array<int, mixed> $output
16
     */
17 1
    public function exec(string $command, array &$output, int &$returnVar): void
18
    {
19 1
        exec($command, $output, $returnVar);
20
    }
21
22 1
    public function fileSize(string $path): int|false
23
    {
24 1
        return filesize($path);
25
    }
26
27
    /**
28
     * Recursively deletes a directory and its contents.
29
     */
30 57
    public function deleteDirectory(string $dir): bool
31
    {
32 57
        if (! is_dir($dir)) {
33
            return true;
34
        }
35
36 57
        $files = scandir($dir);
37
38 57
        if ($files === false) {
39
            return false;
40
        }
41
42 57
        foreach ($files as $file) {
43 57
            if ($file === '.' || $file === '..') {
44 57
                continue;
45
            }
46 37
            $filePath = $dir . DIRECTORY_SEPARATOR . $file;
47 37
            is_dir($filePath) ? $this->deleteDirectory($filePath) : unlink($filePath);
48
        }
49
50 57
        return rmdir($dir);
51
    }
52
}
53