TRemoveCycle::removeCycle()   B
last analyzed

Complexity

Conditions 9
Paths 5

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 9

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 9
eloc 16
c 1
b 0
f 0
nc 5
nop 2
dl 0
loc 24
ccs 16
cts 16
cp 1
crap 9
rs 8.0555
1
<?php
2
3
namespace kalanis\kw_storage\Extras;
4
5
6
/**
7
 * Trait TRemoveCycle
8
 * @package kalanis\kw_storage\Extras
9
 * low-level work with extended dirs - remove dirs and files in cycle - everything with subdirectories
10
 */
11
trait TRemoveCycle
12
{
13
    /**
14
     * Remove sub dirs and their content recursively
15
     * @param string $dirPath
16
     * @param string $sign
17
     * @return bool
18
     */
19 4
    protected function removeCycle(string $dirPath, string $sign = DIRECTORY_SEPARATOR): bool
20
    {
21 4
        $path = realpath($dirPath);
22 4
        if (false === $path) {
23 2
            return false;
24
        }
25 4
        if (is_dir($path)) {
26 2
            $fileListing = scandir($path);
27 2
            if (!empty($fileListing)) {
28 2
                foreach ($fileListing as $fileName) {
29 2
                    if (is_dir($path . $sign . $fileName)) {
30 2
                        if (('.' != $fileName) && ('..' != $fileName)) {
31 2
                            $this->removeCycle($path . $sign . $fileName);
32
                        }
33
                    } else {
34 2
                        unlink($path . $sign . $fileName);
35
                    }
36
                }
37
            }
38 2
            rmdir($path);
39 2
        } elseif (is_file($path)) {
40 2
            unlink($path);
41
        }
42 4
        return true;
43
    }
44
}
45