removeSubDirectoriesAndFiles()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 6
c 1
b 0
f 0
nc 3
nop 2
dl 0
loc 10
ccs 6
cts 6
cp 1
crap 4
rs 10
1
<?php
2
/**
3
 * This file is part of the sauls/helpers package.
4
 *
5
 * @author    Saulius Vaičeliūnas <[email protected]>
6
 * @link      http://saulius.vaiceliunas.lt
7
 * @copyright 2018
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
namespace Sauls\Component\Helper\Operation\FilesystemOperation;
14
15
16
class RemoveDirectoryRecursively implements RemoveDirectoryRecursivelyInterface
17
{
18 1
    public function execute(string $directory): bool
19
    {
20 1
        if (\is_dir($directory)) {
21 1
            $objects = \scandir($directory, SCANDIR_SORT_NONE);
22 1
            foreach ($objects as $object) {
23 1
                $this->removeSubDirectoriesAndFiles($object, $directory);
24
            }
25
26 1
            \reset($objects);
0 ignored issues
show
Bug introduced by
It seems like $objects can also be of type false; however, parameter $array of reset() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

26
            \reset(/** @scrutinizer ignore-type */ $objects);
Loading history...
27
28 1
            return \rmdir($directory);
29
        }
30
31 1
        return false;
32
    }
33
34 1
    private function removeSubDirectoriesAndFiles($object, string $directory): void
35
    {
36 1
        if ($object !== '.' && $object !== '..') {
37
38 1
            $currentDirectory = $this->createDirectoryPath([$directory, $object]);
39
40 1
            if (filetype($currentDirectory) === 'dir') {
41 1
                $this->execute($currentDirectory);
42
            } else {
43 1
                \unlink($currentDirectory);
44
            }
45
        }
46 1
    }
47
48 1
    private function createDirectoryPath(array $directories): string
49
    {
50 1
        return implode(DIRECTORY_SEPARATOR, $directories);
51
    }
52
}
53