Passed
Push — master ( 481714...398263 )
by Chema
01:30 queued 13s
created

DirectoryIo   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
c 1
b 0
f 0
dl 0
loc 30
rs 10
wmc 4

1 Method

Rating   Name   Duplication   Size   Complexity  
A removeDir() 0 25 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gacela\Console\Infrastructure;
6
7
use FilesystemIterator;
8
use Gacela\Console\Domain\Cache\DirectoryIoInterface;
9
use RecursiveDirectoryIterator;
10
use RecursiveIteratorIterator;
11
use Symfony\Component\Finder\SplFileInfo;
12
13
/**
14
 * @codeCoverageIgnore
15
 */
16
final class DirectoryIo implements DirectoryIoInterface
17
{
18
    /**
19
     * @return list<string>
20
     */
21
    public function removeDir(string $target): array
22
    {
23
        if (!is_dir($target)) {
24
            return [];
25
        }
26
27
        $files = new RecursiveIteratorIterator(
28
            new RecursiveDirectoryIterator($target, FilesystemIterator::SKIP_DOTS),
29
            RecursiveIteratorIterator::CHILD_FIRST
30
        );
31
32
        $filenames = [];
33
        /** @var SplFileInfo $file */
34
        foreach ($files as $file) {
35
            if (is_dir($file->getPathname())) {
36
                rmdir($file->getPathname());
37
            } else {
38
                unlink($file->getPathname());
39
                $filenames[] = $file->getPathname();
40
            }
41
        }
42
43
        rmdir($target);
44
45
        return $filenames;
46
    }
47
}
48