Passed
Push — master ( 222099...ff3894 )
by Saulius
01:46
created

remove_directories_and_files_in_directory()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 6
nc 3
nop 2
dl 0
loc 10
ccs 5
cts 5
cp 1
crap 4
rs 9.2
c 0
b 0
f 0
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;
14
15
function rrmdir(string $directory): bool
16
{
17 1
    if (is_dir($directory)) {
18
19 1
        $objects = scandir($directory, SCANDIR_SORT_NONE);
20
21 1
        foreach ($objects as $object) {
22 1
            remove_directories_and_files_in_directory($object, $directory);
23
        }
24
25 1
        reset($objects);
26
27 1
        return rmdir($directory);
28
    }
29
30 1
    return false;
31
}
32
33
function remove_directories_and_files_in_directory($object, string $directory): void
34
{
35 1
    if ($object !== '.' && $object !== '..') {
36
37 1
        $currentDirectory = create_directory_path([$directory, $object]);
38
39 1
        if (filetype($currentDirectory) === 'dir') {
40 1
            rrmdir($currentDirectory);
41
        } else {
42 1
            unlink($currentDirectory);
43
        }
44
    }
45 1
}
46
47
function create_directory_path(array $directories): string
48
{
49 1
    return implode(DIRECTORY_SEPARATOR, $directories);
50
}
51