Passed
Push — master ( 08314e...a4ad40 )
by Julito
10:14
created

move()   C

Complexity

Conditions 15
Paths 12

Size

Total Lines 64
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 15
eloc 42
nc 12
nop 4
dl 0
loc 64
rs 5.9166
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/* For licensing terms, see /license.txt */
3
4
use Symfony\Component\Filesystem\Filesystem;
5
6
/**
7
 * Removes a directory recursively.
8
 *
9
 * @returns true if OK, otherwise false
10
 *
11
 * @author Amary <[email protected]> (from Nexen.net)
12
 * @author Olivier Brouckaert <[email protected]>
13
 *
14
 * @param string $dir directory to remove
15
 */
16
function removeDir($dir)
17
{
18
    if (!@$opendir = opendir($dir)) {
19
        return false;
20
    }
21
22
    while ($readdir = readdir($opendir)) {
23
        if ('..' != $readdir && '.' != $readdir) {
24
            if (is_file($dir.'/'.$readdir)) {
25
                if (!@unlink($dir.'/'.$readdir)) {
26
                    return false;
27
                }
28
            } elseif (is_dir($dir.'/'.$readdir)) {
29
                if (!removeDir($dir.'/'.$readdir)) {
30
                    return false;
31
                }
32
            }
33
        }
34
    }
35
36
    closedir($opendir);
37
38
    if (!@rmdir($dir)) {
39
        return false;
40
    }
41
42
    return true;
43
}
44
45
/**
46
 * Moves a directory and its content to an other area.
47
 *
48
 * @author Hugues Peeters <[email protected]>
49
 *
50
 * @param string $source      the path of the directory to move
51
 * @param string $destination the path of the new area
52
 * @param bool   $move        Whether we want to remove the source at the end
53
 *
54
 * @return bool false on error
55
 */
56
function copyDirTo($source, $destination, $move = true)
57
{
58
    $fs = new Filesystem();
59
    if (is_dir($source)) {
60
        $fs->mkdir($destination);
61
        if (!is_dir($destination)) {
62
            error_log("Chamilo copyDirTo cannot mkdir $destination\n");
63
64
            return false; // could not create destination dir
65
        }
66
        $fs->mirror($source, $destination);
67
        if ($move) {
68
            $fs->remove($source);
69
        }
70
    }
71
72
    return true;
73
}
74
75
/**
76
 * Get a list of all PHP (.php) files in a given directory. Includes .tpl files.
77
 *
78
 * @param string $base_path     The base path in which to find the corresponding files
79
 * @param bool   $includeStatic Include static .html, .htm and .css files
80
 *
81
 * @return array
82
 */
83
function getAllPhpFiles($base_path, $includeStatic = false)
84
{
85
    $list = scandir($base_path);
86
    $files = [];
87
    $extensionsArray = ['.php', '.tpl'];
88
    if ($includeStatic) {
89
        $extensionsArray[] = 'html';
90
        $extensionsArray[] = '.htm';
91
        $extensionsArray[] = '.css';
92
    }
93
    foreach ($list as $item) {
94
        if ('.' == substr($item, 0, 1)) {
95
            continue;
96
        }
97
        $special_dirs = [api_get_path(SYS_TEST_PATH), api_get_path(SYS_COURSE_PATH), api_get_path(SYS_LANG_PATH), api_get_path(SYS_ARCHIVE_PATH)];
0 ignored issues
show
Bug introduced by
The constant SYS_COURSE_PATH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
98
        if (in_array($base_path.$item.'/', $special_dirs)) {
99
            continue;
100
        }
101
        if (is_dir($base_path.$item)) {
102
            $files = array_merge($files, getAllPhpFiles($base_path.$item.'/', $includeStatic));
103
        } else {
104
            //only analyse php files
105
            $sub = substr($item, -4);
106
            if (in_array($sub, $extensionsArray)) {
107
                $files[] = $base_path.$item;
108
            }
109
        }
110
    }
111
    $list = null;
112
113
    return $files;
114
}
115