Passed
Push — master ( 2b0006...a1b0fe )
by Petr
02:56
created

TRemoveCycle::removeCycle()   B

Complexity

Conditions 7
Paths 2

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 7

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 10
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 16
rs 8.8333
ccs 10
cts 10
cp 1
crap 7
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 1
    protected function removeCycle(string $dirPath, string $sign = DIRECTORY_SEPARATOR): bool
20
    {
21 1
        $path = static::removeEndingSign($dirPath, $sign);
22 1
        if (is_dir($path) && $fileListing = scandir($path)) {
23 1
            foreach ($fileListing as $fileName) {
24 1
                if (is_dir($path . $sign . $fileName)) {
25 1
                    if (('.' != $fileName) && ('..' != $fileName)) {
26 1
                        $this->removeCycle($path . $sign . $fileName);
27 1
                        rmdir($path . $sign . $fileName);
28
                    }
29
                } else {
30 1
                    unlink($path . $sign . $fileName);
31
                }
32
            }
33
        }
34 1
        return true;
35
    }
36
37
    /**
38
     * Remove ending separator sign
39
     * @param string $path
40
     * @param string $sign
41
     * @return string
42
     */
43 1
    public static function removeEndingSign(string $path, string $sign = DIRECTORY_SEPARATOR): string
44
    {
45 1
        return ($sign == mb_substr($path, -1, 1)) ? mb_substr($path, 0, -1 * mb_strlen($sign)) : $path ;
46
    }
47
}
48