GeneratorHelper   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 24
c 2
b 1
f 0
dl 0
loc 54
ccs 0
cts 37
cp 0
rs 10
wmc 12

2 Methods

Rating   Name   Duplication   Size   Complexity  
B copyRecursive() 0 25 7
A rmdir() 0 12 5
1
<?php namespace Todaymade\Daux;
2
3
use RuntimeException;
4
5
class GeneratorHelper
6
{
7
    /**
8
     * Remove a directory recursively.
9
     *
10
     * @param string $dir
11
     */
12
    public static function rmdir($dir)
13
    {
14
        $it = new \RecursiveDirectoryIterator($dir);
15
        $files = new \RecursiveIteratorIterator($it, \RecursiveIteratorIterator::CHILD_FIRST);
16
        foreach ($files as $file) {
17
            if ($file->getFilename() === '.' || $file->getFilename() === '..') {
18
                continue;
19
            }
20
            if ($file->isDir()) {
21
                rmdir($file->getRealPath());
22
            } else {
23
                unlink($file->getRealPath());
24
            }
25
        }
26
    }
27
28
    /**
29
     * Copy files recursively.
30
     *
31
     * @param string $source
32
     * @param string $destination
33
     */
34
    public static function copyRecursive($source, $destination)
35
    {
36
        if (!is_dir($destination)) {
37
            mkdir($destination);
38
        }
39
40
        $dir = opendir($source);
41
42
        if ($dir === false) {
43
            throw new RuntimeException("Cannot copy '$source' to '$destination'");
44
        }
45
46
        while (false !== ($file = readdir($dir))) {
47
            if ($file != '.' && $file != '..') {
48
                if (is_dir($source . DIRECTORY_SEPARATOR . $file)) {
49
                    static::copyRecursive(
50
                        $source . DIRECTORY_SEPARATOR . $file,
51
                        $destination . DIRECTORY_SEPARATOR . $file
52
                    );
53
                } else {
54
                    copy($source . DIRECTORY_SEPARATOR . $file, $destination . DIRECTORY_SEPARATOR . $file);
55
                }
56
            }
57
        }
58
        closedir($dir);
59
    }
60
}
61