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

TRemoveCycle   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 35
rs 10
ccs 12
cts 12
cp 1
wmc 9

2 Methods

Rating   Name   Duplication   Size   Complexity  
B removeCycle() 0 16 7
A removeEndingSign() 0 3 2
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